Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Woocommerce Advanced Shipping (WAS) **/
- function custom_get_total_bottles_in_cart() {
- $total_bottles = 0;
- // Only run in the Cart or Checkout pages
- if ( is_cart() || is_checkout() ) {
- //loop through all cart products
- foreach ( WC()->cart->cart_contents as $product ) {
- // See https://docs.woothemes.com/wc-apidocs/source-class-WC_Product.html#1333
- $total_bottles += $product->get_attribute( 'bottles' );
- }
- }
- return $total_bottles;
- }
- /**
- * Add condition to conditions list.
- *
- * @param array $conditions List of existing conditions.
- * @return aray List of modified conditions.
- */
- function was_conditions_add_bottles( $conditions ) {
- // 'General', 'User Details', 'Cart' are default groups, you can also use something custom
- $conditions['General']['bottlenum'] = __( 'Bottles', 'woocommerce-advanced-shipping' );
- return $conditions;
- }
- add_filter( 'was_conditions', 'was_conditions_add_bottles', 10, 1 );
- /**
- * Add value field for 'bottles' condition
- *
- * @param array $values List of value field arguments
- * @param string $condition Name of the condition.
- * @return array $values List of modified value field arguments.
- */
- function was_values_add_bottles( $values, $condition ) {
- switch ( $condition ) {
- case 'bottlenum':
- $values['field'] = 'text';
- $values['placeholder'] = 'ie. 3';
- break;
- }
- return $values;
- }
- add_filter( 'was_values', 'was_values_add_bottles', 10, 2 );
- /**
- * Must match quantity of bottles.
- *
- * @param bool $match Current matching status. Default false.
- * @param string $operator Store owner selected operator.
- * @param mixed $value Store owner given value.
- * @param array $package Shipping package.
- * @return bool If the current user/environment matches this condition.
- */
- function was_match_condition_bottles( $match, $operator, $value, $package ) {
- // Set total quantity of bottles in cart
- $total_bottles = custom_get_total_bottles_in_cart();
- // Check if value exists
- if ( $value ) :
- if ( $operator == '==' ) :
- $match = ( $total_bottles == $value );
- elseif ( $operator == '!=' ) :
- $match = ( $total_bottles != $value );
- elseif ( $operator == '>=' ) :
- $match = ( $total_bottles >= $value );
- elseif ( $operator == '<=' ) :
- $match = ( $total_bottles <= $value );
- endif;
- endif;
- return $match;
- }
- add_action( 'was_match_condition_bottlenum', 'was_match_condition_bottles', 10, 4 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement