繁体   English   中英

WooCommerce 购物车折扣:根据商品数量免费赠送一件商品

[英]WooCommerce cart discount: one item free based on item quantity

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

我使用钩子woocommerce_cart_calculate_fees创建了一个自定义 function ,到目前为止它正在运行。

这是我的代码:

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 的产品,则无法使用。

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

这是购物车页面的屏幕截图:

在此处输入图像描述


编辑:

折扣商品类别说明:

只有当购物车中有 3 件来自同一类别的商品时,才应应用折扣。

例如,类别是 T 恤和连帽衫。 如果我的购物车中有 3 件 T 恤,则应享受折扣。 如果我有 2 件 T 恤和 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 或更多。 真的很感激一些帮助或任何改进的想法。 :) 谢谢:)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM