【问题标题】:Custom Fee based on product dimensions and categories基于产品尺寸和类别的定制费用
【发布时间】:2017-01-14 21:12:57
【问题描述】:

我的 Woocommerce 网站处于独特的境地。

我需要添加包装费,这有点类似于手续费。

不幸的是,这并不是为每个订单添加 5.00 美元的手续费那么简单。

由于我根据尺寸(宽度 x 高度)销售木制品,因此包装费基于物品的总面积。它们也可能因物品的类别而异。

我进行了大量研究,但找不到处理这种情况的插件。

为了增加复杂性,需要创建一个完整的表。例如,如果某一类别的商品总面积在 1-10 之间,则包装费将为 10 美元。如果总平方英尺在 11-20 之间,那么它将是 20 美元

我该怎么做才能做到这一点?

谢谢

【问题讨论】:

  • 请查看stackoverflow.com/help/on-topic。如前所述,这个问题不适合stackoverflow的问答格式。
  • 这似乎是您可能想聘请开发人员为您创建的东西。

标签: php wordpress woocommerce cart


【解决方案1】:

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

使用 woocommerce_cart_calculate_fees 挂钩中的 add_fee() 方法,这是可能的并且简单。这是一个简单产品的简单使用示例,有 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;

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

}

您必须使用每个类别的相关更改计算来设置自己的类别。您将获得一个非详细的一般输出费用,计算时考虑到购物车中的每件商品。

代码进入您的活动子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。

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

相关答案:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多