简体   繁体   English

根据范围条件自动将产品添加到WooCommerce购物车

[英]Automatically add products to WooCommerce cart based on range conditions

What I'm trying to achieve here is the following: 我要在这里实现以下目标:

  • Automatically add BonusProduct0 to cart when subtotal reaches 65 小计达到65后自动将BonusProduct0添加到购物车
  • Automatically replace BonusProduct0 with BonusProduct1 when subtotal reaches 80 小计达到80时自动将BonusProduct0替换为BonusProduct1
  • Automatically replace BonusProduct1 with BonusProduct2 when subtotal reaches 100 小计达到100时自动将BonusProduct1替换为BonusProduct2
  • Remove all any bonus product that happens to be in the cart when subtotal goes below 65 当小计低于65时,删除所有可能在购物车中的所有奖励产品
  • Have the bonus products either zero-priced and unaccessible or normally priced and automatically reset their price when added to cart based on above conditions 将奖励产品设为零价且无法访问或正常定价,并根据上述条件将其添加到购物车时自动重置其价格

Working code. 工作代码。

function add_product_if_not_there($product_id) {
    $found = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        if ( $_product->get_id() == $product_id ) {
            $found = true;
            break;
        }
    }
    if (!$found)
        WC()->cart->add_to_cart( $product_id );

}

function remove_product_if_there($product_id) {
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        if ( $_product->get_id() == $product_id ) {
            WC()->cart->remove_cart_item( $cart_item_key );
            break;
        }
    }
}

abstract class FreeCookiesBundles
{
    const    NoCookiesBundle = 0;
    const   TwoCookiesBundle = 2;
    const ThreeCookiesBundle = 3;
    const  FourCookiesBundle = 4;
    const   TwoCookiesBundleId = 4920;
    const ThreeCookiesBundleId = 4921;
    const  FourCookiesBundleId = 4922;
} 

function bonus_add_product_to_cart() {
    global $woocommerce;

    if ( is_admin() )
        return ;

    $free_product_ids = array(FreeCookiesBundles::TwoCookiesBundleId,
                              FreeCookiesBundles::ThreeCookiesBundleId,
                              FreeCookiesBundles::FourCookiesBundleId);
    $cookies_bundle = FreeCookiesBundles::NoCookiesBundle;
    $cart_total_lvls = array(65, 100, 128);
    if ( ($woocommerce->cart->subtotal >= $cart_total_lvls[0]) &&
         ($woocommerce->cart->subtotal  < $cart_total_lvls[1])) {
        $cookies_bundle = FreeCookiesBundles::TwoCookiesBundle;
    } else if ( ($woocommerce->cart->subtotal >= $cart_total_lvls[1]) &&
                ($woocommerce->cart->subtotal  < $cart_total_lvls[2]) ) {
        $cookies_bundle = FreeCookiesBundles::ThreeCookiesBundle;
    } else if ( ($woocommerce->cart->subtotal >= $cart_total_lvls[2]) ) {
        $cookies_bundle = FreeCookiesBundles::FourCookiesBundle;
    }
    echo $cookies_bundle;

    switch ($cookies_bundle) {
        case FreeCookiesBundles::TwoCookiesBundle:
            add_product_if_not_there($free_product_ids[0]);
            remove_product_if_there($free_product_ids[1]);
            remove_product_if_there($free_product_ids[2]);
            break;
        case FreeCookiesBundles::ThreeCookiesBundle:
            add_product_if_not_there($free_product_ids[1]);
            remove_product_if_there($free_product_ids[0]);
            remove_product_if_there($free_product_ids[2]);
            break;
        case FreeCookiesBundles::FourCookiesBundle:
            add_product_if_not_there($free_product_ids[2]);
            remove_product_if_there($free_product_ids[0]);
            remove_product_if_there($free_product_ids[1]);
            break;
        default:
            remove_product_if_there($free_product_ids[0]);
            remove_product_if_there($free_product_ids[1]);
            remove_product_if_there($free_product_ids[2]);
    }
}
add_action( 'template_redirect', 'bonus_add_product_to_cart' );

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

相关问题 使用 Woocommerce 产品创建下拉菜单,并根据选择自动将产品添加到购物车 - Creating dropdown with Woocommerce products and automatically add product to cart based on selection 在访问WooCommerce时自动将多种产品添加到购物车 - Automatically add multiple products to cart on visit WooCommerce 允许根据 WooCommerce 中的购物车总数将特定产品添加到购物车 - Allow add to cart for specific products based on cart total in WooCommerce 如何在woocommerce购物车页面中基于当前产品添加相关产品? - How to add related products based on current products in woocommerce cart page? 如果 WooCommerce 购物车至少包含 X 个产品,则自动添加百分比折扣 - Automatically add a percentage discount if WooCommerce cart contains at least X products 替换WooCommerce单品中按重量加入购物车按钮 - Replace add to cart button based on weight in WooCommerce single products WooCommerce 添加<custom>产品到购物车</custom> - WooCommerce Add <custom> products to cart 根据 WooCommerce 购物车总数和月份范围添加或删除免费产品 - Add or remove a free product based on WooCommerce cart total and month range 根据类别名称woocommerce检查购物车中的产品? - Checking products in cart based on category name woocommerce? WooCommerce:如何以编程方式将一些产品添加到购物车? - WooCommerce: how to add a number of products to the cart programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM