Jump to content
Can

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

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

Share this post


Link to post
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:

Share this post


Link to post
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

Share this post


Link to post
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:

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By David Karich
      ProcessWire InputfieldRepeaterMatrixDuplicate
      Thanks to the great ProModule "RepeaterMatrix" I have the possibility to create complex repeater items. With it I have created a quite powerful page builder. Many different content modules, with many more possible design options. The RepeaterMatrix module supports the cloning of items, but only within the same page. Now I often have the case that very design-intensive pages and items are created. If you want to use a content module on a different page (e.g. in the same design), you have to rebuild each item manually every time.
      This module extends the commercial ProModule "RepeaterMatrix" by the function to duplicate repeater items from one page to another page. The condition is that the target field is the same matrix field from which the item is duplicated. This module is currently understood as proof of concept. There are a few limitations that need to be considered. The intention of the module is that this functionality is integrated into the core of RepeaterMatrix and does not require an extra module.
      Check out the screencast
      What the module can do
      Duplicate multible repeater items from one page to another No matter how complex the item is Full support for file and image fields Multilingual support Support of Min and Max settings Live synchronization of clipboard between multiple browser tabs. Copy an item and simply switch the browser tab to the target page and you will immediately see the past button Support of multiple RepeaterMatrix fields on one page Configurable which roles and fields are excluded Configurable dialogs for copy and paste Duplicated items are automatically pasted to the end of the target field and set to hidden status so that changes are not directly published Automatic clipboard update when other items are picked Automatically removes old clipboard data if it is not pasted within 6 hours Delete clipboard itself by clicking the selected item again Benefit: unbelievably fast workflow and content replication What the module can't do
      Before an item can be duplicated in its current version, the source page must be saved. This means that if you make changes to an item and copy this, the old saved state will be duplicated Dynamic loading is currently not possible. Means no AJAX. When pasting, the target page is saved completely No support for nested repeater items. Currently only first level items can be duplicated. Means a repeater field in a repeater field cannot be duplicated. Workaround: simply duplicate the parent item Dynamic reloading and adding of repeater items cannot be registered. Several interfaces and events from the core are missing. The initialization occurs only once after the page load event Attention, please note!
      Nested repeaters cannot be supported technically. Therefore a check is made to prevent this. However, a nested repeater can only be detected if the field name ends for example with "_repeater1234". For example, if your MatrixRepeater field is named like this: "content_repeater" or "content_repeater123", this field is identified as nested and the module does not load. In version 2.0.1 the identification has been changed so that a field ending with the name repeater is only detected as nested if at least a two-digit number sequence follows. But to avoid this problem completely, make sure that your repeater matrix field does NOT end with the name "repeater".
      Changelog
       
      2.0.1
      Bug fix: Thanks to @ngrmm I could discover a bug which causes that the module cannot be loaded if the MatrixRepeater field ends with the name "repeater". The code was adjusted and information about the problem was provided 2.0.0
      Feature: Copy multiple items at once! The fundament for copying multiple items was created by @Autofahrn - THX! Feature: Optionally you can disable the copy and/or paste dialog Bug fix: A fix suggestion when additional and normal repeater fields are present was contributed by @joshua - THX! 1.0.4
      Bug fix: Various bug fixes and improvements in live synchronization Bug fix: Items are no longer inserted when the normal save button is clicked. Only when the past button is explicitly clicked Feature: Support of multiple repeater fields in one page Feature: Support of repeater Min/Max settings Feature: Configurable roles and fields Enhancement: Improved clipboard management Enhancement: Documentation improvement Enhancement: Corrected few typos #1 1.0.3
      Feature: Live synchronization Enhancement: Load the module only in the backend Enhancement: Documentation improvement 1.0.2
      Bug fix: Various bug fixes and improvements in JS functions Enhancement: Documentation improvement Enhancement: Corrected few typos 1.0.1
      Bug fix: Various bug fixes and improvements in the duplication process 1.0.0
      Initial release Support this module
      If this module is useful for you, I am very thankful for your small donation: Donate 5,- Euro (via PayPal – or an amount of your choice. Thank you!)
      Download this module (Version 2.0.1)
      > Github: https://github.com/FlipZoomMedia/InputfieldRepeaterMatrixDuplicate
      > PW module directory: https://modules.processwire.com/modules/inputfield-repeater-matrix-duplicate/
      > Old stable version (1.0.4): https://github.com/FlipZoomMedia/InputfieldRepeaterMatrixDuplicate/releases/tag/1.0.4
    • By arnd
      Hi,
      I have a repeater with some images in it.
      As Superuser I can work with the Actions (rotate...). But my Users with lower Rights can't use this Actions.
      The same User can use the Actions on images outsite a Repeter-Field. So I think it's not a Problem of Rights-Management but from Images in Repeater Fields.
      ProcessWire 3.0.164.  Bug? Or can I manage this in the settings?
      Thanks in advance
    • By Robin S
      This is a module I made as an experiment a while ago and never got around to releasing publicly. At the time it was prompted by discussions around using Repeater fields for "page builder" purposes, where the depth feature could possibly be used for elements that would be nested inside other elements. I thought it would be useful to enforce some depth rules and translate the depth data into a multi-dimensional array structure.
      I'm not using this module anywhere myself but maybe it's useful to somebody.
      Repeater Depth Helper
      This module does two things relating to Repeater fields that have the "Item depth" option enabled:
      It enforces some depth rules for Repeater fields on save. Those rules are:
      The first item must have a depth of zero. Each item depth must not be more than one greater than previous item depth. It provides a RepeaterPageArray::getDepthStructure helper method that returns a nested depth structure for a Repeater field value.
      Helper method
      The module adds a RepeaterPageArray::getDepthStructure method that returns a multi-dimensional array where the key is the page ID and the value is an array of nested "child" items, or null if there are no nested children.
      Example

      The module doesn't make any assumptions about how you might want to use the depth structure array, but here is a way you might use it to output a nested unordered list.
      // Output a nested unordered list from a depth structure array function outputNestedList($depth_structure, $repeater_items) { $out = "<ul>"; foreach($depth_structure as $page_id => $nested_children) { $out .= "<li>" . $repeater_items->get("id=$page_id")->title; // Go recursive if there are nested children if(is_array($nested_children)) $out .= outputNestedList($nested_children, $repeater_items); $out .= "</li>"; } $out .= "</ul>"; return $out; } $repeater_items = $page->my_repeater; $depth_structure = $repeater_items->getDepthStructure(); echo outputNestedList($depth_structure, $repeater_items);
       
      https://github.com/Toutouwai/RepeaterDepthHelper
      https://modules.processwire.com/modules/repeater-depth-helper/
    • By ICF Church
      Hi 👋
      Anyone else having this problem?
      Requirements:
      - Repeater (matrix & normal) with mutlilanguage fields (text, textarea…) 
      - Backend language set to something other than default (ie. German) 
      Reproduce:
      - Add a new repeater Item (ajax, I found no way to possible to disable it with matrix)

      (Notice how the default language tab is active instead of the backend language…)
      - Write something into the (default language) field
      - Try to save, if field is required, this will not work. If not required, then when reloading, the content will be inside the backend language field, instead of the default language field who was (presumably) active
      Analysis:
      When  loading  a new repeater element with ajax, the default langue tab is active, but the backend language inputfield is visible (with no visual indication). When writing into the field, it will populate the backend language. When manually clicking on the default language tab (which is already active), the field will switch to the actual default language field (which is [now] empty) (that can now be populated…)
      Also Notice, the labels of the elements to be added are in default language as well instead of the translated label (images instead of Bilder)…
      ProcessWire 3.0.148, Profields 0.0.5…
      Is it my system configuration, or does anyone else have the same issue? This is a screen recording of the problem:
      Issue: https://github.com/processwire/processwire-issues/issues/1179

      Screen Recording 2020-02-25 at 14.18.31.mov
    • By neonwired
      I have a front end form for creating new pages, repeater and repeater matrix field don't seem to save any data. I was considering handling the data manually but can't seem to get anything useful from the post data, are there any methods i can use?
×
×
  • Create New...