Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据商业中的国家/地区添加累进附加费_Php_Wordpress_Woocommerce_Cart_Fee - Fatal编程技术网

Php 根据商业中的国家/地区添加累进附加费

Php 根据商业中的国家/地区添加累进附加费,php,wordpress,woocommerce,cart,fee,Php,Wordpress,Woocommerce,Cart,Fee,我可以使用if语句吗?如果可以,如何使用?基本上,我想它添加27作为附加费东南国和所有其他国家3作为附加费 这是原始代码 add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge' ); function wc_add_surcharge() { global $woocommerce; if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $

我可以使用if语句吗?如果可以,如何使用?基本上,我想它添加27作为附加费东南国和所有其他国家3作为附加费

这是原始代码

add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge' ); 
function wc_add_surcharge() { 
global $woocommerce; 

if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
return;

$county = array('US');
// change the $fee to set the surcharge to a value to suit
$fee = 27.00;

if ( in_array( WC()->customer->get_shipping_country(), $county ) ) : 
    $woocommerce->cart->add_fee( 'Surcharge', $fee, true, 'standard' );  
endif;
}
我可否补充:

if ( !county = 'SE')
$fee = 3
if ( county = 'SE')
$fee 27

是的,您可以添加许多条件以获得不同的费用金额,但由于您所在的国家/地区位于一个数组中,您将使用php函数而不是
=

当使用
IF
/
ELSE
语句时,有一种速记方法:

  • 通常的方式是:

    if ( 'SE' == $shipping_country )
        $fee = 27; // For Sweden
    else
        $fee = 3; // For others
    
  • 速记方式(与此相同):

您的代码有点过时,因此这里有一个重新访问的版本,包括您的条件:

add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge', 10, 1 ); 
function wc_add_surcharge( $cart ) { 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

    $shipping_country = WC()->customer->get_shipping_country();
    $subtotal = WC()->cart->subtotal;

    // Defined fee amount based on countries
    $fee = 'SE' == $shipping_country ? 27 : 3;

    // Minimum cart subtotal
    $minimum = 300; 

    if ( $subtotal < $minimum ) { 
        $cart->add_fee( 'Surcharge', $fee, true );  
    }
}
add_action('woocommerce_cart_计算费用','wc_add_附加费',10,1);
功能wc_添加附加费($cart){
if(定义了('DOING'uajax'))
返回;
$shipping_country=WC()->customer->get_shipping_country();
$subtotal=WC()->cart->subtotal;
//根据国家/地区确定的费用金额
$fee='SE'=$shipping\u country?27:3;
//最低购物车小计
最低$300;
如果($小计<$最小值){
$cart->add_fee(‘附加费’,$fee,true);
}
}

代码进入活动子主题(或活动主题)的function.php文件。测试和工作。

=
是一项任务,比较是
=
完美!要仅为订单购物车->总计<$minimum){if(在_数组中(WC()->customer->get_shipping_country(),$countries)&&isset($fee)){$cart->add_fee('fee',$fee,true);}}}谢谢,但是,没有使用最低顺序:S另外,我认为$subtotal行中缺少分号?我在两个不同的网站上尝试过它(没有任何关联,甚至没有主题/插件)。没用。无论金额多少,它都会增加附加费。哦,在本例中,它只会将附加费添加到阵列中,即US/SE。所以所有其他国家都没有增加任何东西。再次感谢你,我真的很感激。但即使在300:s以上,它仍在增加附加费
add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge', 10, 1 ); 
function wc_add_surcharge( $cart ) { 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

    $shipping_country = WC()->customer->get_shipping_country();
    $subtotal = WC()->cart->subtotal;

    // Defined fee amount based on countries
    $fee = 'SE' == $shipping_country ? 27 : 3;

    // Minimum cart subtotal
    $minimum = 300; 

    if ( $subtotal < $minimum ) { 
        $cart->add_fee( 'Surcharge', $fee, true );  
    }
}