簡體   English   中英

將允許添加到購物車的產品的最大數量設置為 6 並通知

[英]Set Max quantity allowed of product added to cart to 6 with notice

到目前為止,我已經嘗試了幾種變體。 我正在嘗試將任何單個產品的最大數量或可變產品變化設置為最多 6 個以供購買。 我正在使用WooCommerce 答案代碼中驗證的每個產品添加到購物車的最大數量

這樣做的問題是:它不會在產品頁面上檢查,而是允許將其以任意數量添加到購物車而不會出現錯誤消息,然后當您導航到購物車時,它會執行檢查,自動更新購物車(同樣沒有錯誤消息顯示購物車更新為 6 的原因)。

如果它是相關的,我還成功添加了一個 function ,一次最多可以將 6 個添加到購物車中。 (這對我的問題沒有幫助,因為有人可以繼續在產品頁面上添加。

來源: 調整數量輸入值

add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 ); // Simple products

function jk_woocommerce_quantity_input_args( $args, $product ) {
    if ( is_singular( 'product' ) ) {
        $args['input_value']    = 1;    // Starting value (we only want to affect product pages, not cart)
    }
    $args['max_value']  = 6;    // Maximum value
    $args['min_value']  = 1;    // Minimum value
    $args['step']       = 1;    // Quantity steps
    return $args;
}

add_filter( 'woocommerce_available_variation', 'jk_woocommerce_available_variation' ); // Variations

function jk_woocommerce_available_variation( $args ) {
    $args['max_qty'] = 6;       // Maximum value (variations)
    $args['min_qty'] = 1;       // Minimum value (variations)
    return $args;
}

您可以 WC woocommerce_add_to_cart_validation過濾器掛鈎,您可以在其中設置最大數量,然后根據產品購物車數量檢查最大數量。 檢查下面的代碼。 代碼將 go 到活動主題功能。php 文件。

/*
* Validating the quantity on add to cart action with the quantity of the same product available in the cart. 
*/
function show_error_on_max_quantity_reached( $passed, $product_id, $quantity, $variation_id = '', $variations = '' ) {

    $product_max = 6;

    $already_in_cart    = wc_qty_get_cart_qty( $product_id );
    $product            = wc_get_product( $product_id );
    $product_title      = $product->get_title();
    
    if ( !empty( $already_in_cart ) ) {
        
        if ( ( $already_in_cart + $quantity ) > $product_max ) {
            
            $passed = false;            

            wc_add_notice( 
                sprintf( __( 'Sorry, You can add a maximum of %1$s %2$s\'s to %3$s. You already have %4$s.', 'woocommerce-max-quantity' ), 
                    $product_max,
                    $product_title,
                    '<a href="' . esc_url( wc_get_cart_url() ) . '">' . __( 'Your cart', 'woocommerce-max-quantity' ) . '</a>',
                    $already_in_cart 
                ),
            'error' );

        }
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'show_error_on_max_quantity_reached', 1, 5 );

/*
* Get the total quantity of the product available in the cart.
*/ 
function wc_qty_get_cart_qty( $product_id , $cart_item_key = '' ) {
    global $woocommerce;
    $running_qty = 0; // iniializing quantity to 0

    // search the cart for the product in and calculate quantity.
    foreach($woocommerce->cart->get_cart() as $other_cart_item_keys => $values ) {
        if ( $product_id == $values['product_id'] ) {

            if ( $cart_item_key == $other_cart_item_keys ) {
                continue;
            }

            $running_qty += (int) $values['quantity'];
        }
    }

    return $running_qty;
}

Tested and works. 

在此處輸入圖像描述

暫無
暫無

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

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