• Resolved guardiano78

    (@guardiano78)


    Hello,
    I have to replace the product title with the product sku on the cart page when the quantity of the product is not enough.
    In class-wc-cart.php at line 789 i found the code to customize:

    if ( apply_filters( 'woocommerce_cart_item_required_stock_is_not_enough', $product->get_stock_quantity() < ( $held_stock + $required_stock ), $product, $values ) ) {
    				/* translators: 1: product name 2: quantity in stock */
    				$error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s available). We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity() - $held_stock, $product ) ) );
    				return $error;
    			}

    Serching on google, I found this hook:

    function filter_woocommerce_cart_product_not_enough_stock_already_in_cart_message( $message, $product_data, $stock_quantity, $stock_quantity_in_cart ) {
        // New message
        $message = __( 'You are currently trying to order more of this product than are currently available at your shipping location. Please call our sales team to discuss availability', 'woocommerce' );
        
        return $message;
    }
    add_filter( 'woocommerce_cart_product_not_enough_stock_already_in_cart_message', 'filter_woocommerce_cart_product_not_enough_stock_already_in_cart_message', 10, 4 );

    but it doesn’t work.
    Anyone knows hot to do it?
    thanks.

Viewing 15 replies - 1 through 15 (of 16 total)
  • Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @guardiano78

    Try the following:

    function filter_woocommerce_cart_product_not_enough_stock_message( $message, $product_data, $stock_quantity ) {
        // New message
        $message = __( 'You are currently trying to order more of this product than are currently available at your shipping location. Please call our sales team to discuss availability', 'woocommerce' );
        
        return $message;
    }
    add_filter( 'woocommerce_cart_product_not_enough_stock_message', 'filter_woocommerce_cart_product_not_enough_stock_message', 10, 3 );

    Cheers!

    Thread Starter guardiano78

    (@guardiano78)

    Hi @rynald0s
    thank you.
    I wrote your code in my theme child functions.php, but it still doesn’t work ??

    I am going crazy.

    greetings.

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @guardiano78.

    Try changing the priority:

    function filter_woocommerce_cart_product_not_enough_stock_already_in_cart_message( $message, $product_data, $stock_quantity, $stock_quantity_in_cart ) {
        // New message
        $message = __( 'You are currently trying to order more of this product than are currently available at your shipping location. Please call our sales team to discuss availability', 'woocommerce' );
        
        return $message;
    }
    add_filter( 'woocommerce_cart_product_not_enough_stock_already_in_cart_message', 'filter_woocommerce_cart_product_not_enough_stock_already_in_cart_message', 10, 4 );

    It produces the following for me on the individual product page: https://snipboard.io/Ev597C.jpg

    If that doesn’t work for you, then it could be that your theme or another plugin is causing conflict and in which case I would recommend you temporarily switch to another default WordPress theme like Storefront, and disable all your plugins except for WooCommerce core, ad then re-add the code to see if that makes a difference. If the issue goes away, it was indeed a conflict and you will need to re-enable your plugins until it breaks again, then contact the author of the conflicting plugin.

    We have documentation on how to perform the conflict test here:

    https://woocommerce.com/document/how-to-test-for-conflicts/

    Cheers!

    Thread Starter guardiano78

    (@guardiano78)

    Hello,
    i did a clean wordpress installation, i installed storefront:
    https://snipboard.io/e3hOHE.jpg

    i only installed woocommerce:
    https://snipboard.io/2QIdFD.jpg

    i created the hook in functions.php:
    https://snipboard.io/HYSfZ8.jpg

    I added to cart a product, reduced the stock and i gone to cart:
    https://snipboard.io/m5nPOA.jpg

    The message is not overwritten. I don’t know what else to do ??

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @guardiano78.

    Sometimes it takes just clearing your browser cache / hard refresh on the page. Please go ahead and give that a try.

    Cheers!

    Thread Starter guardiano78

    (@guardiano78)

    Hi,
    i already empty browser cache. I also tried with another browser that never used localhost

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @guardiano78.

    As previously mentioned, change the filter. You are still using, which is passing 3 args when there’s 4 args:

    add_filter( 'woocommerce_cart_product_not_enough_stock_already_in_cart_message', 'filter_woocommerce_cart_product_not_enough_stock_already_in_cart_message', 10, 3 );

    Change that to:

    add_filter( 'woocommerce_cart_product_not_enough_stock_already_in_cart_message', 'filter_woocommerce_cart_product_not_enough_stock_already_in_cart_message', 10, 4 );

    Cheers!

    • This reply was modified 2 years, 5 months ago by Rynald0s.
    • This reply was modified 2 years, 5 months ago by Rynald0s.
    • This reply was modified 2 years, 5 months ago by Rynald0s.
    Thread Starter guardiano78

    (@guardiano78)

    Hello,
    unfortunately it does not work, I can not understand where I am wrong.
    Let’s start from the beginning.
    In the file “class-wc-cart.php” starting from line 789 I find this code:

    if ( apply_filters( 'woocommerce_cart_item_required_stock_is_not_enough', $product->get_stock_quantity() < ( $held_stock + $required_stock ), $product, $values ) ) {
    				/* translators: 1: product name 2: quantity in stock */
    				$error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s available). We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity() - $held_stock, $product ) ) );
    				return $error;
    			}

    If i replace “$product->get_name()” with “$product->get_sku()”, I achieve my goal.
    Now I have to create the hook for this message.
    Searching on google I find this:

    add_filter( 'woocommerce_cart_item_required_stock_is_not_enough', 'wp_kama_woocommerce_cart_item_required_stock_is_not_enough_filter', 10, 3 );
    function wp_kama_woocommerce_cart_item_required_stock_is_not_enough_filter( $has_stock, $product, $values ){
    
    	// filter...
    	return $has_stock;
    }

    It should work … but it doesn’t work.
    So I replace:
    add_filter( 'woocommerce_cart_item_required_stock_is_not_enough', 'wp_kama_woocommerce_cart_item_required_stock_is_not_enough_filter', 10, 3 );
    with :
    add_filter( 'woocommerce_cart_item_required_stock_is_not_enough', 'wp_kama_woocommerce_cart_item_required_stock_is_not_enough_filter', 10, 4 );
    but it still doesn’t work.
    New wordpress installation, with only woocommerce and storefront theme …. there is something that escapes me.

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @guardiano78.

    Ah, I totally missed that.

    With that said, it’s going to require much more additional code than what is already readily available, to be honest.

    There’s already code to replace the title with the SKU, as seen here https://stackoverflow.com/questions/55881710/replace-product-title-by-the-sku-in-woocommerce-shop-cart-and-checkout-pages but that will replace the title on all products in the cart (if a SKU is set), and not only under the condition where the stock message is displayed.

    Is there any particular use case for replacing it only under those conditions, if you don’t mind me asking?

    Cheers!

    Thread Starter guardiano78

    (@guardiano78)

    Hello,

    yes, there is a reason.
    The products on the site have a long title, so to avoid problems, when viewing, I created a limit of 50 characters.
    In addition in the cart, I have activated a snippet for viewing the sku.
    So it happens that if there are products in the cart that have similar names except for the last word, with my character limit, the titles look the same, except for the sku. Then replacing the sku at the title for the cart messages would have been useful for the user to identify the product that has stock problems.

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @guardiano78

    Gotcha. Thanks for clarifying.

    I had a deeper look at the filter in question and found that the output on the individual product page isn’t the same as the cart page output (reference from https://github.com/woocommerce/woocommerce/blob/9db2c452905ebf796a8e8b4d63db64b57e169dbe/plugins/woocommerce/includes/class-wc-cart.php#L757), so overall, I couldn’t get it to work either. When using the code, the stock error displays correctly for individual products, but not for cart pages.

    As it stands, I found that it is not possible to filter messages in the function check_cart_item_stock() inside class-wc-cart.php unless a filter is added to woocommerce_cart_item_required_stock_is_not_enough. This is something that has already been reported here https://github.com/woocommerce/woocommerce/issues/33114

    With that new information, it might make things easier once that is resolved, but for now, there’s no other way to do this other than replacing the titles for all products with SKUs in the cart and checkout pages.

    Cheers!

    Thread Starter guardiano78

    (@guardiano78)

    Hello @rynald0s ,
    thank you very much for your help, I think I will give up for the moment and look for any new hooks in the new versions of woocommerce.

    Bye

    Mirko P.

    (@rainfallnixfig)

    Thanks for letting us know.

    I’ll mark this thread as resolved for now. If you have any further questions, I recommend creating a new thread.

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @guardiano78

    You’re very welcome.

    In the meantime, as an alternative workaround you can also consider just adding the SKU in addition to the title (just below the title), so at least it would still show up. Something like this:

    function echo_sku_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
    
        $product = $cart_item['data'];
        $sku = $product->get_sku();
    
        if ( empty( $sku ) ) 
            return $item_name;
    
        if ( is_cart() ) {
            $item_name = $item_name . '<br><small class="product-sku">' . '<span class="sku-title">' . __( "SKU: ", "woocommerce") . '</span>' . $sku . '</small><br>';
        } else {
    		$item_name = '<small class="product-sku">' . $sku . '</small>' . $item_name;
        }
    
        return $item_name;
    }
    
    add_filter( 'woocommerce_cart_item_name', 'echo_sku_in_cart_items', 10, 3 );

    It will output like so: https://snipboard.io/ETJ2Ox.jpg

    Cheers.

    Thread Starter guardiano78

    (@guardiano78)

    Hi @ rynald0s

    I already did.
    My needs are to display the sku instead of the product title in the error messages for “insufficient stock”.
    This way, the user will be able to identify the product more quickly.

    Thank you.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Replace product title with product sku’ is closed to new replies.