php - 在 WooCommerce 中将基于 ACF 字段的折扣应用于购物车总数
问题描述
我正在尝试以编程方式为我商店的订单创建折扣。我的用户有一个可变的折扣率,例如 10%。我想在结帐前将他们的折扣应用于订单,但是,商店中的某些商品不适用折扣。
这些项目有一个切换设置,因此我可以检查哪些产品允许或不允许应用折扣。
目前,我正在遍历订单以检查哪些商品适用并使用'fixed_cart'
优惠券添加折扣。
这是可行的,但是,在管理员中,优惠券适用于所有订单项,即使是应该跳过的项目。当需要退款时,无法确定向客户退款多少。
检查购物车物品
$tradeDiscountTotal = 0;
foreach( WC()->cart->get_cart() as $cart_item ) {
if(!get_field('trade_discount_exempt', $cart_item['product_id'])) {
$tradeDiscountTotal += $cart_item['line_subtotal'];
}
}
应用此订单的总折扣
$coupon->set_discount_type('fixed_cart');
$coupon->set_amount($tradeDiscountTotal);
return $coupon;
如何为每个订单创建定制折扣并确保打折的产品在管理区域中正确显示?
解决方案
更新
一种解决方案是手动创建具有百分比折扣的优惠券,例如 10%。然后我们在基础上添加如下代码couponcode
- 第一部分确保对符合条件的产品应用 0 的折扣
- 第二部分保证优惠券自动加入购物车
所以我们得到:
- 折扣金额
trade_discount_exempt
= 0
function filter_woocommerce_coupon_get_discount_amount( $discount, $price_to_discount , $cart_item, $single, $coupon ) {
// Product ID in cart
$product_id = $cart_item['product_id'];
// If 'trade_discount_exempt'
if ( get_field( 'trade_discount_exempt', $product_id ) ) {
$discount = 0;
}
return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
trade_discount_exempt
如果不在购物车中,则以编程方式应用优惠券
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Only cart
if( ! is_cart() )
return;
// Coupon code
$coupon_code = 'testcoupon';
// Format
$coupon_code = wc_format_coupon_code( $coupon_code );
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Product ID in cart
$product_id = $cart_item['product_id'];
// NOT 'trade_discount_exempt'
if ( ! get_field( 'trade_discount_exempt', $product_id ) ) {
// Applied coupons
$applied_coupons = $cart->get_applied_coupons();
// Is applied
$is_applied = in_array( $coupon_code, $applied_coupons );
// NOT applied
if ( ! $is_applied ) {
// Apply
$cart->apply_coupon( $coupon_code );
break;
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
可选:这里有 2 个其他可能的解决方案:
1.使用woocommerce_calculated_total
过滤钩,在购物车上应用全球折扣
function filter_woocommerce_calculated_total( $total, $cart ) {
// Discount (percentage)
$percentage = 10; // 10 %
// Set variable, used in loop
$new_total = 0;
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Product ID in cart
$product_id = $cart_item['product_id'];
// NOT 'trade_discount_exempt'
if( ! get_field( 'trade_discount_exempt', $product_id ) ) {
// Product price
$price = $cart_item['data']->get_price();
// Caclulate new price
$new_price = $price * ( 1 - ( $percentage / 100 ) );
// Add new price to new total
$new_total += $new_price;
}
}
// New total greater than
if ( $new_total > 0 ) {
$total = $new_total;
}
return $total;
}
add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 10, 2 );
2.使用woocommerce_before_calculate_totals
action hook,给予产品价格折扣
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Discount (percentage)
$percentage = 10; // 10 %
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Product ID in cart
$product_id = $cart_item['product_id'];
// NOT 'trade_discount_exempt'
if( ! get_field( 'trade_discount_exempt', $product_id ) ) {
// Product price
$price = $cart_item['data']->get_price();
// Caclulate new price
$new_price = $price * ( 1 - ( $percentage / 100 ) );
// Set price
$cart_item['data']->set_price( $new_price );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
推荐阅读
- c - 如何像数组一样处理整数作为输入输出参数?
- python - 将 tf.distribute 策略与 tf.keras 模型子类化一起使用
- mysql - ASP.NET Core MVC 应用程序无法在 MariaDB 上应用 Entity Framework Core Migration。指定的密钥太长
- javascript - 如何加载 App 类的 componentWillUnmount
- vue.js - 获取 vue-cli-service test:unit 以从不同的文件(不是 .env.test)加载环境变量
- javascript - 使用 JS 禁用选项
- laravel - Laravel 雄辩 - 与不同表的一对一关系
- bash - shell 脚本 - 从字母数字列表中获取最大值
- selenium - Webdriver.io - 无法让 firefox 运行
- python - matplotlib savefig 格式参数不起作用