Cart not updating anymore in plugin

  • Posts: 189
  • Thank you received: 11
  • Hikashop Business
6 years 11 months ago #284144

-- HikaShop version -- : 3.2.1
-- Joomla version -- : 3.8.2
-- PHP version -- : 7.x
-- Browser(s) name and version -- : FF

Hi Guys,

In the past I used the following code to update the cart using a plugin:

$class = hikashop_get('class.cart');

// Check if cart total is larger than te plugin minimum price
if ($total > $product_min_price)
{
	// Add the free product
	$class->update($product_id, $quantity);
}
else
{
	// Remove the free product
	$class->update($product_id, 0);
}

This isn't working anymore in the new Hikashop version. Can you tell me the new way to update the cart with the ajax cart activated...?

Thanks in advance!

Last edit: 6 years 11 months ago by Rixters.

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

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
6 years 11 months ago #284146

Hello,

Yes that function is deprecated/obsolete for about a year now.
But you will find "addProduct" and "updateProduct" functions for that kind of job.
Don't hesitate to take a look at the HikaShop product controller to see how the "updatecart" function is working and handling the retro-compatibility.

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: 189
  • Thank you received: 11
  • Hikashop Business
6 years 11 months ago #284305

Hi Jerome,

Could you give me some coding samples (update / delete / add) a product to the cart to get me starting?
That would be of great help.

My plugin ( github.com/RickR2H/hikashop-free-product ) is still working but only the cart module is handling the update. The normal cart component is not changed when products are added or removed...

Regards

Last edit: 6 years 11 months ago by Rixters.

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 11 months ago #284310

Hi,

You can do like that:

$class = hikashop_get('class.cart');
$cart_id = $class->getCurrentId();

// Check if cart total is larger than te plugin minimum price
if ($total > $product_min_price)
{
	// Add the free product
	$class->addProduct($cart_id, array('id' => $product_id, 'qty' => $quantity);
}
else
{
	// Remove the free product
	$class->updateProduct($cart_id, array('id' => $cart_product_id, 'qty' => 0 ));
}
where $cart_product_id is the id of the product entry in the hikashop_cart_product table.
If you don't know it in the code where you are, you need to loop through the $cart->products array to search for it based on the $product_id.

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

  • Posts: 189
  • Thank you received: 11
  • Hikashop Business
6 years 11 months ago #284404

Thanks so much for the info!

I tried the following code, but the cart is not updated when the quantity field is changed:

$class = hikashop_get('class.cart');
$cart_id = $class->getCurrentCartId();

$class->addProduct($cart_id, array('id' => 128, 'qty' => 1));
Any suggestions how to fix?

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 11 months ago #284411

Hi,

What do you mean by "when the quantity field is changed" ? This code will add a product with the id 128 with a quantity of 1 to the cart.
If you have it around some other code, it might be the rest of the code which doesn't work properly and call that code.
We would need more information on the situation and the code you have to provide a suggestion.

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

  • Posts: 189
  • Thank you received: 11
  • Hikashop Business
6 years 11 months ago #284807

The behavior is now that the card module will update using the code without a refresh of the page but the cart component still nee a refresh of the page. We really need you help to fix this probably small problem.

<?php
/**
 * @HikaShop add free product for Joomla!
 * @version	1.0.2
 * @author	rick@r2h.nl
 * @copyright	(C) 2010-2016 R2H B.V.. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.plugin.plugin');
class plgSystemCustom_freeproduct extends JPlugin{
    function plgSystemCustom_freeproduct(&$subject, $config){
		parent::__construct($subject, $config);
	}
	// Call the trigger onBeforeOrderCreate
	function onAfterCartProductsLoad(&$cart)
    {
        // Set variables
        $product_id         = $this->params->get('free_prod_id','');
        $quantity           = $this->params->get('free_prod_quantity','1');
        $product_min_price  = $this->params->get('free_prod_price','0');
        $usecoupon          = $this->params->get('usecoupon','0');
        $free_couponcode    = $this->params->get('free_couponcode','Empty');
        $productClass       = hikashop_get('class.product');
        $productinfo        = $productClass->get($product_id,''); // Load free product info

        // Actions to do when my trigger is called
        // Load free product info to check if product exist
        if (isset($productinfo))
        {
			// Get total from cart
	        $total = $cart->full_total->prices[0]->price_value_with_tax;
            // Instantiate cart class
	        $class = hikashop_get('class.cart');
            // Check to use coupon code
            if ($usecoupon == 1) // Use it
            {
                // Load cart data from Hikashop cart class
                $cartData = $class->loadCart($cart->cart_id);
                //var_dump($cartData);
                // Check if coupon code from the Hikashop cart class
                // is equal to selected coupon code in plugin
                if ($cartData->cart_coupon == $free_couponcode) {
                	// Check if cart total is larger than te plugin minimum price
                    if ($total > $product_min_price)
                    {
                        // Add the free product
                        $class->update($product_id, $quantity);
                    }
                    else
                    {
                        // Remove the free product
                        $class->update($product_id, 0);
                    }
                }
                else
                {
                    // Remove the free product
                    $class->update($product_id, 0);
                }
            }
            else
            {
                // Check if cart total is larger than te plugin minimum price
                if ($total > $product_min_price)
                {
                    // Add the free product
                    $class->update($product_id, $quantity);
                }
                else
                {
                    // Remove the free product
                    $class->update($product_id, 0);
                }
            }
        }
    }
}

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

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
6 years 11 months ago #284813

Hello,

I don't see any reference of "addProduct" in your code and as we said previously, the "update" function is obsolete.
And you can't use the function loadCart neither.

The trigger you're using, "onAfterCartProductsLoad" is not meant to do what you're trying to do.
It's a trigger which is called during the cart loading, just after the product are retrieved from the database. It is not safe to modify the cart content in the database while it's being loaded since the cart modification will invalid the cache and you can generate an infinite loop.

I rather recommend you to use other triggers, like "onAfterCheckCartQuantities" which allow you to remove/limit cart products.
And the trigger "onAfterCouponCheck" would be more appropriate since it's only called when there is a coupon in the cart which need to be check (called via the CartClass addCoupon function).

Afterwards, please understand that what you're asking is custom development.

Yes, we had to modify the Cart class and we had to remove that "update" function ; modifying a product during the loading of the cart was/is generating unwanted side effect since while the cart is being loaded, its content is being modified so the cart is directly invalid and need to be reload, once again (and you can create infinite loop with such behavior ; even in the old system).

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: 131
  • Thank you received: 7
6 years 9 months ago #287384

I'm pretty disappointed with these responses to be honest.

Firstly, providing this kind of discount offer is very common in e-commerce. That Hikashop does not provide this functionality is poor enough, but when you remove a feature from your API which breaks a plug-in like this and you cannot be bothered to provide a working example is not what I'd call great service.

I funded the development of this plug-in to support it's coupon based free product capability. The developer who's last request you completely ignored, has quite rightly pointed out to me that your API documentation is, to put it politely "WEAK". He has now informed me that he has not got the support needed to get this working in Hikashop 3. My clients are in turn very disappointed.

I'd have some sympathy if this was a commercial, paid plug-in but it's something that I and the developer agreed would be released free of charge to anyone in the Hikashop community that wanted to use it. A plug-in that fills a big hole in the capabilities of Hikashop itself.

...and before you suggest going with Acymailing, don't! Acymailing's offering does not work properly, and anyone who uses it to provide this capability is going to find themselves wide open to literally losing money. Happy to provide the reason for this in a PM.

So what do I think should happen? Well, I think that you should put some effort into taking the code available on Github and fixing up so that it uses your undocumented new API to work properly. github.com/RickR2H/hikashop-free-product

Last edit: 6 years 9 months ago by bonzomedia. Reason: Github link added. Typo fixed.

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

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
6 years 9 months ago #287453

Hello,

but when you remove a feature from your API which breaks a plug-in like this

We did not remove any feature in HikaShop's new cart system.

We made a function obsolete yes ; because we have a more organized structure.
Like one function to add a product, one function to modify an item, one to add a coupon, etc.
The "update" was a mix of lot of things and it is still an internal function which has some legacy support.

Nicolas gave some code to explain how the new functions are working.
It's not very complex since there is one function to add a product (requiring a product id) and another to modify a product in the cart (requiring a cart product id).

The plugin was working in HikaShop 2.x only thanks to fact that the cart was reloaded several times in page loading.
Modifying a cart content during the cart loading is illogical and there are other thread in the forum where I already explained that and gave solutions.

Creating such plugin is possible, I already made a similar plugin in custom job and I gave the base points for the implementation..
Now, I can provide hints (and I already did) but the current version of the plugin would require a full rewriting ; it's not something which would require few minutes and the fact that the plugin is free does not change that.

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: 131
  • Thank you received: 7
6 years 9 months ago #287455

Apparently the developer has spent over 6 hours trying to figure out how to rewrite the plug-in and has followed your "hints". In absence of decent documentation detailing how to achieve this, they have concluded that it is impossible. I will PM you for a price to get this work done.

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

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
6 years 9 months ago #287511

Hello,

After a small test, I admit that the trigger "onAfterCouponCheck" is not best.
It does not allow the cart product modification since the cart object would be used afterwards in the "save" of "addCoupon".
But if your developer had an issue with it ; he could say it so we would have made a patch proposal to use a partial cart object in the function "addCoupon".

Be sure that we are aware and open to suggestions for improvements.

Nevertheless, without using the trigger "onAfterCouponCheck", I was able to create a working plugin which extend the coupon capabilities and allow the user to select, in the backend, a product that he want to give for free when using the coupon.
Product which is added into the cart when the coupon is added ; product which is made "free" with a fixed quantity by the plugin and product which is removed automatically if the coupon is removed (or the coupon is removed if the free product is removed).

I know that I'm a HikaShop expert ; since I'm in the core team since 2012 and that I rewrote all the cart and the checkout systems for Hk3.
But it was not impossible.

Like I wrote :

It's a trigger which is called during the cart loading, just after the product are retrieved from the database. It is not safe to modify the cart content in the database while it's being loaded since the cart modification will invalid the cache and you can generate an infinite loop.

I know that I explained it with more details in another thread.
Modifying the cart content during that trigger can result an infinite loop if there is not the appropriate check.

About the documentation ; it is made to explain the triggers since they are the entry points to create plugins.

Regarding the private message ; please check my forum signature :)

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: 26158
  • Thank you received: 4028
  • MODERATOR
6 years 9 months ago #287659

Hello,

I talked with Nicolas and we just released a plugin : "coupon free product".
www.hikashop.com/component/hikashop/prod...on-free-product.html

For the moment the plugin allow to add a product (for free) into the customer cart when using a coupon (cf my previous message).
Depending the feedback, that plugin could continue to evolve and provide other kind of features to extend the coupon capabilities.

And regarding of the feature to add a free product depending the cart total ; it's something which can be done using the "autoload coupon" feature of commercial editions of HikaShop.

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.
The following user(s) said Thank You: bonzomedia

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

  • Posts: 131
  • Thank you received: 7
6 years 9 months ago #287660

Jerome,

Many thanks for this. We should now be able to look at this and properly figure out how to make our plug-in work for us.

For our purposes, we'd need to use our current business model which is that the coupon has to be applied to the cart manually. We don't always want every customer to have access to the free offer.

Thanks once again for listening - I'm sure that other Hikashop users will find this very useful.

Last edit: 6 years 9 months ago by bonzomedia.

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

Time to create page: 0.097 seconds
Powered by Kunena Forum