我正在尝试以编程方式为商店中的订单创建折扣。我的用户有可变的折扣率,例如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;
如何为每个订单创建量身定制的折扣,并确保打折的产品在管理区域中正确显示?
答案 0 :(得分:1)
更新
一种解决方案是手动创建优惠券,并提供一定比例的折扣,例如10%。
然后,基于RuntimeException
所以我们得到:
mysql:
networks:
- main
image: library/mysql:5.7
container_name: 'mysql'
command: >
bash -c "
chmod 644 /etc/mysql/conf.d/*.cnf
&& /entrypoint.sh mysqld
"
ports:
- "3308:3308"
volumes:
- ./my_local_dir/var/lib/mysql:/var/lib/mysql
- ./my_local_dir/etc/mysql/conf.d/:/etc/mysql/conf.d/
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_DATABASE: 'some'
上的折扣金额= 0 couponcode
trade_discount_exempt
,则以编程方式应用优惠券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 );
可选:这是另外两种可能的解决方案:
1。。使用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。。使用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 );