【问题标题】:Add fee to woocommerce cart将费用添加到 woocommerce 购物车
【发布时间】:2015-12-02 12:18:14
【问题描述】:

当客户有特定的帐单国家/地区时,我想在我的购物车中添加费用。例如比利时 (BE)

我发现此代码默认添加费用。 任何人都可以帮助我使用 IF 公式或其他东西,以便仅在计费国家 = BE 时应用它?

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

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

$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );

}

【问题讨论】:

  • 您应该分享您尝试应用的代码。
  • 你是对的 :) 抱歉忘记密码了...

标签: wordpress woocommerce


【解决方案1】:

这就是你需要的。只需将 BE 添加到 $county 数组即可(我只是在下面的代码中为您完成了该操作)。

/**
 * Add a 1% surcharge to your cart / checkout based on delivery country
 * Taxes, shipping costs and order subtotal are all included in the surcharge amount
 *
 * Change $percentage to set the surcharge to a value to suit
 *
 * Add countries to array('BE'); to include more countries to surcharge
 * http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes for available alpha-2 country codes 
 *
 * Change in_array to !in_array to EXCLUDE the $countries array from surcharges
 *
 * Uses the WooCommerce fees API
 * Add to theme functions.php
 */
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

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

    $county     = array('BE');
    $percentage     = 0.01;

    if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
        $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
        $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
    endif;

}

来源:https://docs.woothemes.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/

【讨论】:

  • 谢谢埃里克!我之前也找到了该代码,但它对我不起作用。不知何故,当我从您的答案中复制它时,它确实有效。您能否进一步帮助我处理此代码?因为我还有一个问题。
  • 当然,您可以在 LinkedIn 上将您的问题发送给我
猜你喜欢
  • 1970-01-01
  • 2015-10-10
  • 2021-12-17
  • 2015-08-18
  • 2014-02-25
  • 2018-04-10
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多