首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WooCommerce快车费

WooCommerce快车费
EN

Stack Overflow用户
提问于 2017-09-06 20:41:05
回答 2查看 322关注 0票数 2

如果所有定义类别的产品总数为1,我想在某些类别中添加一项费用,如果数量为2-9,则另加费用。(即)+ $10的产品,如果他们只订购1,+$5每产品,如果他们订购2-9项)。

我从这个基础开始:Custom Fee based on product dimensions and categories

我有一些零钱,但我不能让它起作用,我被卡住了。

这是我的代码:

代码语言:javascript
运行
复制
add_action( 'woocommerce_cart_calculate_fees','custom_applied_fee');
function custom_applied_fee() {

    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;

    // Iterating through each cart item
    foreach(WC()->cart->get_cart() as $cart_item){
        // Get the product object
        $product = new WC_Product( $cart_item['product_id'] );
        $quantiy = $value['quantity']; //get quantity from cart

        // Initialising variables (in the loop)
        $cat1 = false; $cat2 = false;

        // ## CALCULATIONS ## (Make here your conditional calculations)
        $quantity = GET TOTAL QUANTITY
        if($quantity <= 1){
            $fee += 10 * $quanity;
        } elseif($quantity > 1 && $dimention <= 9){
            $fee += 5 * $quanity;
        } elseif($dimention > 10){
            $fee += 1 * $quanity;
        }
    }

    // Adding the fee
    if ( $fee != 0 )
        WC()->cart->add_fee( $fee_text, $fee, true );
}

我做错什么了?我该怎么做呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-07 00:11:56

更新2

你的需求不太清楚,所以我已经尽力了。据我所知,你想补充:

  • 一个固定的初始费用,当有一个购物车项目从定义的产品类别(Ies)。
  • 当有(从2到9)定义产品类别(Ies)的购物车项目时,根据购物车项目总数计算的费用。

以下是相应的代码:

代码语言:javascript
运行
复制
add_action( 'woocommerce_cart_calculate_fees','conditional_custom_multiple_fees', 10, 1 );
function conditional_custom_multiple_fees( $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)
    $product_categories = array('clothing', 'disc');
    $cat_qty = 0;
    $cart_total_qty = $cart_object->get_cart_contents_count();

    // Iterating through each cart item
    foreach( $cart_object->get_cart() as $cart_item ){
        $product = $cart_item['data']; // The product object
        $item_qty = $cart_item['quantity']; // Item quantity

        // Counting quanties for the product categories
        if( has_term( $product_categories, 'product_cat', $cart_item['data']->get_id() ) )
            $cat_qty += $cart_item['quantity'];
    }

    ##  --  --  --  --  CALCULATIONS AND CONDITIONAL FEES  --  --  --  --  ##

    // 1. A fee for the product defined categories for 1 cart item only
    if($cat_qty == 1) // updated HERE
    {
        $fee_amount = 10;
        $cart_object->add_fee( __( "Fee (1)" ), $fee_amount, true );
    }

    // 2. Or a calculated fee for the defined product categories (qty from 2 to 9)
    if( $cat_qty >= 2 && $cat_qty <= 9 )
    {
        $amount_by_item = 5; // amount by item (updated)
        $calculated_fee = $amount_by_item * $cat_qty;  // calculation (updated)
        $cart_object->add_fee( __( "Fee (2 to 9)" ), $calculated_fee, true );
    }
}

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

这段代码是经过测试和工作的。

票数 2
EN

Stack Overflow用户

发布于 2017-09-07 14:27:07

谢谢LoicTheAztec,我想只要做几个小编辑,你的代码就能工作.

代码语言:javascript
运行
复制
add_action( 'woocommerce_cart_calculate_fees','conditional_custom_multiple_fees', 10, 1 );
function conditional_custom_multiple_fees( $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)
$product_categories = array('clothing', 'disc');
$cat_qty = 0;
$cart_total_qty = $cart_object->get_cart_contents_count();

// Iterating through each cart item
foreach( $cart_object->get_cart() as $cart_item ){
    $product = $cart_item['data']; // The product object
    $item_qty = $cart_item['quantity']; // Item quantity

    // Counting quanties for the product categories
    if( has_term( $product_categories, 'product_cat', $cart_item['data']->get_id() ) )
        $cat_qty += $cart_item['quantity'];
}

##  --  --  --  --  CALCULATIONS AND CONDITIONAL FEES  --  --  --  --  ##

// 1. The 1st fee for the product defined categories (qty = 1)
if($cat_qty = 1)
{
    $fee_amount = 10;
    $cart_object->add_fee( __( "Fee (1)" ), $fee_amount, true );
}

// 2. The Separate  additional fee for the defined product categories (qty from 2 to 9)
if( $cat_qty >= 2 && $cat_qty <= 9 )
{
    $amount_by_item = 5; // amount by item
    $calculated_fee = $cat_qty * $amount_by_item;  // calculation
    $cart_object->add_fee( __( "Fee (2 to 9)" ), $calculated_fee, true );
}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46084005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档