Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

E-Commerce Modules /

Discuss about the various e-commerce modules available:
Ecommerce, SS Shop, SilverCart and SwipeStripe
Alternatively, have a look the shared mailinglist.

Moderators: martimiz, Nicolaas, Sean, Ed, frankmullenger, biapar, Willr, Ingo, Jedateach, swaiba

Update the quantity of every item in the cart


Go to End


3 Posts   1354 Views

Avatar
Fraser

Community Member, 48 Posts

5 June 2012 at 10:17am

Hi,

In the application I am building (relating to travel), the user adds a number of items to their basket and prior to checkout is prompted for how many guests it relates to so I need to update the number of items of everything in their cart.

Is it possible to modify:

function setquantityitem($request) {
		$quantity = $request->getVar('quantity');
		$product = $this->buyableFromURL();
		if (is_numeric($quantity) && $product) {
			$item = ShoppingCart::get_item($this->urlFilter());
			if($quantity > 0){
				if(!$item){
					if($item = self::create_order_item($product,$quantity,self::get_clean_param_array($this->getRequest()->getVars()))){
						$item->Quantity = $quantity;
						self::add_new_item($item);
					}
				}
				else{
					ShoppingCart::set_quantity_item($item, $quantity);
				}
			}elseif($item){
				ShoppingCart::remove_all_item($item);
				return self::return_data("success","Item removed completely");//TODO: i18n
			}
			return self::return_data("success","Quantity set successfully");//TODO: i18n
		}
		return self::return_data("failure","Quantity provided is not numeric");//TODO: i18n
	}
	
	static function set_quantity_item($existingitem, $quantity) {
		if ($existingitem) {
			$existingitem->Quantity = $quantity;
			$existingitem->write();
		}
	}

To update the quantity of every item in the users cart?

Avatar
Jedateach

Forum Moderator, 238 Posts

5 June 2012 at 10:58am

Hi Fraser,

Can you specify which module you are using for ecommerce? I'm managing the shop module, which it doesn't look like you are using. I'll try to offer some advice anyway.

Certainly with some development effort, you'll be able to achieve what you're wanting to.

Personally, I would try keeping this non-standard functionality separate. I've looked inside my ShoppingCart code for my shop module, and noticed I don't have extension points to make changes when a quantity is updated for example. Introducing such functionality would allow external code like this:

class TravelOrder extends Extension{

    function afterSetQuantity($item,$buyable,$quantity,$filter){

        foreach($this->owner->Items() as $orderitem){$item->Quantity = $quantity;}

    }
}

What do you think?

For your reference, here is my ShoppingCart class, without these changes: https://github.com/burnbright/silverstripe-shop/blob/master/code/ShoppingCart.php

regards,
Jeremy

Avatar
Fraser

Community Member, 48 Posts

5 June 2012 at 11:25am

Hi Jeda,

Thanks for the response.

I'm using the Ecommerce module.

I created a subclass of the Shopping Cart class and am making all my modifications in here.

Cheers