簡體   English   中英

根據范圍條件自動將產品添加到WooCommerce購物車

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

我要在這里實現以下目標:

  • 小計達到65后自動將BonusProduct0添加到購物車
  • 小計達到80時自動將BonusProduct0替換為BonusProduct1
  • 小計達到100時自動將BonusProduct1替換為BonusProduct2
  • 當小計低於65時,刪除所有可能在購物車中的所有獎勵產品
  • 將獎勵產品設為零價且無法訪問或正常定價,並根據上述條件將其添加到購物車時自動重置其價格

工作代碼。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM