编辑wooccommerce附加费代码段



我想知道如何修改代码,使所有交易都有百分比附加费,并排除法国?

* Add a standard $ value surcharge to all transactions in cart / checkout
*/
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 = 1.00;
if ( in_array( WC()->customer->get_shipping_country(), $county ) ) : 
$woocommerce->cart->add_fee( 'Surcharge', $fee, true, 'standard' );  
endif;
}

我在wooccommerce网站上找到了代码

感谢您的回复。

我通过重新编写代码得到了我想要的东西,它给出了以下内容:

//Ajouter un supplément à votre paiement en fonction du pays de livraison exclu france
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' ); 
function woocommerce_custom_surcharge() { 
global $woocommerce; 
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
$county = ['FR']; //france
// change the $fee to set the surcharge to a value to suit

$percentage = 0.025;
if ( !in_array( WC()->customer->get_shipping_country(), $county ) ){
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
$woocommerce->cart->add_fee( 'Frais de transaction', $surcharge, true, '' );
}
}

剩下的就是为那些不想对自己的国家提出指控的人改变以下国家!

这样的东西至少是基于我阅读代码的方式。无法真正测试它,但它看起来很简单。

* Add a standard $ value surcharge to all transactions in cart / checkout
*/
add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge' ); 
function wc_add_surcharge() { 
global $woocommerce; 
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
$county = ['FR']; //france
// change the $fee to set the surcharge to a value to suit
$fee = 1.00;
if ( !in_array( WC()->customer->get_shipping_country(), $county ) ){  //add the ! to exclude only countries in the list
$woocommerce->cart->add_fee( 'Surcharge', $fee, true, 'standard' );  
}
}

超载费用是固定的,理想情况下应该有价格层

示例从01欧元到55欧元=0.35欧元56至150欧元=1.00欧元…

无论如何,该代码在我的网站上有效,不包括法国。

最新更新