当使用特定的促销代码时,我在WooCommerce商店的functions.php中使用以下代码在结账时添加费用
function conditional_custom_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE set your targeted coupon code
$coupon_code = 'ABC123' ;
// Check if our targeted coupon is applied
if( in_array( wc_format_coupon_code( $coupon_code ), $cart->get_applied_coupons() ) ){
$title = __('One-off fee', 'woocommerce'); // The fee title
$cost = 2.5; // The fee amount
// Adding the fee (not taxable)
$cart->add_fee( $title, $cost, false );
}
}
除了这个,我希望能够在其他促销代码上使用这个规则。作为一个PHP新手,我该如何更改此代码,以便能够使用代码ABC123
或XYZ789
来应用此2.5
费用?
如果您只想检查第二个优惠券代码,您可以修改if
语句:
if( in_array( wc_format_coupon_code( $coupon_code ), $cart->get_applied_coupons() ) ){
要检查两种不同的情况,请使用||
(或(逻辑运算符。
在本例中,我们要检查优惠券代码ABC123
或YYZ789
是否正在使用:
if( in_array( wc_format_coupon_code( "ABC123" ), $cart->get_applied_coupons() ) ||
in_array( wc_format_coupon_code( "XYZ789" ), $cart->get_applied_coupons() ) ){