根据 Woocommerce 中的国家/地区添加累进附加费
Add a progressive surcharge based on countries in Woocommerce
我可以使用 if 语句吗?如果可以,如何使用?基本上我希望它向 SE 国家和所有其他国家添加 27 作为附加费。
这里是原代码
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
?
是的,您可以添加许多条件以获得不同的费用金额,但由于您的国家/地区在一个数组中,您将使用 in_array()
php 函数而不是 ==
。
使用IF
/ELSE
语句时有shorthand方式:
正常方式:
if ( 'SE' == $shipping_country )
$fee = 27; // For Sweden
else
$fee = 3; // For others
shorthand方式(相同):
// The fee cost will be 27 for Sweden and 3 for other allowed countries
$fee = 'SE' == $shipping_country ? 27 : 3;
您的代码有点过时,所以这里是一个包含您的条件的重新访问版本:
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 );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。
我可以使用 if 语句吗?如果可以,如何使用?基本上我希望它向 SE 国家和所有其他国家添加 27 作为附加费。
这里是原代码
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
?
是的,您可以添加许多条件以获得不同的费用金额,但由于您的国家/地区在一个数组中,您将使用 in_array()
php 函数而不是 ==
。
使用IF
/ELSE
语句时有shorthand方式:
正常方式:
if ( 'SE' == $shipping_country ) $fee = 27; // For Sweden else $fee = 3; // For others
shorthand方式(相同):
// The fee cost will be 27 for Sweden and 3 for other allowed countries $fee = 'SE' == $shipping_country ? 27 : 3;
您的代码有点过时,所以这里是一个包含您的条件的重新访问版本:
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 );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。