在Woocommerce中添加基于购物车物品高度的费用 [英] Add a fee based on cart items height in Woocommerce

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

问题描述

我正在尝试找到一种功能,如果其中产品的高度超过2.9厘米,该功能会自动向购物车添加费用.

I'm trying to find a function that automatically adds a fee to the cart if the height of the products in it is over 2,9 cm.

我正在将Woocommerce用于我们简单的非营利漫画书店.我们将重量运输作为瑞典的标准,如果重量超过3厘米,则收取巨额费用.

I'm using Woocommerce for our simple non-profit comic bookstore. We use weight based shipping as a standard in Sweden, with a bulky fee if something is 3 cm or over.

我尝试修改 LoicTheAztec的答案关于基于购物车总重量的费用,但是我真的不知道自己在做什么,因为保存代码后我得到了空白页.

I've tried modifying this answer of LoicTheAztec regarding fees based on total cart weight, but I really don't know what I'm doing as I am getting a blank page after saving the code.

我要修改的代码是这样的:

The code I'm trying to modify is this one:

add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Convert cart weight in grams
    $cart_weight = $cart->get_cart_contents_weight() * 1000;
    $fee = 50; // Starting Fee below 500g

    // Above 500g we add $10 to the initial fee by steps of 1000g
    if( $cart_weight > 1500 ){
        for( $i = 1500; $i < $cart_weight; $i += 1000 ){
            $fee += 10;
        }
    }
    // Setting the calculated fee based on weight
    $cart->add_fee( __( 'Weight shipping fee' ), $fee, false );
}

我对php的经验不仅仅在于能够将动作粘贴到我的子主题的function.php中.

My experience with php is not more than being able to paste actions into my child-theme's functions.php.

感谢您能提供的任何帮助.

I appreciate any help I can get.

推荐答案

如果任何购物车商品的高度不超过3厘米,以下代码将收取特定费用(Woocommerce中的尺寸单位设置必须位于 cm ):

The following code will add a specific fee if any cart item has a height up to 3 cm (dimensions unit setting in Woocommerce need to be in cm):

add_action( 'woocommerce_cart_calculate_fees', 'shipping_height_fee', 10, 1 );
function shipping_height_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Your settings (here below)
    $height = 3; // The defined height in cm (equal or over)
    $fee    = 50; // The fee amount
    $found  = false; // Initializing

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        if( $cart_item['data']->get_height() >= $height ) {
            $found = true;
            break; // Stop the loop
        }
    }
    // Add the fee
    if( $found ) {
        $cart->add_fee( __( 'Height shipping fee' ), $fee, false );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

添加项:基于购物车商品总高度的代码:

Addition: Code based on cart items total height:

add_action( 'woocommerce_cart_calculate_fees', 'shipping_height_fee', 10, 1 );
function shipping_height_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Your settings (here below)
    $target_height = 3; // The defined height in cm (equal or over)
    $total_height  = 0; // Initializing
    $fee           = 50; // The fee amount

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        $total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
    }
    // Add the fee
    if( $total_height >= $target_height ) {
        $cart->add_fee( __( 'Height shipping fee' ), $fee, false );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

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

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