Jump to content

single-page-shop question - update specific repeater field where another field matches


Can
 Share

Recommended Posts

Hey guys,

I struggled about half a week, okay wasn´t the only thing I´m on..but I´m not getting it.

I´m working on a little page with a little shop, just a few books. Of course I know Apeisas shop, but it doesn´t support tax out of the box right now and I want to have the whole shop with cart and checkout on a single page and I had half of it already working with jquery but wanted to have something more scalable. While trying to get it on my own I´m digging more into php and PW  :)

Blabla

I have a template called order. My plan is to store the cart in order page like "Cart 12.5.2014" and name it to "Order 12.5.2014" when checkout is completed.

The choosen cart items are stored in a repeater "order_items" with fields "order_qty" and "order_product" (page fieldtype)

Everything works fine. First time the customer is adding an item to the cart the system will create the page and add the product to the repeater. I´m storing an individual id in the session which will be the name (path) of the page so I can check if one is already existing and just add more items to it.

Once the customer is ready and fills out the order form, the page gets updated his name and stores the address information and the id in the session gets deleted so the order is kind of locked.

The only problem is, I´m not able to update the item list properly.

Either each time the customer adds item 1 to the cart it creates a new repeater

or it changes only the qty of the first and only repeater even when he adds another item to the cart

Because I´m not a 100% sure what I´m doing it´s more like trial and error.

This is my last working code where it´s updating the same repeater item every time

if($input->post->addtocart) {
	
	$sid = $session->sid;
	$order = $pages->get("name=$sid");
	$order_items = $pages->get("name=$sid")->order_items;

	if(!$sid) {
		$order = new Page();
	        $order->parent = $pages->get("/bestellung/");
	        $order->template = 'order';
		$order->title = 'Warenkorb '.date("d.m.Y - G:i:s");
		$session->set('sid', str_shuffle(session_id())); //str_shuffle to prevent problems when customer finishes an order and want to make another order
		$order->name = $session->sid;
		$order->save;
	}

	if(count($order_items)) {
		foreach($order_items as $items) {
			if($items->order_product = $form_addtocart[item]) {
				$items->of(false);
				$items->order_qty += $form_addtocart[qty];
				$items->save();
				break;
			}
		}
	}
	else {
		$order->of(false);
		$order_items = $order->order_items->getNew();
		$order_items->order_qty		= $form_addtocart[qty];
		$order_items->order_product = $form_addtocart[item];
		$order_items->save();
		$order->save();
	}
} // end addtocart

On the one hand, I´m understanding what happens. On the other I have no idea what´s going on.

Maybe there are a lot of improvement possibilities on my code haha.

Uh, I thought about customizing the events fieldtype to store the items, but I thought repeaters are easier for the beginning. Maybe tailored events field will be the next version of the shop.

I really hope there is an easy solution for this "little" thing.

Maybe I missed the right search terms.

Cheers and a nice evening to all :-)

EDIT:

Forgot to mention, that I know, that the "if(count($order_items))" is not a good way, because once there is an item in the cart it´s true

And I already tried in_array() and array_search() but couldn´t get it working :-/

Edited by Can
Link to comment
Share on other sites

if($items->order_product = $form_addtocart[item]) {

Above may not work as you need:

if($items->order_product == $form_addtocart[item]) {

Doube equals sign instead of single equal sign. The rest should be working as expected unless I am missing something  :rolleyes:

Link to comment
Share on other sites

Thanks peterfoeng :-)

I think I already tried =, == and === and only with 1 = it´s updating, but as I mentioned before everything gets summed to one item no matter if it´s the right one.

With 2 or 3 it´s not getting true?!  ??? So nothing happens.

EDIT

May I need to get the repeater page first to update it something like that

1. if(count($order->order_items)) {
2.   foreach($order->order_items as $items => $item) {
3.	$update_cart = $pages->get($item->get('id'));
4.	$content .= $item->order_product->title;
5.	if($update_cart->order_product = $form_addtocart[item]) {

I don´t know, but what I really don´t understand is that line 4. gives me the right title (or ID of course) so it should only match if it´s the same

but as I said before, it´s counting everytime on the same product in the cart no matter what I add?!

I still think it should be something small because of my lack of knowledge...hopefully :-)

Cheers

Edited by Can
Link to comment
Share on other sites

Got it working myself with this one

	$updated = 0;
	
	foreach($order->order_items as $item) {
		if ($item->order_product == $form_addtocart[item]) {
			$item->of(false);
			$item->order_qty += $form_addtocart[qty];
			$item->save();
			$updated = 1;
			break;
		}
	}	
	if($updated === 0){
		$order->of(false);
		$order_items = $order->order_items->getNew();
		$order_items->order_qty	= $form_addtocart[qty];
		$order_items->order_product = $form_addtocart[item];
		$order_items->save();
		$order->save();
	}

Why not editing my post or marking as solved? Hope to get some improvement tips, if there are any (what I guess).

I´m studious  :lol:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...