Update cart in payment view

  • Posts: 62
  • Thank you received: 0
11 years 3 months ago #117404

After having some users accidentally purchase multiple quantities of the same product I am trying to add some additional business logic to the Payment View which will update the product quantity. We sell software and if somebody is just renewing software with an existing serial number then our new business logic says that the quantity should not be more than 1.

Below is a snip from my current code but prior to this it is already determined that only once product is in the cart. In the case that the quantity on this product is more than one I would like to just update the cart and advise the end user. The code below is partially working by updating the Final Total but not the Product Quantity, Line Total or Total. I assume the issue is that cartClass->update() does not update the orderInfos in this view.

Can you please advise the best approach to update the quantity within the payment view. Payment is part of the last step with the following checkout workflow.
login_address_shipping_coupon,fields,payment_terms_confirm_cart_status,end

foreach($this->orderInfos->products as $product) {
	if($product->cart_product_quantity > 1) {
		$app->enqueueMessage('You may only purchase one product when using an existing serial number. Your product quantity has been updated from '.$product->cart_product_quantity.' to 1.');
		$cartClass = hikashop_get('class.cart');
		$cartClass->update((int)$product->product_id, 1); 
	}
}

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

  • Posts: 12953
  • Thank you received: 1778
11 years 3 months ago #117442

Hi,
If just want your customer to buy a maximum of one product per order, why don't you use the Maximum quantity per order option through your product configuration page ?
In you code your checking all your order products regardless of what product it is and then reduce the quantity if it exceed 1.

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

  • Posts: 62
  • Thank you received: 0
11 years 3 months ago #117460

It is only under certain circumstance that I want to restrict the quantity to 1. The code snip does not show it but I check some order custom field first before trying to update the cart. Otherwise I am happy to sell quantities more than 1.

So yes in the above code it is my intention to reduce the quantity of each product to 1. The problem is it does not seem to update the $this->orderInfos so the view is displaying the incorrect amounts.

Last edit: 11 years 3 months ago by Devine. Reason: Add additional info to clarify intention.

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

  • Posts: 13201
  • Thank you received: 2322
11 years 3 months ago #117541

So you can use the limits, with the limit you can restrict the quantity of one or many product, or products from a category.
The options allow you to define the period of the restriction.

The limits are available via the products menu, the button is in the toolbar.

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

  • Posts: 62
  • Thank you received: 0
11 years 3 months ago #117601

Thanks guys, I appreciate that you are trying to make it easy with a non programmatic approach but I don't think it is going to work. The key point is that I am not trying to limit the quantity or the number of products based on what product/s are in the cart. The only time I want to limit the quantity to 1 is when the user has specified that they are using an existing serial number (order custom field, see condition below). Before the original code snip I provided above there is also the following condition, along with some other plugin calls that I have not shown which confirm that the entered serial number is valid.

if($fields->order_existing_serial == 'yes')
It is only if the users specifies a serial number that we wish to limit the cart. Can you please advise how I can update the cart and all of the payment view values. As I mention earlier the $cartClass->update((int)$product->product_id, 1); is only partially working. It does indeed update the cart but the $this->orderInfos is not updated and therefore the incorrect values are shown in the view.

I know the $cartClass->update() works because I can restart the checkout process and the correct quantities have been updated but I don't want to make the users start the checkout process again if possible, but rather just notify them via a message the the quantities have been updated. This is what my original code snip is trying to achieve but only partially working.

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

  • Posts: 13201
  • Thank you received: 2322
11 years 3 months ago #117640

Hi,

As you say, the cart has the correct values, what you need is potentially to reload the current step.
You can call the url "index.php?option=com_hikashop&ctrl=checkout&task=step&step=1", you have to change the values depending on which step is the check.

And reload the page by calling that url.

The best way, is to create a plugin based on onBeforeCartUpdate, to change the quantities depending on your checks, and it will display the correct quantities directly, you can too display a Joomla notice message.

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

Time to create page: 0.081 seconds
Powered by Kunena Forum