Add or Delete Products or Items to/from Cart in onAfterCartProductsLoad(&$cart)

  • Posts: 73
  • Thank you received: 0
7 years 3 months ago #274979

-- url of the page with the problem -- : www.sokart.ch
-- HikaShop version -- : HikaShop Business 3.1.1 [1707191036]
-- Joomla version -- : 3.7.4
-- PHP version -- : 7.1
-- Browser(s) name and version -- : Firefox 54.0.1 (32-Bit)

Hello

You helped me to develop one plugin and it don't work with new HikaShop Version!
OLD: onAfterCartUpdate(&$cartClass,&$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force)
...
$cartClass = hikashop_get('class.cart');
$cartContent = $cartClass->get();
...
// remove item
$cartClass->updateEntry(0,$cartContent,(int)$product->cart_product_id,0,false,'item',$force);
$cartClass->mainProduct = null;
// add product
$cartClass->updateEntry('1',$cartContent,self::PROD_DIENSTL_ONHE_PROD,'1',false,'product',$force);
...

Function updateEntry(...) seems to be obsolette now, how may I rewrite it in "onAfterCartProductsLoad(&$cart)"?

Thank you in advance!
PR

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

  • Posts: 73
  • Thank you received: 0
7 years 3 months ago #274982

Hello

Is this OK?

...
$cartClass = hikashop_get('class.cart');
$cartContent = $cartClass->get();
...
foreach($cart->products as $product) {
...
// Delete item
$cartClass->update((int)$product->cart_product_id, 0, 0, 'item' );
// Add product
$cartClass->update((int)self::PROD_DIENSTL_ONHE_PROD, 6, 1, 'product' );
...
}

Do I need anything else in the initialisation?

Regards,
PR

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

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

Hello,

The updateEntry function was an internal function only used by the class.
Despite the function "update" was the general entry point and that function got the retrocompatibility support.

I would most recommend you to use the new function such as "addProduct" which has been design to work as an API. You can see how the function "update" of the cart class is working and how it call it (with what data).
The structure is an array with the product id and the quantity.
Afterwards there is the function updateProduct which work almost the same but it requires the cart_product_id and a quantity.

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: 73
  • Thank you received: 0
7 years 3 months ago #275017

Hello

Thank you for your answer.

addProduct() and updateProduct() does the job ...

Despite them I have anther problem:

- When I remove or add products in cart in function onAfterCartProductsLoad() I need additional site refresh to see the changes.
Info: Product counter in the cart is still the same after calling addProduct() or updateProduct().

- When I use onAfterCartSave() for remove or add products then I got recursion and the site hangs.

With old HikaShop version
$cartClass->mainProduct = null;
has helped to refresh the cart and show changes immediatelly.

So, what is the right way?

Regards,
PR

Last edit: 7 years 3 months ago by sokartch. Reason: clarification

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

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

Hello,

1 - Sure, the trigger give you a direct access to the cart object when you're loading the cart ; if you do not touch the given object, it won't affect it.

2- Yes, using "onAfterCartSave" you can create a loop if you do not perform specific checks to avoid it.

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: 73
  • Thank you received: 0
7 years 3 months ago #275092

Hi Jerome,

Sorry for my simple English!

I know that that you wrote. This explains but do not helps.

How may I immediately refresh Card view, without reload the page?
Product counter in the cart is still the same after calling addProduct() or updateProduct().

Following code seems to not change products list in $cart parameter!
---
public function onAfterCartProductsLoad(&$cart) {
$cartClass = hikashop_get('class.cart');
$cartContent = $cartClass->get();
...
echo count($cart->products); // bellow
$cartClass->updateProduct($cart->cart_id,$proarr);
echo count($cart->products); // same as bellow
...
}
---

Regards,
PR

Last edit: 7 years 3 months ago by sokartch.

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

  • Posts: 82868
  • Thank you received: 13376
  • MODERATOR
7 years 3 months ago #275097

Hi,

As Jerome said, you need to use onAfterCartSave and not onAfterCartProductsLoad as it won't work properly.

And in onAfterCartSave, you of course need to check if the product you're trying to add is not already in the cart.
If it is, then you should not call updateProduct. If you do, that will create a loop because your call to updateProduct will trigger again onAfterCartSave.

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

  • Posts: 73
  • Thank you received: 0
7 years 3 months ago #275185

Hi

That not really helps, I must have possibility to remove or to add products to the cart.

I may place those code to own plugin but where I may safely trigger it? Probably in cart. view?

Regards,
PR

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

  • Posts: 82868
  • Thank you received: 13376
  • MODERATOR
7 years 3 months ago #275189

Hi,

You say it doesn't help, but you don't say why ?
What I wrote in my previous message do allow you to add and remove products to the cart.
So I don't see why it doesn't help. Please provide more information.
You can add and remove products not only from a plugin but also from a view. However, if you do it in the view, the page being displayed won't get the modification since you'll be modifying the cart after most of the data has already been loaded / displayed.
And with the ajax refresh of the new checkout, it's more complex to ask for a full page refresh ora refresh of the different blocks on the page as you can't just do a redirect to the current page like it was possible with the old checkout.

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

  • Posts: 73
  • Thank you received: 0
7 years 3 months ago #275237

Hi Nicolas,

It's always a problem how many information is enough.

1. We need to remove and add products from and to cart. We make engravings and depending from added engraving we add one additional product for design and review. When customer removes engraving we must also remove automatically added product for design and review.

2. Implementing those PHP code in former mentionend onAfterCartSave() and onAfterCartProductsLoad() events leads to recursions or is "to late" for cart view.

3. I thought that there is one point in one view (cart?) where it's would be possible with own plugin. May you give me one hint?
We need one point where our plugin got $cart parameter, that may be changed and later displayed. Our plugin uses updateProduct() and addProduct() functions but if you think we must use another functions, no problem.

Regards,
PR

Last edit: 7 years 3 months ago by sokartch. Reason: Clarification

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

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

Hello,

During the trigger "onAfterCartProducsLoad" you have a direct access to the cart object while it is being loaded.
If you want to add or remove products and not have to interact with the current loaded cart ; you need to attach your code to a different "event".

And there is no reason to get a loop (or recursion) if your code made the appropriate checks.
You do not need to remove a product from the cart if it's not there.
You do not need to add a product into the cart if they are already there.

And more than that, some function are made to handle the "direct edition".
For example the updateProduct function, if you give the cart object instead of the cart_id ; the function will update the cart object and not perform a save in the database (that you'll need to do if the function gives you a "true" in return).
The addProduct function do not have the same feature because it's not necessary (but I'll explain it below).

The old cart system was loading too much the carts while the new system have a cache which allow to improve performances.
I'm really sorry that it generates side effects for you but please understand that we can't provide you code like that if we don't have the knowledge what you're doing exactly. So I can't even give you an advice for the trigger to use.
Our support also have its limits while it comes to customization.

Now.

In HikaShop 3 we wanted to add new triggers.
But in order to be able to use it ; you need to be sure to get the latest package because I made a little fix to split the new triggers correctly.
The one that you will want to use is :

onAfterProductCheckQuantities( &$products, &$cart, $options )
Which is called to check the cart integrity when products are added or updated.
Via the variable $products you can change the quantity (0 to remove the product) and also add new entries.

It's a really good entry point to interact with cart products while the cart is being modified.
And I would be more confident to change the cart products there than while the cart is being loaded.

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: 73
  • Thank you received: 0
7 years 3 months ago #275370

Hello

Our support also have its limits while it comes to customization.
I understand that, but from my point of view HikaShop developer documentation is to short and simple. I'm software developer for 30 years and I have had so much problems to do very simple things. Searching in source, that's not a good approach to find information...

Probably I did not readed some documentation, which part?

You wrote:
---
onAfterProductCheckQuantities( &$products, &$cart, $options )

Which is called to check the cart integrity when products are added or updated.
Via the variable $products you can change the quantity (0 to remove the product) and also add new entries.
---
1. $products contains allways only one product "count($products)" od var_dump. For example: Where is $products array documented?
2. $cart contains no products "count($cart->products)".

Simple: I need to access all actual products in the cart, remove or add products. Is it possible, on high level, with actual HikaShop Version?

Regards
PR

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

  • Posts: 82868
  • Thank you received: 13376
  • MODERATOR
7 years 3 months ago #275376

Hi,

1. No, $products contains all the products that are being added/modified to the cart at the same same.
When you click on the "add to cart" button for one product, you'll only have one product in that array, but it's potentially possible to have several products in there, hence why it's an array.

2. $cart doesn't always contain the products in that trigger because it depends on the flow of the code where you're doing your testing and if your cart is a new cart being created or not.
However, you'll have $cart->cart_id so you can simply run a MySQL query in that trigger in order to load the products of the cart from the hikashop_cart_product table or even better use the get($cart_id) function of class.cart in order to load the products of the cart:
$classCart = hikashop_get('class.cart');
$cartData = $cartClass->get($cart->cart_id);
You should have the current products in $cartData->cart_products (not counting the product that is being added to the cart during the call of that trigger of course.

It's not complex code to write. It just requires using triggers in a careful way compared to before because the system's cache of the cart has been highly tuned for efficiency and ajax support and thus it's easy to mess it up if you're not using the correct function calls at the correct place.

Here is a code sample for you with the onAfterProductCheckQuantities trigger Jerome talked about :

public function onAfterProductCheckQuantities( &$products, &$cart, $options ){
 $classCart = hikashop_get('class.cart');
 $cartData = $cartClass->get($cart->cart_id);
 $addProductXXX = false;
 $removeProductAAA = false;
 foreach($products as $productBeingAdded){
  foreach($cartData->cart_products as $productAlreadyInCart){
   // do your processing to know if you want to add or remove your product and set the variables $addProductXXX and $removeProductAAA accordingly
  }
 }
 if($addProductXXX)
  $products[] = array('id'=>XXX, 'qty'=>YYY);
 if($removeProductAAA)
  $products[] = array('id'=>AAA, 'qty'=>BBB);
}
The system will take care of adding your product XXX and removing your product AAA automatically along with the product being added/modified/removed from the cart. Now you just need to adapt the code to your needs as we don't know what you want to change in $productBeingAdded and $productAlreadyInCart.

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

  • Posts: 73
  • Thank you received: 0
7 years 3 months ago #275395

Thank you again

Hint: in your code is:
$classCart = hikashop_get('class.cart');
$cartData = $cartClass->get($cart->cart_id);

After another two hours of... This code did not delete products:

public function onAfterProductCheckQuantities( &$products, &$cart, $options ) {
$cartClass = hikashop_get('class.cart');
$cartData = $cartClass->get($cart->cart_id);
foreach($cartData->cart_products as $item) {
if ($this->is_in_category($item->product_id,self::CAT_GENER_PRODUKTE)) {
//$products[] = array('id'=>$item->cart_product_id,'qty'=>0);
$products[] = array('id'=>$item->product_id,'qty'=>0);
}
}
}

Another:
...
The system will take care of adding your product XXX and removing your product AAA automatically along with the product being added/modified/removed from the cart.
...
Function call happens only for Ajax modification !!!???!!! not for adding or removing products.

Installed version: HikaShop Business 3.1.1 [1708031110]

Regards,
PR

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

  • Posts: 82868
  • Thank you received: 13376
  • MODERATOR
7 years 3 months ago #275418

Hi,

Sorry for the typo...please understand that I'm not testing the code here. I'm only giving hints for something we're not supposed to help with as part of the user support...

The qty variable is the qty that you want to add to the cart. If you set it to 0, it means that you don't want the quantity of the product in the cart to change. You should set it to -1 to remove 1 item of that product in the cart.

that function is called each time a product is added or removed from the cart, regardless of how you add or remove it (ajax or not). Even with the legacy add to cart it will go through it.

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

  • Posts: 73
  • Thank you received: 0
7 years 3 months ago #275437

Hi

1.
Jerome wrote:
Via the variable $products you can change the quantity (0 to remove the product) and also add new entries.
Nicolas wrote:
The qty variable is the qty that you want to add to the cart. If you set it to 0, it means that you don't want the quantity of the product in the cart to change. You should set it to -1 to remove 1 item of that product in the cart.

2.
When I use:
---
public function onAfterProductCheckQuantities( &$products, &$cart, $options ) {
$products[] = array( 'id' => 869, 'qty' => 1 ); // product
}
---
It adds product, but!
a) Only when new another products are added from the product page with "Add to Cart".
b) Every added product is one separate product with 1 piece, not same product with 1 then 2 then 3 pieces (discount!!!)
c) When I change products account in Cart (Ajax) then product is not added.
d) When I remove an product from cart (X button) then product is not added.
e) When I change 'qty' => -1 nothing happens, no product is removed!

Regards
PR

Last edit: 7 years 3 months ago by sokartch.

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

  • Posts: 73
  • Thank you received: 0
7 years 3 months ago #276655

Hello

- I bought HikaShop in the hope that it is customizable. So far so good.

- You helped me to implement my idea in older version (2.x). So far, many thanks. Note: I was not informed that it was about a fast draft.

- With new version (3.x) this has not worked. Then you gave me controversial suggestions for the solution !!! And then let me drop !!!

- We are, as a company, very affected by this behavior. A fast CHANGE of the concept is not possible. Our shop is not working for 3 weeks !!!

- This will be a fierce comment in Joomla Extensions.

Best Regards
Petar Ristanovic
www.sokart.ch

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

Time to create page: 0.086 seconds
Powered by Kunena Forum