【问题标题】:WooCommerce - Adding Extra fee to Cart based on total items weightWooCommerce - 根据总物品重量向购物车添加额外费用
【发布时间】:2016-08-15 19:06:14
【问题描述】:

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

这个费用应该这样计算:

$extra_charge = $total_cart_weight * 0.15;

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

谢谢

【问题讨论】:

  • 你测试过我的代码吗?比插件好得多,资源消耗低且轻量级。谢谢
  • 是的,我测试过。我不了解代码知识,所以我需要您的帮助:我的主题名称是“santa”,而购物车 wight 是“$woocommerce->cart->cart_contents_weight”,我该如何更改此功能才能正常工作?

标签: php wordpress woocommerce attributes cart


【解决方案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,具体取决于您的全球税务设置。

参考:

【讨论】:

    【解决方案2】:

    我相信您可以使用this 插件来创建额外收费类别。您可能需要编辑插件以使购物车费用的百分比占总重量费用的百分比,但这应该不难弄清楚。除非我知道您从哪里获得重量,否则我无法真正为您编辑插件。您只需编辑 wc-extra-fee-option.php 文件并更改第 133 行,以便 $extra_fee_option_cost 将乘以权重而不是 $total。您可以通过将权重放在一个类中,然后在第 133 行调用该类来做到这一点。或者,您可以将权重设为全局变量,并简单地使用该全局变量代替第 133 行的 $total。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 2020-10-18
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多