Tanvir
Tanvir

Reputation: 1683

Set max weight per cart item in Woocommerce

How can I set the maximum weight per product (not per order)?

The customer can buy any number (quantity) of products until the max weight is reached. If he buys 5 products, The weight of total unit of each product cant reach max weight. There can be many products in an order but Total units(quantity) per product has Max weight.

For example, In a single order:

Upvotes: 3

Views: 1250

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254271

In this example, the hooked function is triggered on add to cart action and you can perform all kind of checks to validate or not the action (and display a custom error message).

So you will see that you can target the total item weight individually…

add_filter( 'woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 20, 5 );
function custom_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = '', $variations = '' ) {
    // HERE define the weight limit per item
    $weight_limit = 2; // 2kg

    $total_item_weight = 0;

    // Check cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        $item_product_id = empty($variation_id) ? $product_id : $variation_id;

        // If the product is already in cart
        if( $item_product_id == $cart_item['data']->get_id() ){
            // Get total cart item weight
            $total_item_weight += $cart_item['data']->get_weight() * $cart_item['quantity'];
        }
    }

    // Get an instance of the WC_Product object
    $product = empty($variation_id) ? wc_get_product($product_id) : wc_get_product($variation_id);

    // Get total item weight
    $total_item_weight += $product->get_weight() * $quantity;

    if( $total_item_weight > $weight_limit ){
        $passed = false ;
        $message = __( "Custom warning message for weight exceed", "woocommerce" );
        wc_add_notice( $message, 'error' );
    }

    return $passed;
}

You will need also an additional hooked function that will be triggered on cart item quantity change:

add_filter( 'woocommerce_after_cart_item_quantity_update', 'limit_cart_item_quantity', 20, 4 );
function limit_cart_item_quantity( $cart_item_key, $new_quantity, $old_quantity, $cart ){
    // HERE define the weight limit per item
    $weight_limit = 2; // 2kg
    
    // Get an instance of the WC_Product object
    $product = $cart->cart_contents[ $cart_item_key ]['data'];
    
    $product_weight = $product->get_weight(); // The product weight
    
    // Calculate the limit allowed max quantity from allowed weight limit
    $max_quantity = floor( $weight_limit / $product_weight );
    
    // If the new quantity exceed the weight limit
    if( ( $new_quantity * $product_weight ) > $weight_limit ){
        
        // Change the quantity to the limit allowed max quantity
        $cart->cart_contents[ $cart_item_key ]['quantity'] = $max_quantity;
        
        // Add a custom notice
        $message = __( "Custom warning message for weight exceed", "woocommerce" );
        wc_add_notice( $message, 'notice' );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 5

Related Questions