【问题标题】:Wordpress: Add extra fee in cartWordpress:在购物车中添加额外费用
【发布时间】:2015-03-17 13:45:15
【问题描述】:

我在 Woocommerce (wordpress) 中创建了一个网上商店。这是一家葡萄酒商店,我需要增加额外费用:每瓶 0.08 欧元(产品)。

我已经找到并对其进行了调整,但我无法将产品(瓶子)的数量乘以 0.08 欧元。 在购物车中,我确实得到了一条额外的线,但值为 0。

谁能解释我做错了什么?

function get_cart_contents_count() {
     return apply_filters( 'woocommerce_cart_contents_count', $this->cart_contents_count );
}


function woo_add_cart_fee() {

  global $woocommerce;

  $woocommerce->cart->add_fee( __('Custom', 'woocommerce'), $get_cart_contents_count * 0.08 );

}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

【问题讨论】:

  • $get_cart_contents_count 是一个未定义的变量,PHP 会礼貌地将其视为0。也许您想要get_cart_contents_count()(即:函数调用)。
  • 谢谢马克,但是当我尝试这个时,页面没有加载。

标签: php wordpress woocommerce


【解决方案1】:

在您的 functions.php 中尝试此代码,但它将应用于所有购物车

// Hook before adding fees 
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');

function add_custom_fees( WC_Cart $cart ){
    $fees = 0.08;
    $cart->add_fee( 'Handling fee', $fees);
}

编辑: 要将它与每个产品相乘,请执行以下操作

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( WC_Cart $cart ){
    $fees = 0;

    foreach( $cart->get_cart() as $item ){
       $fees += $item[ 'quantity' ] * 0.08; 
    }

    if( $fees != 0 ){
        $cart->add_fee( 'Handling fee', $fees);
    }
}

【讨论】:

  • 谢谢 Omer,但我如何将它与购物车中的产品数量相乘?
  • 是的,我们越来越近了。此代码乘以每个产品的费用,但我需要将其乘以产品数量(数量/件)。现在我的购物车中有 2 种不同的产品,它给了我 0.16 欧元的费用,这已经很划算了。但是每个产品我有 5 件,所以我应该收取 0.80 欧元的费用。这个可以吗?
  • 在 foreach 循环中,$item[ 'quantity' ] 的倍数为 0,08,检查编辑
  • 是的!非常感谢奥马尔!
【解决方案2】:

您正在谈论向购物车添加附加费。您可以从Woocommerce Doc in here获取所有相关信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2011-12-19
    相关资源
    最近更新 更多