Guest User

Untitled

a guest
Dec 13th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
  2.  
  3. remove_action('woocommerce_add_to_cart', __FUNCTION__);
  4.  
  5. if(isset($_REQUEST['add-to-cart'])){
  6. $productId = $_REQUEST['add-to-cart'];
  7. }else{
  8. $productId = $_REQUEST['product_id'];
  9. }
  10.  
  11.  
  12. $can_be_addded = false;
  13. //check if product already in cart
  14. if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
  15.  
  16. foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
  17.  
  18. $_product = $values['data'];
  19. //type of cart of first item
  20. $cart_type = get_post_meta($_product->id,'_point_base',true);
  21. //type of product that is requested to add in cart
  22. $type_of_new_item = get_post_meta($productId,'_point_base',true);
  23.  
  24. if ($cart_type == 'yes' && $type_of_new_item == 'yes' )
  25. $can_be_addded = true;
  26.  
  27. if (($cart_type == 'no' || $cart_type == '' ) && ($type_of_new_item == 'no' || $type_of_new_item == '')) {
  28. $can_be_addded = true;
  29. }
  30. break;
  31. }
  32.  
  33. // if product not found, add it
  34. if ($can_be_addded){
  35.  
  36. WC()->cart->add_to_cart( $productId );
  37. }else{
  38.  
  39. wc_add_notice('Product cant be added','error');
  40. }
  41.  
  42. } else {
  43. // if no products in cart, add it
  44. WC()->cart->add_to_cart( $productId );
  45. }
Add Comment
Please, Sign In to add comment