Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
- remove_action('woocommerce_add_to_cart', __FUNCTION__);
- if(isset($_REQUEST['add-to-cart'])){
- $productId = $_REQUEST['add-to-cart'];
- }else{
- $productId = $_REQUEST['product_id'];
- }
- $can_be_addded = false;
- //check if product already in cart
- if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
- foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
- $_product = $values['data'];
- //type of cart of first item
- $cart_type = get_post_meta($_product->id,'_point_base',true);
- //type of product that is requested to add in cart
- $type_of_new_item = get_post_meta($productId,'_point_base',true);
- if ($cart_type == 'yes' && $type_of_new_item == 'yes' )
- $can_be_addded = true;
- if (($cart_type == 'no' || $cart_type == '' ) && ($type_of_new_item == 'no' || $type_of_new_item == '')) {
- $can_be_addded = true;
- }
- break;
- }
- // if product not found, add it
- if ($can_be_addded){
- WC()->cart->add_to_cart( $productId );
- }else{
- wc_add_notice('Product cant be added','error');
- }
- } else {
- // if no products in cart, add it
- WC()->cart->add_to_cart( $productId );
- }
Add Comment
Please, Sign In to add comment