I am working on having WooCommerce restrict product weight per category and starting from the ability to restrict total weight in cart as shown here.
The function is loaded into the themes functions.php file and works as expected by restricting the weight in cart of all items to 28.34g.
add_action('woocommerce_check_cart_items','check_cart_weight');
function check_cart_weight(){
global $woocommerce;
$weight = $woocommerce->cart->cart_contents_weight;
if( $weight > 28.34 ){
wc_add_notice( sprintf( __( 'You have %sg weight and we allow only one ounce of weight per order.', 'woocommerce' ), $weight ), 'error' );
}
}
The issue I'm having is identifying the right way to make the weight function conditional on category name while allowing different weights per category with each category having independent weight totals.
Meaning that each category can combine with weight limits maxed per category.
Example, restrict weight in cart by:
- Category 1: 30g
- Category 2: 38.34g
- Category 3: 50g
- Category 4: 100g
The initial thought was to use if ( $category = "Category 1") to start it off but haven't had success in that direction and not sure if a foreach prior to the if statement would be best direction. Any advice?