wcpgsk_add_cart_item

The Woocommerce Poor Guys Swiss Knife wcpgsk add cart item function.

Description

wcpgsk_add_cart_item( $cart_item_data, $cart_item_key ); 

Parameters (2)

0. $cart_item_data
The cart item data.
1. $cart_item_key
The cart item key.

Usage

  1. if ( !function_exists( 'wcpgsk_add_cart_item' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-poor-guys-swiss-knife/wcpgsk-af.php'; 
  3.  
  4. // The cart item data. 
  5. $cart_item_data = null; 
  6.  
  7. // The cart item key. 
  8. $cart_item_key = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wcpgsk_add_cart_item($cart_item_data, $cart_item_key); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wcpgsk-af.php  
  1. function wcpgsk_add_cart_item( $cart_item_data, $cart_item_key ) { 
  2. global $woocommerce; 
  3. $options = get_option( 'wcpgsk_settings' ); 
  4.  
  5. $product_id = $cart_item_data['product_id']; 
  6. $variation_id = $cart_item_data['variation_id']; 
  7. $product = get_product($product_id); 
  8. $quantity = $cart_item_data['quantity']; 
  9.  
  10. $maxqty = isset($options['cart']['maxqty_' . $product->product_type]) ? $options['cart']['maxqty_' . $product->product_type] : 0; 
  11. $minqty = isset($options['cart']['minqty_' . $product->product_type]) ? $options['cart']['minqty_' . $product->product_type] : 0; 
  12. $stepqty = isset($options['cart']['stepqty_' . $product->product_type]) ? $options['cart']['stepqty_' . $product->product_type] : 0; 
  13.  
  14. if ( isset($options['cart']['minmaxstepproduct']) && $options['cart']['minmaxstepproduct'] == 1 ) : 
  15. $maxval = get_post_meta($product_id, '_wcpgsk_maxqty', true); 
  16. if ( isset($maxval) && $maxval > 0 ) : 
  17. $maxqty = $maxval; 
  18. endif; 
  19. $minval = get_post_meta($product_id, '_wcpgsk_minqty', true); 
  20. if ( isset($minval) && $minval > 0 ) : 
  21. $minqty = $minval; 
  22. endif; 
  23. $stepval = get_post_meta($product_id, '_wcpgsk_stepqty', true); 
  24. if ( isset($stepval) && $stepval > 0 ) : 
  25. $stepqty = $stepval; 
  26. endif; 
  27. endif; 
  28.  
  29. if ($minqty > 0 && $quantity < $minqty) : 
  30. $cart_item_data['quantity'] = $minqty; 
  31. wcpgsk_add_message( sprintf( __( 'You have to buy a minimum quantity. We have set the required minimum of %s as quantity for you.', WCPGSK_DOMAIN ), $minqty ) ); 
  32. elseif ($maxqty > 0 && $quantity > $maxqty) : 
  33. $cart_item_data['quantity'] = $maxqty; 
  34. wcpgsk_add_message( sprintf(__( 'You cannot buy more than the allowed maximum quantity. We have set the allowed maximum of %s as quantity for you.', WCPGSK_DOMAIN ), $maxqty ) ); 
  35. elseif ( $stepqty > 0 && ( $quantity % $stepqty ) > 0 ) : 
  36. $remainder = $quantity % $stepqty; 
  37. $newqty = $quantity - $remainder; 
  38. if ( $newqty > 0 ) : 
  39. $cart_item_data['quantity'] = $newqty; 
  40. else : 
  41. $newqty = $stepqty; 
  42. $cart_item_data['quantity'] = $newqty; 
  43. endif;  
  44. wcpgsk_add_message( sprintf( __( 'You have to buy this product in multiples of %s. We have set the product quantity to the closest lower multiple available.', WCPGSK_DOMAIN ), $stepqty ) ); 
  45. endif; 
  46. return $cart_item_data;