https://codewp.ai/snippet/6ga3aypgslpz6jyalgph3knuswaj6/
Copy URL

Share this on your favorite social networks:

OR
Please log in to use this feature.
Email To Yourself Or A Developer
verified
Verified working as of March 21, 2023
GenID: 1345

Oxygen Builder Conditions - total weight of all WooCommerce products in cart

This Oxygen Builder condition for WooCommerce (generated by CodeWP) checks the total weight of all products in the cart. For example, if you want to conditionally display a message across the site if a customer has over 1000kg of products in their cart (for example, a notice saying that shipping will cost more due to this weight), you can use this to do just that.

All operators are available, meaning you can check if the weight is equal to, not equal to, greater than, less than, and more.

Original
if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

	global $oxy_condition_operators;

	oxygen_vsb_register_condition(
		// Condition Name
		'Total Weight of Products in Cart',

		// Values: The array of pre-set values the user can choose from.
		// Set the custom key's value to true to allow users to input custom values.
		array( 
			'options' => array(),
			'custom' => true
		),

		// Operators
		$oxy_condition_operators['int'],
		
		// Callback Function: Name of function that will be used to handle the condition
		'cwpai_condition_total_weight_callback',

		// Condition Category: Default ones are Archive, Author, Other, Post, User
		'WooCommerce'
	);

}

/**
 * Callback function to handle the condition.
 * @param  mixed 	$value    	Input value - in this case, weight of a product entered by the user.
 * @param  string 	$operator 	Comparison operator selected by the user.
 *
 * @return boolean 				true or false.
 */
function cwpai_condition_total_weight_callback( $value, $operator ) {

	$total_weight = 0;

	if ( ! empty( WC()->cart->get_cart() ) ) {
		foreach ( WC()->cart->get_cart() as $cart_item ) {
			$total_weight += $cart_item['data']->get_weight() * $cart_item['quantity'];
		}
	}

	// return the integer value of $value
	$value = intval( $value );

	return oxy_condition_eval_int( $total_weight, $value, $operator );

}
publishLoad Prompt
preview
Make Public
share
Share Snippet
download
Export Snippet
Mode: Oxygen Builder Conditions
Language: PHP
Visibility: Public
Date:
November 25, 2022
Creator:
@James LePa...
cross