根据产品尺寸和类别的自定义费用 [英] Custom Fee based on product dimensions and categories

查看:79
本文介绍了根据产品尺寸和类别的自定义费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Woocommerce网站上处在一个特殊的情况。



我需要增加包装费,这有点像手续费。

p>

不幸的是,这并不像向每笔订单增加$ 5.00的手续费一样简单。



由于我销售的木材产品在其尺寸(宽x高)上,包装费基于商品的总平方英尺数。



我已经进行了大量研究,但找不到适合这种情况的插件。





要增加复杂性,需要创建一个完整的表。例如,如果一个类别的商品的总平方英尺在1-10之间,则包装费为 $ 10 。如果总平方英尺为11-20,则为 $ 20



我该怎么做?



谢谢

解决方案


已更新:添加了WooCommerce 3+兼容性


使用 add_fee() 方法, woocommerce_cart_calculate_fees 挂钩。这是一个简单商品的简单用法示例,具有2个类别,并且基于购物车中每个商品的商品尺寸测量结果。


以下是示例代码(您需要根据自己的情况进行自定义):

  add_action('woocommerce_cart_calculate_fees','custom_applied_fee',10,1); 
function custom_applied_fee($ cart_object){

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

//在此处设置您的类别(可以是ID,子词或名称……或类似的数组)
$ category1 =‘普通’;
$ category2 =胶合板;

//变量初始化
$ fee = 0;
$ coef = 1;

//遍历每个购物车项目
foreach($ cart_object-> get_cart()as $ cart_item){
$ product_id = version_compare(WC_VERSION,'3.0',' <'))? $ cart_item [’data’]-> id:$ cart_item [’data’]-> get_id();
$ product = $ cart_item [’data’]; //获取产品对象

//获取产品的尺寸
$ height = $ product-> get_height();
$ width = $ product-> get_width();
// $ length = $ product-> get_length();

//初始化变量(在循环中)
$ cat1 = false; $ cat2 = false;

//检测产品类别并定义足以改变价格的类别(例如)
//在此处为每个类别设置修改计算规则…
if(has_term( $ category1,'product_cat',$ cart_item ['product_id']))
$ coef = 1.15;
if(has_term($ category2,'product_cat',$ cart_item ['product_id']))
$ coef = 1.3;

// ##计算##(在此处进行条件计算)
$ dimention = $ height * $ with;
if($ dimention< = 10){
$ fee + = 10 * $ coef;
} elseif($ dimention> 10&& $ dimention&=; = 20){
$ fee + = 20 * $ coef;
} elseif($ dimention> 20){
$ fee + = 30 * $ coef;
}
}

//在此处设置显示的费用文字
$ fee_text = __('包装费','woocommerce');

//如果($ fee> 0)
WC()-> cart-> add_fee($ fee_text,$ fee,false);
//注意:add_fee()方法中的最后一个参数与是否对折扣应用税项有关(对或错)

}



您将必须设置自己的类别以及每个类别的相关更改计算。考虑到购物车中的每个物品,您将获得不明的一般输出费用。


代码会放入您的function.php文件中活动子主题(或主题)。还是在任何插件php文件中。


该代码已经过测试并且功能齐全。


相关答案:



I'm in a unique situation with my Woocommerce site.

I need to add a packaging fee, which will be somewhat similar to a handling fee.

Unfortunately, it's not as simple as adding a $5.00 handling fee to every order.

Since I sell wood products based on their measurements (width x height), the packaging fees are based on the total square footage for items. And they can differ depending on the category of the items, as well.

I've done a ton of research and I can't find a plugin to handle this situation.

To add to the complexity, there's a whole table that would need to be created. For example, if items from one category have a total square footage between 1-10, then the packaging fee will be $10. If total sq ftg is between 11-20, then it'll be $20.

How can I do to achieve that?

Thanks

解决方案

Updated: Added WooCommerce 3+ compatibility

This is possible and easy with the add_fee() method in the woocommerce_cart_calculate_fees hook. Here is a simple usage example for simple products, with 2 categories and based on the product dimensions measurement calculations for each item of the cart. Its also possible for other product types.

Here is the example code (That you will need to customize for your own case):

add_action( 'woocommerce_cart_calculate_fees','custom_applied_fee', 10, 1 );
function custom_applied_fee( $cart_object ) {

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

    // Set HERE your categories (can be an ID, a slug or the name… or an array of this)
    $category1 = 'plain';
    $category2 = 'plywood';

    // variables initialisation
    $fee = 0;
    $coef = 1;

    // Iterating through each cart item
    foreach( $cart_object->get_cart() as $cart_item ){
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        $product = $cart_item['data']; // Get the product object

        // Get the dimentions of the product
        $height = $product->get_height();
        $width = $product->get_width();
        // $length = $product->get_length();
        
        // Initialising variables (in the loop)
        $cat1 = false; $cat2 = false;

        // Detecting the product category and defining the category coeficient to change price (for example)
        // Set here for each category the modification calculation rules…
        if( has_term( $category1, 'product_cat', $cart_item['product_id']) )
            $coef = 1.15;
        if( has_term( $category2, 'product_cat', $cart_item['product_id']) )
            $coef = 1.3;

        // ## CALCULATIONS ## (Make here your conditional calculations)
        $dimention = $height * $with;
        if($dimention <= 10){
            $fee += 10 * $coef;
        } elseif($dimention > 10 && $dimention <= 20){
            $fee += 20 * $coef;
        } elseif($dimention > 20){
            $fee += 30 * $coef;
        }
    }

    // Set here the displayed fee text
    $fee_text = __( 'Packaging fee', 'woocommerce' );

    // Adding the fee
    if ( $fee > 0 )
        WC()->cart->add_fee( $fee_text, $fee, false );
        // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)

}

You will have to set your own categories with the related changes calculations for each category. You will get a non detailed general output fee calculated taking into account each item in the cart.

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

The code is tested and fully functional.

Related Answers:

这篇关于根据产品尺寸和类别的自定义费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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