Wordpress:在购物车中添加额外费用 [英] Wordpress: Add extra fee in cart

查看:36
本文介绍了Wordpress:在购物车中添加额外费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a webshop in Woocommerce (wordpress). It's a wine shop and I need to add an extra cost: 0,08 euro per bottle (product).

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

I've found this and adjusted it, but I cannot get the number of products (bottles) to multiply with 0.08 euro. In the cart I do get an extra line but the value is 0.

谁能解释一下我做错了什么?

Can anyone explain me what I'm doing wrong?

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' );

推荐答案

在你的functions.php 中试试这个代码,但它会应用于所有的购物车

Try this code in your functions.php but it will be applied on over all cart

// 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);
    }
}

这篇关于Wordpress:在购物车中添加额外费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆