• Resolved nartoof

    (@nartoof)


    Hi,

    I’m building a Woocommerce website, and stock amounts are updated with an API so I can’t fake them.

    I’d like to make the online stock amount different than my real stock, for example :

    I got 10 items in my real stock.
    I’d like to save 3 items for my physical shop, then I’d like to have 7 items available online.

    I already added a product meta where I set the number from which I want the product to be unavailable.

    In my example, if real stock goes down to 3, my product is unpurchasable and it works well.

    But if I got 10 items, I can actually add these 10 items to the cart.

    Is there any simple solution to override the product stock amount for every features (PHP function, AJAX add to cart, notices) ?

    I don’t find any solution online and I’m scared I have to use a lot of hooks and filter to verify my “fake” stock on every step…

    Thank you for your help.

    Anthony

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello @nartoof,

    Thank for the detailed description of the issue. I have a much simpler solution to this. Since you are managing stock, you should be able to set the store’s Out of stock threshold setting to the number you want to reserve for your brick and mortar store. This will prevent sales when the stock hits that level. The one caveat is that this setting is Global so it would affect all products. There is not a way to do this on a per-product basis. I hope this helps! If you have any further questions please feel free to reply to this message and I will get back to you.

    Take care and stay safe!

    Thread Starter nartoof

    (@nartoof)

    Hi @phillipwoo ,

    Thank you for your answer, but for this project I need this number to be variable for each product.

    I’ve finally found 2 magic filters to make it works :

    function MSW_custom_get_stock_quantity( $value, $product ) 
    { 
    	if ($product->get_type()=='variable' || $product->get_type()=='variation') $website_quantity=floatval(get_post_meta($product->get_parent_id(), 'out_of_stock_value', true));
    	elseif ($product->get_type()=='simple') $website_quantity=floatval(get_post_meta($product->get_id(), 'out_of_stock_value', true));
    
    	$value = floatval($value-$website_quantity); //Float cause I can have not INT quantities
    
    	return $value;
    }
    add_filter( 'woocommerce_product_get_stock_quantity' ,'MSW_custom_get_stock_quantity', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_stock_quantity' ,'MSW_custom_get_stock_quantity', 10, 2 );

    It is what I love with WordPress and Woocommerce, there is almost always a filter or a hook you can use to do magic ??

    Thank you for your help anyway ??

    Anthony

    • This reply was modified 4 years, 6 months ago by nartoof.

    You’re very welcome. I am glad you found a hook for this! I will be sure to add it to my “customer service toolbox” ;-). I am marking this issue as Resolved now. If you still need support on this feel free to change that and let us know what else is occurring. If you have a new issue for us, you can open a new thread for that. Take care!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Make stock available different than real stock amount’ is closed to new replies.