向购物车添加折扣功能

时间:2020-08-03 作者:tsvetko.krastev

嘿,伙计们:)我正在尝试对购物车实施自定义折扣规则。基本上有WooCommerce,该网站出售t恤衫。目前有一个促销活动,如果你买了3件t恤,你只需付2件,另一件价格最低的t恤是免费的。我使用钩子“woocommerce\\u cart\\u calculate\\u fees”创建了一个自定义函数,到目前为止,它仍在运行。这是我的代码:

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\', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}
这是购物车页面的屏幕截图-->;https://pasteboard.co/JkES3RD.png将显示并应用折扣。我无法理解的棘手部分是,如何使折扣产品的价格删除,并在表中显示价格0.00,以及如何编辑函数,以便在迷你购物车中仍显示3种产品,但显示2种产品的折扣价格?提前非常感谢!:)

编辑:而且,它似乎只有在我的购物车中有3种不同的产品时才起作用。如果我有一个数量为2的产品和一个数量为1的产品,那么它不起作用。。如何调整函数使其工作?

1 个回复
SO网友:tsvetko.krastev

在一阵头痛之后,我设法使它发挥了作用。这是我的代码:

add_action( \'woocommerce_cart_calculate_fees\', \'iom_add_custom_discount\', 10, 1 );
function iom_add_custom_discount( $wc_cart ){
    $discount = 0;
    $product_ids = array();
    $product_ids_disc = array();
    $item_prices = array();
    $in_cart = false;
 
    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_product = $cart_item[\'data\'];
 
        // 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() ) ) {
            // var_dump(count($in_cart));
            $in_cart = true;
            $product_ids_disc[] = $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 check 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() ) ) ) {
        $count_ids = count($product_ids_disc); // 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 ) {
                    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)
    }
}
我还有一些问题需要解决。仅当购物车中有3件相同类别的商品时,才应使用折扣。例如,类别为t恤和连帽衫。如果我的购物车里有3件t恤,就应该打折。如果我有两件t恤和一件连帽衫,则不应享受折扣。如果我有3件t恤和1件连帽衫,则应享受折扣。但是,我的功能似乎只有在购物车中有来自同一类别的3种不同产品时才起作用。如果我有1件数量为2的t恤和1件数量为1的t恤,那么它不起作用,应该在什么时候应用折扣。此外,如果我在购物车里放了3件t恤和1件连帽衫,然后取下1件t恤,那么在不应该的时候,折扣仍然适用。。任何帮助都将不胜感激。谢谢!:)

相关推荐

在PHP中删除ECHO输出中的重复项

现在,下面的函数输出产品的所有变体图像。E、 g.3倍蓝色(S、M、L号),3倍红色(S、M、L号)我想下面的功能只输出唯一的彩色图像。E、 g.1个蓝色,1个红色尝试在回显字符串中使用array\\u unique,但无法使其正常工作。谢谢你的帮助。function loop_display_variation_attribute_and_thumbnail() { global $product; if( $product->is_type(\'variable\