Advertisement
Guest User

Woocommerce Advanced Shipping - Total Bottles

a guest
Oct 8th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. /**  Woocommerce Advanced Shipping (WAS)  **/
  2.  
  3. function check_total() {
  4.        
  5.     // Only run in the Cart or Checkout pages
  6.     if ( is_cart() || is_checkout() ) {
  7.  
  8.         global $woocommerce, $product;
  9.  
  10.         $total_bottles = 0;
  11.  
  12.         //loop through all cart products
  13.         foreach ( $woocommerce->cart->cart_contents as $product ) {
  14.  
  15.             // Add all bottles, 'bottles' is custom attribute set on the product page
  16.             $total_bottles += $product['bottles'];
  17.  
  18.         }
  19.  
  20.     }
  21.     return $total_bottles;
  22. }
  23.  
  24.  
  25. /**
  26.  * Add condition to conditions list.
  27.  *
  28.  * @param   array   $conditions List of existing conditions.
  29.  * @return  aray            List of modified conditions.
  30.  */
  31. function was_conditions_add_bottles( $conditions ) {
  32.    
  33.     // 'General', 'User Details', 'Cart' are default groups, you can also use something custom
  34.     $conditions['General']['bottlenum'] = __( 'Bottles', 'woocommerce-advanced-shipping' );
  35.     return $conditions;
  36. }
  37. add_filter( 'was_conditions', 'was_conditions_add_bottles', 10, 1 );
  38.  
  39.  
  40. /**
  41.  * Add value field for 'bottles' condition
  42.  *
  43.  * @param   array   $values     List of value field arguments
  44.  * @param   string  $condition  Name of the condition.
  45.  * @return  array   $values     List of modified value field arguments.
  46.  */
  47. function was_values_add_bottles( $values, $condition ) {
  48.     switch ( $condition ) {
  49.         case 'bottlenum':
  50.             $values['field'] = 'text';
  51.             $values['placeholder']  = 'ie. 3';
  52.             // Option 2; Drop down value field
  53.             //
  54.             // $values['field'] = 'select';
  55.             //
  56.             // foreach ( array( '1', '2' ) as $key => $value ) :
  57.             //  $values['options'][ $key ] = $value;
  58.             // endforeach;
  59.            
  60.         break;
  61.     }
  62.     return $values;
  63. }
  64. add_filter( 'was_values', 'was_values_add_bottles', 10, 2 );
  65.  
  66.  
  67. /**
  68.  * Must match quantity of bottles.
  69.  *
  70.  * @param  bool   $match    Current matching status. Default false.
  71.  * @param  string $operator Store owner selected operator.
  72.  * @param  mixed  $value    Store owner given value.
  73.  * @param  array  $package  Shipping package.
  74.  * @return bool             If the current user/environment matches this condition.
  75.  */
  76. function was_match_condition_bottles( $match, $operator, $value, $package ) {
  77.    
  78.     // Set total quantity of bottles in cart
  79.  
  80.     $total_bottles = check_total();
  81.  
  82.     // Check if value exists
  83.     if ( $value ) :
  84.         if ( $operator == '==' ) :
  85.             $match = ( $total_bottles == $value );
  86.         elseif ( $operator == '!=' ) :
  87.             $match = ( $total_bottles != $value );
  88.         elseif ( $operator == '>=' ) :
  89.             $match = ( $total_bottles >= $value );
  90.         elseif ( $operator == '<=' ) :
  91.             $match = ( $total_bottles <= $value );
  92.         endif;
  93.     endif;
  94.     return $match;
  95. }
  96. add_action( 'was_match_condition_bottles', 'was_match_condition_bottles', 10, 4 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement