我已实施代码,除非使用优惠券代码,否则所有WooCommerce产品都将收取$ 1.50的手续费。但是,我只希望免除一张优惠券的手续费,而不是使用任何优惠券时。有什么想法吗?下面是我当前的代码,唯一不需支付手续费的优惠券是QFPZ8KSS(如果需要,优惠券的邮编ID为4432)。
add_action( 'woocommerce_cart_calculate_fees','conditional_handling_fee' );
function conditional_handling_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get the applied coupons + the count (in cart)
$applied_coupons_arr = WC()->cart->get_applied_coupons();
$applied_coupons_count = count($applied_coupons_arr);
$fee = 1.50;
if( 0 == $applied_coupons_count )
WC()->cart->add_fee( ''.$applied_coupons_count, $fee, true, 'standard' );
}
答案 0 :(得分:0)
您只需要将订单中使用的优惠券与免费送货的优惠券进行比较即可。我认为下面的方法应该有效,请记住我还没有测试过。希望它会有所帮助。
<?php
add_action( 'woocommerce_cart_calculate_fees','conditional_handling_fee' );
function conditional_handling_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get the applied coupons + the count (in cart)
$applied_coupons_arr = WC()->cart->get_applied_coupons();
$fee = 1.50;
$applied_coupon_ids;
// Coupons used in the order loop
foreach( $applied_coupons_arr as $coupon ){
// Get Coupon ID
$coupon_post_obj = get_page_by_title($coupon, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
// Store in array
array_push($applied_coupon_ids, $coupon_id);
};
// Check if your free shipping coupon is used
if(! in_array('COUPON_ID_HERE', $applied_coupon_ids)){
WC()->cart->add_fee( ''.$applied_coupons_count, $fee, true, 'standard' );
};
?>