php - 基于 Woocommerce 中特定产品变体的购物车商品折扣
问题描述
我在我的 DIY 销售商店有一种名为“可重复使用的湿”的产品,它有不同的图案和不同的包装。
- 5 可重复使用的湿法成本 10 欧元
- 10 可重复使用的湿纸成本 18 欧元
由于它们有很多不同的图案,客户可能想要 10 个湿的,但拿两个不同的包装 5 个,以获得两种不同的图案。不幸的是,这会让他多花 2 欧元,我想避免这种情况。
我怎么能检测到这种行为,更准确地说,我应该在哪里挂钩?
我想把它放在购物车预览中,如果我检测到这些包裹,可能会自动添加折扣券,但我不确定这是否是最有效的方法。
任何建议都会对我有所帮助。
解决方案
您的产品名为“Reusable wet”似乎是一种可变产品,基于某些产品属性具有多种变体。
因此,在您的情况下,可以通过 2 种不同的方式应用折扣。
但首先您需要在代码中定义“可重复使用的湿”数量包装中涉及的相关产品属性SLUG以及“5 件装”的相关术语NAME值。
如果这些设置没有以正确的方式完成,代码将无法工作。
1)更改相关商品价格 (最好的方法):
在这里,当购物车中有 2 件或更多相关商品时,我们设置
9
(18 / 2 = 9
)的折扣单价。
add_action( 'woocommerce_before_calculate_totals', 'conditionally_set_discounted_price', 30, 1 );
function conditionally_set_discounted_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE your product attribute slug for "Reusable wet" (without "pa_")
$product_attr = 'reusable-wet';
$product_attr = 'color';
// HERE set the corresponding (value) term NAME for "5 Reusable wet"
$term_slug = '5 reusable wet';
$term_slug = 'Black';
// HERE set the discounted price for each cart item with "5 Reusable wet" when they are 2 or more
$discounted_price = 9; // (18 / 2 = 9)
$five_rw_count = 0; // Initializing variable
// 1st Loop: count cart items with "5 Reusable wet"
foreach ( $cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_attribute( $product_attr ) == $term_slug ){
$five_rw_count += $cart_item['quantity'];
}
}
// Continue if there is at least 2 units of "5 Reusable wet"
if( $five_rw_count < 2 ) return; // Exit
// 2nd Loop: set the discounted price for cart items with "5 Reusable wet"
foreach ( $cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_attribute( $product_attr ) == $term_slug ){
$cart_item['data']->set_price($discounted_price); // Set the discounted price
}
}
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作
2)优惠券方式 (由于许多逻辑原因不是很方便):
在这里您必须设置优惠券代码。此优惠券设置应如下所示:
您将在“产品”字段中设置所有相关产品变体。
完成后,您将在以下代码中设置优惠券名称(小写):
add_action( 'woocommerce_before_calculate_totals', 'conditionally_auto_add_coupon', 30, 1 );
function conditionally_auto_add_coupon( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') ) return; // Exit
// HERE your product attribute slug for "Reusable wet" (without "pa_")
$product_attr = 'reusable-wet';
// HERE set the corresponding (value) term NAME for "5 Reusable wet"
$term_slug = '5 reusable wet';
// HERE set the coupon code (in lowercase)
$coupon_code = 'multiplefive';
$five_rw_count = 0; // Initializing variable
// 1st Loop: count cart items with "5 Reusable wet"
foreach ( $cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_attribute( $product_attr ) == $term_slug ){
$five_rw_count++; // Increasing count
}
}
// Apply the coupon if there is at least 2 units of "5 Reusable wet"
if ( ! $cart->has_discount( $coupon_code ) && $five_rw_count >= 2 ) {
$cart->add_discount( $coupon_code );
}
// Remove the coupon if there is at less than 2 units of "5 Reusable wet"
elseif ( $cart->has_discount( $coupon_code ) && $five_rw_count < 2 ) {
$cart->remove_coupon( $coupon_code );
}
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作
推荐阅读
- scheme - 如果语句如何调试方案
- arrays - 如何将字符转换为整数以便将它们存储在 C 中的数组中
- c# - 使用 C# 在 Electron 应用程序上自动执行 UI 操作?
- javascript - 6 次尝试后获取 POST 未“写入”
- oauth - OAuth 客户端 ID 和猜测
- python - csv.writer 将每一行写入单独的单元格/列
- racket - 用于查找列表中与谓词匹配的第一个元素的球拍函数
- sql-server - 当值出现多次时,通过索引另一列中的值来从列创建有效地址
- r - 如何删除 0 个或多个标记(单词),它们可能会按顺序出现或中间有数据出现?
- r - 如何在 R 中使用 ghql 使用 NTLM 身份验证