WooCommerce - 根据项目总重量向购物车添加额外费用

时间:2016-08-15 19:06:15

标签: php wordpress woocommerce attributes cart

我需要在产品总重量上添加费用并将其显示在购物车中 我的意思是在购物车中,当添加一个项目时,我会设置一个额外的费用。

此费用应按以下方式计算:

df.bfill()

如果可能,我该如何实现?

由于

1 个答案:

答案 0 :(得分:1)

您可以轻松地将此功能挂钩到 woocommerce_cart_calculate_fees 钩子,这样:

function weight_add_cart_fee() {

    // Set here your percentage
    $percentage = 0.15;

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

    // Get weight of all items in the cart
    $cart_weight = WC()->cart->get_cart_contents_weight();

    // calculate the fee amount
    $fee = $cart_weight * $percentage;

    // If weight amount is not null, adds the fee calcualtion to cart
    if ( !empty( $cart_weight ) ) { 
        WC()->cart->add_fee( __('Extra charge (weight): ', 'your_theme_slug'), $fee, false );
    }
}
add_action( 'woocommerce_cart_calculate_fees','weight_add_cart_fee' );

此代码已经过测试并正常运行。它继续活动子主题或主题的function.php文件。

  

有关税务选项:请参阅add_fee() method tax options,具体取决于您的全球税务设置。

<强>参考:

相关问题