Custom popup message for OnBeforeCartSave

  • Posts: 78
  • Thank you received: 4
  • Hikamarket Multivendor Hikashop Business
4 years 9 months ago #315702

Hello,

I have a onBeforeCartSave trigger and check several things to verify if the order is valid.
How can I change the popup message that is showed if the product could not be added to the cart when I set $do false..
I tried with the default joomla messages, but did not work.

if ($status == 'PlaceNotFound') {
$do = false;
$app = JFactory::getApplication();
$app->enqueueMessage( JText::sprintf( 'CUSTOM EROR MESSAGE FOR THE USER', $status));
return false;
}

Thank you and best regards

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
4 years 9 months ago #315705

Hi,

The add to cart is done in AJAX (with a call to the server by the javascript on the page without reloading the page). However, to display an error message via the Joomla message system requires a reload of the page.
So to do that, you would have to turn off the AJAX system. So in the view file displaying the add to cart button, you would have to remove the "onclick" attribute and it would fallback to adding the product with a full page reload.

However, I would rather recommend that you use HikaShop notification system to display your error message.
You can do that like this:

$cartClass = hikashop_get('class.cart');
$cartClass->addMessage($cart, array(
					'msg' => JText::sprintf( 'CUSTOM EROR MESSAGE FOR THE USER', $status),
					'product_id' => $product_id,
					'type' => 'error'
				));

Please Log in or Create an account to join the conversation.

  • Posts: 78
  • Thank you received: 4
  • Hikamarket Multivendor Hikashop Business
4 years 8 months ago #316049

Many thanks.
This works now.

How can I just get the product that was added to the cart (without looping through the full cart ?)
Can I also find out, why this trigger was called ? E.g. because a product was added or deleted etc ?
I only want to run my code, when a new product was added.

Thank you

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
4 years 8 months ago #316051

Hi,

You have the information of the product_id being added in $_POST. Then, you can just load the product data from the hikashop_product table if you need some data from the product.

The information of what happened can't be known in this trigger.
However, you can use the onBeforeCartSave trigger to store what you have in the cart before in the database and compare it to what you have after.

But if you check the $_POST, you can directly check on the ctrl and task values in it.

If($_POST['ctrl'] == 'product' && $_POST['task'] == 'updatecart') {
 // the code in here will only run when a product is being added to the cart
}
The cart modifications, deletions, etc will have different values in these variables

The following user(s) said Thank You: tgdkere2

Please Log in or Create an account to join the conversation.

  • Posts: 78
  • Thank you received: 4
  • Hikamarket Multivendor Hikashop Business
4 years 8 months ago #316084

Thank you, perfect :-)

Please Log in or Create an account to join the conversation.

Time to create page: 0.056 seconds
Powered by Kunena Forum