onBeforeCartUpdate in HIkashop 3?

  • Posts: 128
  • Thank you received: 2
7 years 7 months ago #267772

-- HikaShop version -- : 3.0.1
-- Joomla version -- : 3.6
-- PHP version -- : 5.6

Hi there,

I'm finally testing Hikashop version 3 and a plugin I developed for Hikashop 2.6.4 does not seem to be working in HIkashop3.

According to the documentation all the triggers should still work in V3, right? However my onBeforeCartUpdate function does not get called and it works fine on V2.6.4

Is there an issue with this trigger?

I have seen other posts where it is suggested to replace onBeforeCartUpdate by onBeforeCartSave, is that the solution? The developper documentation does not mention onBeforeCartSave and keeps referencing onBeforeCartUpdate. What parameters do you get with onBeforeCartSave?

Many thanks

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

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
7 years 7 months ago #267774

Hello,

That's right, there is no more "onBeforeCartUpdate" because the Cart::update function is now a transitional function and is "deprecated" in some ways.
It is highly possible that cart can be updated via the new functions and it would mean that the trigger will not be called.

So the usage of the trigger "onBeforeCartSave" will be more appropriated if you want to control the data when the cart is saved (for any kind of modification) ; otherwise there are some triggers in order to control the quantities ( onAfterProductCheckQuantities )
It will depends on what you want to control/check exactly.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

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

  • Posts: 128
  • Thank you received: 2
7 years 7 months ago #267831

Ok, my plugin is preventing the user from adding to the cart products with incompatible features ( this feature is a product custom field and I have my rules to check for this). It is working fine in HK 2.6.4

So, is onBeforeCartSave the best trigger to achieve this in HK3?

Can you please let me know what are the parameters that onBeforeCartSave receives?

Many thanks

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
7 years 7 months ago #267833

Hi,

Yes, totally. I've updated the developer documentation with the information regarding that trigger:
www.hikashop.com/support/documentation/6...umentation.html#cart

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

  • Posts: 128
  • Thank you received: 2
7 years 7 months ago #267913

Ok thanks, I managed to re-write my plugin

The following user(s) said Thank You: Jerome

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

  • Posts: 27
  • Thank you received: 4
7 years 2 months ago #277037

I noticed that inside onBeforeCartSave, you are only given the $element and $do parameters, but they do not give any extra information on whether a product was added, quantity added/subtracted, or removed.

In the onBeforeCartUpdate function, this was easy to see by the parameters $add and $product_id.

Is there any other way to know which action was taken besides having to keep track of the cart contents?

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
7 years 2 months ago #277041

Hi,

You have the trigger onAfterProductCheckQuantities(&$products, &$cart, $options) which is called after the checks have been done on whether the product can be added to the cart, and before the call to the save of class.cart to store the change in the database (where onBeforeCartSave is called).
Also, note that since the add to cart is always done following an action of the user, you can also look in $_REQUEST for the different variables to know the product_id and quantity of the product being added.

The following user(s) said Thank You: fengel

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

  • Posts: 6
  • Thank you received: 0
7 years 2 months ago #279101

I have a problem where onAfterProductCheckQuantities, onBeforeCartSave, and onAfterCartSave causes the add to cart notification to not work, and I dont see the product add to the cart until i refresh the page.

If i remove these triggers from the plugin, everything works find, but if they are there, nothing happens when I click add to cart, except when i manually refresh the page and I can see the product added. Can you help with this?

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
7 years 1 month ago #279160

Hi,

Well, we would need to look at the code you have in these to be able to say anything.

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

  • Posts: 6
  • Thank you received: 0
7 years 1 month ago #279189

Thanks for the reply nicolas. This is what I have so far;

class plgHikashopDump_Events extends JPlugin
{
	function __construct(&$subject, $config) {
		parent::__construct($subject, $config);
	}

	function onProductDisplay(&$element,&$html) {
		echo '<script type="text/javascript">
		    	console.log("onProductDisplay");
		      </script>';
	}

	function onProductFormDisplay(&$element,&$html) {
		echo '<script type="text/javascript">
		    	console.log("onProductFormDisplay");
		      </script>';
	}

	function onProductBlocksDisplay(&$element,&$html) {
		echo '<script type="text/javascript">
		    	console.log("onProductBlocksDisplay");
		      </script>';
	}

	function onCheckoutWorkflowLoad(&$checkout_workflow, &$shop_closed, $cart_id) {
		echo '<script type="text/javascript">
		    	console.log("onCheckoutWorkflowLoad");
		      </script>';
	}

	function onAfterProductCheckQuantities(&$products, &$cart, $options) {
		echo '<script type="text/javascript">
		    	console.log("onAfterProductCheckQuantities");
		      </script>';
	}
	
	function onBeforeCartSave(&$element,&$do) {
		echo '<script type="text/javascript">
		    	console.log("onBeforeCartSave");
		      </script>';
	}

	function onAfterCartSave(&$element) {
		echo '<script type="text/javascript">
		    	console.log("onAfterCartSave");
		      </script>';
	}	
}
The others work except for onAfterProductCheckQuantities, onBeforeCartSave, and onAfterCartSave.
These cause the Add to Cart notification to not show and the shopping cart to not update until you refresh the page.

Last edit: 7 years 1 month ago by Jerome. Reason: [code] tag is really really realllyyyyyyy nice !

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
7 years 1 month ago #279218

Hi,

That's normal. When you click on the "add to cart" buttons with the "add to cart legacy" setting deactivated, HikaShop's javascript sends an AJAX request to the server to process the operation on the server and waits for the answer from the server in the form of a json structure with data about the results of the operation and potential error messages to display.
And when HikaShop receives that request on the server, it process it and call the plugin triggers and finally returns back the result.
If you echo HTML in these triggers you basically mess with the return result which isn't anymore a json structure as there now is HTML before the json. Hence, the javascript can't understand anymore the result of the operation and can't update the cart modules or display the notification popup.

So if you want to do some logging of data in these triggers, you should rather write your text in a file on the server, like we do with the payment methods, using the hikashop_writeToLog($data) function and the setting "payment log file" of the HikaShop configuration. In fact, you can use that function in your code and look at the log in the HikaShop configuration if you want.

The following user(s) said Thank You: hatchlinks

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

  • Posts: 6
  • Thank you received: 0
7 years 1 month ago #279259

That makes sense.
Can you please tell me where I can find the code that is called when the AJAX request is sent to the server.
What I ultimately want to do is run FaceBook pixel code when the user clicks on the add to cart button, I thought I could accomplish this with the Hikashop event trigger plugin.

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
7 years 1 month ago #279264

Hi,

In that case, your best bet is to modify the HikaShop cart notification plugin to make your own notification plugin instead of using the default one of HikaShop and add the HTML/JS you want in the popup notification of that custom plugin. So you want to look in /plugins/hikashop/cartnotify/

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

  • Posts: 6
  • Thank you received: 0
7 years 1 month ago #279469

I'm not sure what you mean create my own notification plugin. Couldn't I just add the code somewhere in the notification pligin itself to run the facebook pixel code?

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

  • Posts: 6
  • Thank you received: 0
7 years 1 month ago #279471

From the notification plugin, is there a way to get the product that just added?

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

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
7 years 1 month ago #279470

Hello,

Modifying the HikaShop core or the core plugins is not something we can recommend since you will loose all your modifications with the next update.
So no, you'd better implement your own custom plugin to be sure that it will continue to work in the future. So basically, make a copy of it, change its name and modify it to suits your needs.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.
Last edit: 7 years 1 month ago by nicolas.

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

  • Posts: 6
  • Thank you received: 0
7 years 1 month ago #279510

Ok I see. I think I can agree with that.
Can you help me to figure out how to retrieve the item that was just added to the cart within the notification plugin?

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

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
7 years 1 month ago #279513

Hello,

The notification plugin is mostly adding Javascript code in the page and all the information about the added item are sent to the Javascript function as its arguments.
If you have a more precise question ; we would be able to answer it but actually, your question is too vaste to allow us to give you a precise information.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

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

  • Posts: 231
  • Thank you received: 28
  • Hikashop Business
6 years 7 months ago #290649

Hi!
I don't know why, but this trigger is not working in my plugin, i don't get nothing in console log.
am I doing something wrong?

jimport('joomla.plugin.plugin');

class plgHikashopSeniorerp extends JPlugin {

    public function __construct(&$subject, $config = array()) {
        // call parent constructor
        parent::__construct($subject, $config);
    }

    function onBeforeCartSave(&$product, &$do) {
        echo '<script type="text/javascript">
                console.log("on Before Cart Save");
              </script>';
    }

}

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 7 months ago #290657

Hi,

If you use the new add to cart system, then the add to cart is done in AJAX and the server returns a JSON with a specific set of data to the browser.
So you can't ouput HTML like that, it just won't be added to the page and it might well even break the javascript handling the add to cart.

What you can do potentially is add an exit; after the echo in your code and use the developer tools' network feature of your browser to look at what is returned by the server for the XHR (AJAX) request sent when you click on the add to cart button.
developers.google.com/web/tools/chrome-d...network-performance/

The following user(s) said Thank You: oxido

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

Time to create page: 0.120 seconds
Powered by Kunena Forum