• Resolved Luca

    (@screenload)


    When a user in my shop adds an item to his cart, he gets redirected to the cart page where a green message shows which item was just added (ajax is disabled).

    I want to show different information based on the product that was added, but I can’t figure out how to get the id of the product.

    I can’t just use the last item in cart, because that would always show the custom message, even when the product was not just added.

    I can’t use the wc_add_to_cart_message filter because I need to create a new element above the message.

    How could this be done?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Stef

    (@serafinnyc)

    What you’re wanting done is it a bit complex. I’m not aware of any plugins that can help you. You might want to look at hiring a developer and posting your question on Slack or Stack Exchange.
    https://woocommerce.com/customizations

    Thread Starter Luca

    (@screenload)

    Since I’m able to develop myself and just need a simple tip on how to get a single variable, this is a bit overboard.

    I asked on stack exchange, too. Seems that nobody ever had a similar problem before and nobody could help me.

    Luckyly I found a solution (a quite dirty one…)

    function wc_add_to_cart_message_filter($message, $product_id = null) {
    	$message = 'Extra field.</div><div class="woocommerce-message" role="alert">' . $message;
    	return $message;
    }
    add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );

    Results in this.

    Stef

    (@serafinnyc)

    That doesn’t get the specific product you stated you wanted to display for. That will query every product and display the same message.

    Not to mention you would have to add a custom meta box in the backend for each product to show a specific separate message.

    Unless we are misunderstanding your request entirely.

    Thread Starter Luca

    (@screenload)

    It DOES get the product, I can just use $product_id and yes, I have to write custom code.
    That’s what I wanted to do, but I needed that ID to do that in the first place ??

    Stef

    (@serafinnyc)

    Ok. If you say so.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get the item that was just added (PHP)’ is closed to new replies.