• Resolved nartoof

    (@nartoof)


    Hi,

    I’m looking for a solution to override the check_cart_item_stock() function in the class-wc-cart.php file.

    My shop have products with many variations.

    If a user have the same product in the cart, but with 4 or 5 different variations, if there is an out-of-stock error message, having just the name is too ambiguous for user.

    I’d like to return something like :

    Sorry, we do not have enough "%1$s - Women, Blue, S" in stock

    To resume, I’d like to add the product attribute list in the out of stock error message.

    This function doesn’t have any filter to do it.

    Could you help me to find the solution ?

    Thank you.

    Anthony

Viewing 10 replies - 1 through 10 (of 10 total)
  • function stock_info_error( $message ){
        global $woocommerce;
        foreach ($woocommerce->cart->cart_contents as $item) {
            $product_id = isset($item['variation_id']) ? $item['variation_id'] : $item['product_id'];
            $product = wc_get_product( $product_id );
    
            if ($item['quantity'] > $product->get_stock_quantity()){
                $name = $product->get_name();
    			
    			/* Get Product Variations
    			 * $product->get_attributes();
    			 * $product->get_default_attributes();
    			 */
    			
    			$message = 'Sorry, we do not have enough "'.$name.'" in stock.';
                return $message;
            }
        }
    }
    add_filter( 'woocommerce_add_error', 'stock_info_error', 10, 1 );
    Thread Starter nartoof

    (@nartoof)

    @crslz Thank you very much, it works perfectly.

    I tried to use this filter before but I certainly didn’t do it correctly because it didn’t works.

    Now it’s ok !

    Have a nice day ??

    Thread Starter nartoof

    (@nartoof)

    @crslz I finally have a problem with your solution.

    It override other notice error, like at least invalid login informations on my account page.

    Maybe it override other notices error.

    Any idea to fix it ?

    Or another solution to add the product attribute list in the out of stock cart error message ?

    Thank you very much.

    Anthony

    Hi,

    Mormally the message will only be adjusted if the if condition is met, give it a try this way

    function stock_info_error( $message ) {
        global $woocommerce;
    	
        foreach ($woocommerce->cart->cart_contents as $item) {
            $product_id = isset($item['variation_id']) ? $item['variation_id'] : $item['product_id'];
            $product = wc_get_product( $product_id );
    
            if ($item['quantity'] > $product->get_stock_quantity()) {
                $name = $product->get_name();			
                $message = 'Sorry, we do not have enough "'.$name.'" in stock.';
            }
        }
    	
        return $message;
    }
    add_filter( 'woocommerce_add_error', 'stock_info_error', 10, 1 );
    Thread Starter nartoof

    (@nartoof)

    Hi,

    I still have the same problem, but I just can’t find where the second function is different from the first one ?

    I already tried to filter on woocommerce_add_error before to post here, and I had the same problem.

    I’ll need to find another way if it hide other errors…

    Thank you for your help anyway ??

    Anthony

    The difference is that in the new code the return is outside the if statement. $message can only be adjusted if the if condition is met, otherwise the default $message returns unchanged.

    Plugin Support AW a11n

    (@slash1andy)

    Automattic Happiness Engineer

    Hey there!

    Marking this as resolved, as it looks like you’re in a spot to finish the customizations you’ve been working on.

    Thread Starter nartoof

    (@nartoof)

    @crslz @andrew

    Actually it’s not resolved.

    Sorry I didn’t try the new solution earlier.

    I just tried it and the result is now an empty message error on my account page (and maybe somewhere else but I didn’t try any notices error spot) :

    <ul class="woocommerce-error" role="alert">
       <li>
       </li>
    </ul>

    It still overidding the notices errors.
    But now the $message variable is empty because the condition isn’t met on my account page.

    I can’t understand why this filter is overriding oher errors.

    I need to find why, but I don’t have any clue ATM…

    Wait? in your original question you spoke about the same product in the shopping cart, now about ‘because the condition isn’t met on my account page’? so this is about something else? I’ve tested this several times and this works, so unfortunately I cannot help you further because I can only repeat the same answer.

    Regards

    Thread Starter nartoof

    (@nartoof)

    I’m just saying your filter works great on the shopping cart, but it still override other notices error, like the wrong login and register message on the my account page.

    And I said this : ‘because the condition isn’t met on my account page’

    Because you told me to put the return $message outside the foreach loop and the if condition.

    I tried it and it still working on the shopping cart. But on other notices error, I got an empty message.

    Anyway I understand the problem isn’t coming from your answer and your filter, but it’s coming from my website configuration.

    I’ll check and try to find the reason why.

    But thank you very much for trying to help me on this ??

    • This reply was modified 4 years, 10 months ago by nartoof.
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Override check_cart_item_stock function’ is closed to new replies.