Cart not updating anymore in plugin

  • Posts: 105
  • Thank you received: 6
  • Hikamarket Multivendor Hikashop Business
1 month 5 days 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: 1 month 5 days ago by Rixters.

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

  • Posts: 21738
  • Thank you received: 3402
  • MODERATOR
1 month 4 days 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: 105
  • Thank you received: 6
  • Hikamarket Multivendor Hikashop Business
1 month 3 days 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: 1 month 3 days ago by Rixters.

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

  • Posts: 60468
  • Thank you received: 8524
  • MODERATOR
1 month 3 days 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: 105
  • Thank you received: 6
  • Hikamarket Multivendor Hikashop Business
1 month 2 days 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: 60468
  • Thank you received: 8524
  • MODERATOR
1 month 2 days 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: 105
  • Thank you received: 6
  • Hikamarket Multivendor Hikashop Business
3 weeks 5 days 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: 21738
  • Thank you received: 3402
  • MODERATOR
3 weeks 5 days 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.

Time to create page: 0.094 seconds
Powered by Kunena Forum