Advertisement
Guest User

Woocommerce Advanced Shipping - Total Bottles

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