最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Limit total quantity globally for specific product category in WooCommerce - Stack Overflow

programmeradmin2浏览0评论

For a webshop i build in wordpress/flatsome/woocommerce there is a compliance limitation for the maximum amount of 10 weedseeds a customer can order (yess i'm from Holland haha..). There are 5 seeds in a package so there is a maximum of 2 packages in a order.

I have defined some code in functions.php to handle this, but it work's almost fine only when i open the cart i can increase the amount of a weedseeds-product with the +button to 3 or higher. In the mini-cart(pop-up cart) it works good allready instead!

actual code: theme editor-> childtheme selected:

<?php
// Add custom Theme Functions here
//
   function limit_weedseeds_to_two_packages( $passed, $product_id, $quantity, $variation_id = '', $variations = '' ) {
    $limit = 2; // Set the limit to 2 packages
    $weedseeds_category = 'seeds-wietzaadjes'; // actual category slug
    
    $total_quantity = get_total_weedseeds_quantity();

    // Check if the total quantity of weedseeds exceeds the limit
    if ( $total_quantity + $quantity > $limit ) {
        wc_add_notice( sprintf( 'Je mag maximaal %d verpakkingen wiedzaadjes per bestelling kopen.', $limit ), 'error' );
        return false;
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'limit_weedseeds_to_two_packages', 10, 5 );




function limit_weedseeds_quantity_in_cart( $passed, $cart_item_key, $values, $quantity ) {
    $limit = 2; // Set the limit to 2 packages
    $weedseeds_category = 'seeds-wietzaadjes'; // actual category slug
    
    $total_quantity = get_total_weedseeds_quantity();

    // Check if the total quantity of weedseeds exceeds the limit
    if ( $total_quantity + $quantity - $values['quantity'] > $limit ) {
        wc_add_notice( sprintf( 'Je mag maximaal %d verpakkingen wiedzaadjes per bestelling kopen.', $limit ), 'error' );
        return false;
    }

    return $passed;
}
add_filter( 'woocommerce_update_cart_validation', 'limit_weedseeds_quantity_in_cart', 10, 4 );




function check_weedseeds_limit_before_checkout() {
    $limit = 2; // Set the limit to 2 packages
    $weedseeds_category = 'seeds-wietzaadjes'; // actual category slug

    $total_quantity = get_total_weedseeds_quantity();

    // Check if the total quantity of weedseeds exceeds the limit
    if ( $total_quantity > $limit ) {
        wc_add_notice( sprintf( 'Je mag maximaal %d verpakkingen wiedzaadjes per bestelling kopen.', $limit ), 'error' );
    }
}
add_action( 'woocommerce_check_cart_items', 'check_weedseeds_limit_before_checkout' );




function get_total_weedseeds_quantity() {
    $weedseeds_category = 'seeds-wietzaadjes'; // actual category slug
    $cart = WC()->cart->get_cart();
    $total_quantity = 0;

    // Loop through the cart items
    foreach ( $cart as $cart_item ) {
        // Check if the cart item belongs to the weedseeds category
        if ( has_term( $weedseeds_category, 'product_cat', $cart_item['product_id'] ) ) {
            $total_quantity += $cart_item['quantity'];
        }
    }

    return $total_quantity;
}

So the first function is working correct:

add_filter( 'woocommerce_add_to_cart_validation', 'limit_weedseeds_to_two_packages', 10, 5 );

The problem i think logically has to be in the second function:

add_filter( 'woocommerce_update_cart_validation', 'limit_weedseeds_quantity_in_cart', 10, 4 );

The second end 3rd function both give the notice/errormessage in frontend but don't prevent the increasing of product amount.

For a webshop i build in wordpress/flatsome/woocommerce there is a compliance limitation for the maximum amount of 10 weedseeds a customer can order (yess i'm from Holland haha..). There are 5 seeds in a package so there is a maximum of 2 packages in a order.

I have defined some code in functions.php to handle this, but it work's almost fine only when i open the cart i can increase the amount of a weedseeds-product with the +button to 3 or higher. In the mini-cart(pop-up cart) it works good allready instead!

actual code: theme editor-> childtheme selected:

<?php
// Add custom Theme Functions here
//
   function limit_weedseeds_to_two_packages( $passed, $product_id, $quantity, $variation_id = '', $variations = '' ) {
    $limit = 2; // Set the limit to 2 packages
    $weedseeds_category = 'seeds-wietzaadjes'; // actual category slug
    
    $total_quantity = get_total_weedseeds_quantity();

    // Check if the total quantity of weedseeds exceeds the limit
    if ( $total_quantity + $quantity > $limit ) {
        wc_add_notice( sprintf( 'Je mag maximaal %d verpakkingen wiedzaadjes per bestelling kopen.', $limit ), 'error' );
        return false;
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'limit_weedseeds_to_two_packages', 10, 5 );




function limit_weedseeds_quantity_in_cart( $passed, $cart_item_key, $values, $quantity ) {
    $limit = 2; // Set the limit to 2 packages
    $weedseeds_category = 'seeds-wietzaadjes'; // actual category slug
    
    $total_quantity = get_total_weedseeds_quantity();

    // Check if the total quantity of weedseeds exceeds the limit
    if ( $total_quantity + $quantity - $values['quantity'] > $limit ) {
        wc_add_notice( sprintf( 'Je mag maximaal %d verpakkingen wiedzaadjes per bestelling kopen.', $limit ), 'error' );
        return false;
    }

    return $passed;
}
add_filter( 'woocommerce_update_cart_validation', 'limit_weedseeds_quantity_in_cart', 10, 4 );




function check_weedseeds_limit_before_checkout() {
    $limit = 2; // Set the limit to 2 packages
    $weedseeds_category = 'seeds-wietzaadjes'; // actual category slug

    $total_quantity = get_total_weedseeds_quantity();

    // Check if the total quantity of weedseeds exceeds the limit
    if ( $total_quantity > $limit ) {
        wc_add_notice( sprintf( 'Je mag maximaal %d verpakkingen wiedzaadjes per bestelling kopen.', $limit ), 'error' );
    }
}
add_action( 'woocommerce_check_cart_items', 'check_weedseeds_limit_before_checkout' );




function get_total_weedseeds_quantity() {
    $weedseeds_category = 'seeds-wietzaadjes'; // actual category slug
    $cart = WC()->cart->get_cart();
    $total_quantity = 0;

    // Loop through the cart items
    foreach ( $cart as $cart_item ) {
        // Check if the cart item belongs to the weedseeds category
        if ( has_term( $weedseeds_category, 'product_cat', $cart_item['product_id'] ) ) {
            $total_quantity += $cart_item['quantity'];
        }
    }

    return $total_quantity;
}

So the first function is working correct:

add_filter( 'woocommerce_add_to_cart_validation', 'limit_weedseeds_to_two_packages', 10, 5 );

The problem i think logically has to be in the second function:

add_filter( 'woocommerce_update_cart_validation', 'limit_weedseeds_quantity_in_cart', 10, 4 );

The second end 3rd function both give the notice/errormessage in frontend but don't prevent the increasing of product amount.

Share Improve this question edited Mar 5 at 22:13 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked Mar 5 at 19:55 mjCodemjCode 96 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

There are some mistakes and missing things in your code: For example, in the 2 validation functions, you need first to check that the current product or item, belongs to your targeted product category.

If your 2 validation functions work effectively, you don't need the last one.

Try the following:

// Utility function: Checks a a given product Id belongs to weedseeds category
function has_weedseeds_category( $product_id ) {
    return has_term( 'seeds-wietzaadjes', 'product_cat', $product_id );
}

// Utility function: Get cart items total quantity for weedseeds items only
function get_cart_total_weedseeds_qty( $item_key = null, $total_qty = 0 ) {
    if ( WC()->cart->is_empty() ) return $total_qty;

    // Loop through the cart items
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check if the cart item belongs to the weedseeds category
        if ( has_weedseeds_category( $cart_item['product_id'] ) && $item_key !== $cart_item_key ) {
            $total_qty += $cart_item['quantity'];
        }
    }
    return $total_qty;
}

// Utility function: Define the allowed weedseeds limit
function get_weedseeds_limit() {
    return 2; // Set the limit to 2 packages
}

// Utility function: Add an error notice for weedseeds exceeding limit
function add_error_notice_for_weedseeds_limit() {
    wc_add_notice( sprintf( esc_html__('Je mag maximaal %d verpakkingen wiedzaadjes per bestelling kopen.'), get_weedseeds_limit() ), 'error' );
}

// Add to cart validation for weedseeds products
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_weedseed_limit', 10, 3 );
function add_to_cart_weedseed_limit( $passed, $product_id, $quantity ) {
    // Check that current product belongs to weedseeds category, if not exit.
    if ( ! has_weedseeds_category( $product_id ) ) return $passed;

    // Check if the product quantity exceeds the weedseeds limit
    if ( WC()->cart->is_empty() && $quantity > get_weedseeds_limit() ) {
        add_error_notice_for_weedseeds_limit();
        return false;
    }
    // Check if the total weedseeds quantity exceeds the weedseeds limit
    elseif ( ( get_cart_total_weedseeds_qty() + $quantity ) > get_weedseeds_limit() ) {
        add_error_notice_for_weedseeds_limit();
        return false;
    }
    return $passed;
}

// Update cart validation for weedseeds cart items
add_filter( 'woocommerce_update_cart_validation', 'limit_weedseeds_quantity_in_cart', 10, 4 );
function limit_weedseeds_quantity_in_cart( $passed, $cart_item_key, $values, $quantity ) {
    // Check that current cart item belongs to weedseeds category, if not exit.
    if ( ! has_weedseeds_category( $values['product_id'] ) ) return $passed;

    // Check if the total quantity of weedseeds exceeds the limit
    if ( ( get_cart_total_weedseeds_qty( $cart_item_key ) + $quantity ) > get_weedseeds_limit() ) {
        add_error_notice_for_weedseeds_limit();
        return false;
    }
    return $passed;
}

It should work.


The last revised hooked function, but unneeded as it will drop a 2nd error notice:

// Check cart items (not really needed anymore)
add_action( 'woocommerce_check_cart_items', 'check_weedseeds_limit_before_checkout' );
function check_weedseeds_limit_before_checkout() {
    // Check if the total quantity of weedseeds exceeds the limit
    if ( get_cart_total_weedseeds_qty() > get_weedseeds_limit() ) {
        add_error_notice_for_weedseeds_limit();
    }
}
发布评论

评论列表(0)

  1. 暂无评论