簡體   English   中英

在 WooCommerce 中將產品添加到購物車時自動添加包裝

[英]Automatically add packaging when adding product to cart in WooCommerce

在 WooCommerce 中,我使用一個代碼,在將任何菜餚添加到購物車時自動添加包裝。

add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );
function add_delivery_charge_to_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $dcharge_id  = 5737; // "LunchBox" to be added to cart
    $items_count = 0;  // Initializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check if "LunchBox" product is already in cart
        if( $cart_item['data']->get_id() == $dcharge_id ) {
            $dcharge_key = $cart_item_key;
            $dcharge_qty = $cart_item['quantity'];
        }
        // Counting other items than "LunchBox"
        else {
            $items_count++;
        }
    }

    // If product "LunchBox" is in cart, we check the quantity to update it if needed
    if ( isset($dcharge_key) && $dcharge_qty != $items_count ) {
        $cart->set_quantity( $dcharge_key, $items_count );
    }
    // If product "LunchBox" is not in cart, we add it
    elseif ( ! isset($dcharge_key) && $items_count > 0 ) {
        $cart->add_to_cart( $dcharge_id, $items_count );
    }
}

我一盤=一盒飯,三道菜(三盒飯)=一包。

我想實現這樣的功能......

  1. 添加菜品時,會自動添加“午餐盒”。 此外,如果購物車中有 3 個便當盒,則會自動添加一個“包裹”。

  2. “Lunchbox”和“Package”名稱已經在購物車中,然后它們的數量會自動添加並計算包裝的總成本。

但是這里有兩個問題:

一種。 隨着已經在購物車中的一道菜的份數增加,午餐盒的數量不會增加。

如果午餐盒的數量變成“3”,我就是無法理解如何自動添加另一個產品“包裹”。

我會很高興為您提供幫助!

為清楚起見:

每個產品都是一道菜,除了“飯盒”和“包裝”也是產品。 客戶不會自己選擇任何東西,一切都會自動發生。

然后你得到:

function add_delivery_charge_to_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $lunchbox_id  = 5737; // "LunchBox" to be added to cart
    $pakket_id = 218; // "Pakket" to be added to cart

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check if "LunchBox" product is already in cart
        if( $cart_item['data']->get_id() == $lunchbox_id ) {
            $lunchbox_key = $cart_item_key;
            $lunchbox_qty = $cart_item['quantity'];
        }
        
        // Check if "Pakket" product is already in cart
        if( $cart_item['data']->get_id() == $pakket_id ) {
            $pakket_key = $cart_item_key;
            $pakket_qty = $cart_item['quantity'];
        }       
    }
    
    // Get total items in cart, counts number of products and quantity per product
    $total_items_in_cart = $cart->get_cart_contents_count();
    
    // If product "LunchBox" is in cart, we check the quantity to update it if needed
    if ( isset($lunchbox_key) && $lunchbox_qty != $total_items_in_cart ) {
        // Lunchbox total = total_items_in_cart 
        $lunchbox_total = $total_items_in_cart;
        
        // Isset lunchbox qty, lunchbox total - lunchbox qty
        if ( isset($lunchbox_qty) ) {
            $lunchbox_total = $lunchbox_total - $lunchbox_qty;
        }

        // Isset pakket qty, lunchbox total - pakket qty        
        if ( isset($pakket_qty) ) {
            $lunchbox_total = $lunchbox_total - $pakket_qty;
        } 
        
        // Set quantity, lunchbox
        $cart->set_quantity( $lunchbox_key, $lunchbox_total );
        
    } elseif ( !isset($lunchbox_key) && $total_items_in_cart > 0 ) {
        // Product "LunchBox" is not in cart, we add it
        $cart->add_to_cart( $lunchbox_id, $total_items_in_cart );
    }
    
    // Total items in cart greater than or equal to 3
    if ( $total_items_in_cart >= 3 ) {
        // Pakket total = total_items_in_cart 
        $pakket_total = $total_items_in_cart;
        
        // Isset lunchbox qty, pakket total - lunchbox qty
        if ( isset($lunchbox_qty) ) {
            $pakket_total = $pakket_total - $lunchbox_qty;
        }

        // Isset pakket qty, pakket total - pakket qty      
        if ( isset($pakket_qty) ) {
            $pakket_total = $pakket_total - $pakket_qty;
        }       
        

        // Pakket total = pakket_total / 3 = floor(result)
        // Floor = round fractions down, rounding result down
        $pakket_total = floor( $pakket_total / 3 );
        
        // If product "Pakket" is in cart
        if ( isset($pakket_key) ) {         
            $cart->set_quantity( $pakket_key, $pakket_total );
        } elseif ( !isset($pakket_key) ) {
            // Product "Pakket" is not in cart, we add it
            $cart->add_to_cart( $pakket_id, $pakket_total );
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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