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

时间:2018-11-29 15:44:36

标签: php wordpress woocommerce

我要在此处实现的目标如下:

  • 小计达到65后自动将BonusProduct0添加到购物车
  • 小计达到80后自动将BonusProduct0替换为BonusProduct1
  • 小计达到100时自动将BonusProduct1替换为BonusProduct2
  • 当小计低于65时,删除所有可能在购物车中的所有奖励产品
  • 让奖励产品的价格为零且无法访问或正常定价,并根据上述条件将其添加到购物车时自动重置其价格

1 个答案:

答案 0 :(得分:0)

工作代码。

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' );