• Resolved tom

    (@klickchoice)


    I need to sell a product , for that product user can purchase only one quantity at a time . For that i write the following code

    function remove_qty_field( $return, $product ) {
        $my_product_id= $product -> post->ID ;
        if( $my_product_id==222){
        return true;
        }
    
    }
    add_filter( 'woocommerce_is_sold_individually', 'remove_qty_field', 10, 2 );

    The code is working properly and user can only order one item at a time .

    But there is a problem , that product have variation like red, black color . But user can add one black and one red in a order . I want to restrict this .

    Please help .

    https://www.ads-software.com/plugins/woocommerce/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Thats how it works for variable products. It does the check per line item, as as you know, these line items are split based on chosen variation.

    You’ll need custom validation on the ‘woocommerce_add_to_cart’ action to check if a product ID already exists in the cart.

    Thread Starter tom

    (@klickchoice)

    Thank you . As per your comment i made the following code . Please help to complete this

    add_filter('woocommerce_add_to_cart', 'woo_for_vaiarion');
    function woo_for_vaiarion() {
    
    	foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    		$_product = $values['data'];
    
    		if( (222 == $_product->id ) ) {
    
    			no need to do any thing  or show you cannot add (what will be the code ? )
    		}
    	}
    
    }

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Actually there is a better hook. Here is an example in core:

    function wc_protected_product_add_to_cart( $passed, $product_id ) {
    	if ( post_password_required( $product_id ) ) {
    		$passed = false;
    		wc_add_notice( __( 'This product is protected and cannot be purchased.', 'woocommerce' ), 'error' );
    	}
    	return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_cart', 10, 2 );

    Adapt that. Do a different check on cart items, and throw an error if it fails.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘woocommerce sold individually not working in variation product’ is closed to new replies.