【问题标题】:WooCommerce cart discount: one item free based on item quantityWooCommerce 购物车折扣:根据商品数量免费赠送一件商品
【发布时间】:2020-11-24 04:57:14
【问题描述】:

我正在尝试对购物车实施自定义折扣规则。基本上有 WooCommerce,该网站正在销售 T 恤。目前有一项促销活动,如果您购买 3 件 T 恤,您只需支付 2 件,而价格最低的一件则免费。

我使用钩子 woocommerce_cart_calculate_fees 创建了一个自定义函数,到目前为止它正在工作。

这是我的代码:

add_action( 'woocommerce_cart_calculate_fees', 'iom_add_custom_discount', 10, 1 );
function iom_add_custom_discount( $wc_cart ){
    $discount = 0;
    $product_ids = array();
    $item_prices = array();
    $in_cart = true;

    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_product = $cart_item['data'];
        if ( has_term( 'detski-bodita', 'product_cat', $cart_product->get_id() ) ) {
            $in_cart = true;
        }else {
            $product_ids[] = $cart_product->get_id();
            $item_prices[$cart_product->get_id()] = $cart_product->get_price();
        }

    }

    if( $in_cart ) {
        $count_ids = count($product_ids);
        asort( $item_prices ); //Sort the prices from lowest to highest
        
        $count = 0;
        if( $count_ids == 3 ) { 
           foreach( $item_prices as $id => $price ) {
                if( $count >= 1 ) {
                    break;
                }
                //$product = wc_get_product( $id );
                //$price = $product->get_price();
                $discount -= ($price * 100) / 100;
                $count++;
           }
       }

    } 

    if( $discount != 0 ){
        $wc_cart->add_fee( 'Отстъпка', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

显示并应用折扣。但是,它似乎只有当我在购物车中有 3 种不同的产品时才有效。如果我有 1 个数量为 2 的产品和 1 个数量为 1 的产品,则它不起作用。

如何调整函数以使其适用于项目数量计数?

这是购物车页面的截图:


编辑:

折扣中的产品类别说明:

只有当购物车中有 3 件属于同一类别的商品时,才能享受折扣。

例如,类别是 T 恤和连帽衫。如果我的购物车中有 3 件 T 恤,则应享受折扣。如果我有 2 件 T 恤和 1 件连帽衫,则不应享受折扣。

【问题讨论】:

  • 我不知道。感谢您编辑帖子。 :)
  • 是的,这就是我需要帮助的原因。 :) 我更偏向于“前端”,但我对 php 的使用仍然不是很好。

标签: php wordpress woocommerce hook-woocommerce discount


【解决方案1】:

经过一些测试和工作后,我设法使其按描述工作。代码如下:

add_action( 'woocommerce_cart_calculate_fees', 'iom_add_custom_discount', 10, 1 );
function iom_add_custom_discount( $wc_cart ){
    $discount = 0;
    $product_ids = array();
    $item_prices = array();
    $in_cart = false;

    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_product = $cart_item['data'];
        // var_dump($cart_product->get_parent_id());
        // var_dump(has_term( 'detski-bodita', 'product_cat', $cart_product->get_parent_id() ));

        // here we check if the products in the cart are in category 'detski-bodita'.
        // since these are variation products, we check if their parents have the category.
        // if they do we add them the $in_cart boolean to true.
        if ( has_term( 'detski-bodita', 'product_cat', $cart_product->get_parent_id() ) ) {
            $in_cart = true;
            // var_dump($cart_product);
            $product_ids[] = $cart_product->get_id();
            $item_prices[$cart_product->get_id()] = $cart_product->get_price(); 
        } else {
            $product_ids[] = $cart_product->get_id();
            $item_prices[$cart_product->get_id()] = $cart_product->get_price(); 
        }

    }

    // here we chek if we have products with $in_cart boolean to true 
    // and if they are in category 'detski-bodita'
    if( $in_cart && has_term( 'detski-bodita', 'product_cat', $cart_product->get_parent_id() )) {
        // var_dump($item_prices);
        $count_ids = count($product_ids); // We count the items we have
        asort( $item_prices ); //Sort the prices from lowest to highest
        

        $count = 0; // we add another counter for the products that will be discounted.
        // here we check if the minimum amount of products is met for the discount.
        if( $count_ids >= 3 ) { 
            foreach( $item_prices as $id => $price ) {
                if( $count >= 1 ) {
                // var_dump($price)
                    break;
                }
                //$product = wc_get_product( $id );
                //$price = $product->get_price();
                $discount -= ($price * 100) / 100; // this is the discount that we apply - 100%
                $count++; // increase the counter in order to stop after the max amount of discounted products
           }
       }

    }

    if( $discount != 0 ){
        $wc_cart->add_fee( 'Отстъпка', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

我仍在测试它,但到目前为止它正在工作。也许之前的主要问题是我没有考虑到产品是有变化的,类别解析失败了。当我添加“get_parent_id()”时,我能够获得正确的类别。 现在我只需要弄清楚数量的问题。例如,如果该类别中的一种产品的数量为 2,而一种产品的数量为 1,或者该类别中的一种产品的数量为 3 或更多。非常感谢一些帮助或任何改进的想法。 :) 谢谢 :)

编辑:看来,如果我从正确的类别中选择 3 种产品并从任何其他类别中选择 1 种产品,则会应用折扣。但是,当我从正确的类别中删除一件商品时,折扣仍然适用,但不应该。 :( 我该如何解决这个问题?谢谢 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2020-08-18
    • 2017-06-05
    • 2019-06-28
    • 1970-01-01
    • 2018-01-30
    • 2017-09-27
    相关资源
    最近更新 更多