问题描述
我有一个代码,可以为客户提供Woocommerce中特定付款方式的折扣。百分比是使用ACF字段设置的,也可以通过将产品ID设置到ACF中继器中来排除特定产品的折扣。而且,如果购物车中已经有优惠券,则不适用每次付款的折扣。
问题是,我完成的所有测试均正常运行,并且对大多数客户都有效,但是由于某种原因,每天至少进行2到3笔购买交易,因此无法享受折扣,因此我无法了解原因。我的代码有问题吗?
我从[here] [1]处获得了代码,并进行了一些更改,不确定是否出错。
代码:
function add_deposit_discount( $cart ){
if ( is_admin() && ! defined('DOING_AJAX') )
return;
if ( did_action('woocommerce_cart_calculate_fees') >= 2 )
return;
$discount = 0; // Initialising
// HERE Define your targeted shipping method ID
$payment_method = 'bacs';
// The percent to apply (get value from ACF)
$percent = get_field('desconto_deposito',16243);
$cart_total = $cart->subtotal_ex_tax;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
//Array to receive ACF repeater values for exception
$product_ids = array();
if( have_rows('dont_apply_discount',16243) ):
// Loop through rows.
while( have_rows('dont_apply_discount',16243) ) : the_row();
$product_row = get_sub_field('produto');
//Push product IDs into array
array_push($product_ids,$product_row);
// End loop.
endwhile;
endif;
//Check if exceptions are in the cart and set variable to true or false
if( 0 < matched_cart_items($product_ids) ){
$isincart = true;
} else {
$isincart = false;
}
//Check if Payment Method is 'bacs' AND there is no exception product in the cart AND there are no coupons already applied
if( $payment_method == $chosen_payment_method && !$isincart && !WC()->cart->get_coupons()){
$label_text = __( "discount for Bank Deposit (".$percent."%)" );
// Calculation
$discount = number_format(($cart_total / 100) * $percent,2);
// Add the discount
$cart->add_fee( $label_text,-$discount,false );
}
}
//Update checkout if payment method is changed
add_action( 'woocommerce_review_order_before_payment','refresh_payment_methods' );
function refresh_payment_methods(){
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change','input[name^="payment_method"]',function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?PHP
}
[1]: https://stackoverflow.com/questions/47956053/add-a-discount-for-specific-selected-payment-method-in-woocommerce
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)