Jump to content

[Solved] Editing a repeater field, but values aren't updated


Harmen
 Share

Recommended Posts

Hi All,

You need a short introduction for this. The company I am working for has approx. 80 products on their website and they all have their own features. The features are imported from an older system which isn't used anymore. Back then, when they changed from the old system to PW, we imported the features into the product pages as a JSON-array so the pages could load a bit faster as exploding a JSON array is a bit faster than loading in a lot of items from a table or a repeater field.

How the JSON array looks like: 

{
    "2": { 										// The ID of the group that contains the actual features
        "name": "Model and function",			// The name of the group
        "features": {
            "43": {								// Attribute ID
                "name": "Resolution (DPI)",		// Attribute name
                "values": {						
                    "896": "500-1500-2000-3500" // Value ID and value name
                }
            }
        }
    }
}

Now we are adding a few more products to the catalog with new features and some that are already in use by other products but I am really struggling to assign the right ID's with the correct values in 7 different languages without messing up filters etc. So I decided to develop a module that lets you easily add feature groups, attributes and values as pages and connect these to the product using a repeater. This started by exporting all the current groups, attributes and values and import them as pages in the following structure:

- Features
	- Feature Groups
		- Group 1
		- Group 2
		- ...
	- Feature Attributes
		- Attribute 1
		- Attribute 2
		- ...
	- Feature Values
		- Value 1
		- Value 2
		- ...

Secondly, I created a repeater field that I assigned to the 'Features'-page that can handle the JSON structure explained above by seeing each item of the repeater field as a group. Inside each item you can select the group page and you will find another repeater which contains 2 page selectors to select an attribute and a value. A single repeater field looks like this:

756090769_Schermafbeelding2018-11-26om16_24_04.thumb.png.2407971d0fb8ac9830fcc492449c02ec.png

Next step was to create a module that gets this field as an Inputfield from the Features page. Once the user has finished adding groups, attributes and values he can click on save and I export all the values in the same JSON array structure as earlier but now the ID's of the groups, attributes and values are just the page ID's. This works great to add new features to new products. 

BUT, sometimes the features of a product change or need to be changed and you don't want to change the JSON array manually.

So my idea was to do the same thing as adding the features, but now you grab the values from the product first and populate the items of the repeater field.

$feature_groups = json_decode($product->features, true);
if (is_array($feature_groups) || is_object($feature_groups)) {
	foreach ($feature_groups as $feature_group_id => $feature_group) {
		$groupPage = wire('pages')->get($feature_group_id);
		if ($groupPage) {
			$row = $page->features_repeater->getNewItem();
			$row->feature_group_selector = $groupPage;
			foreach ($feature_group['features'] as $feature_id => $feature) {
				$featurePage = wire("pages")->get($feature_id);
				if ($featurePage) {
					$valuePage = wire('pages')->get(key($feature["values"]));
					if ($valuePage) {
						$feature_row = $row->feature_repeater->getNewItem();
						$feature_row->feature_attribute_selector = $featurePage;
						$feature_row->feature_value_selector = $valuePage;
					} else {
						continue;
					}
				} else {
					continue;
				}
			}
			$row->save();
		} else {
			continue;
		}
	}
}

Then the user can change / add features, click save and done!

Now the issue is that I can't detect any changes made to the repeater field! So when the user clicks the save button I execute the following code:

private function processUpdateFeatures(InputfieldForm $form){
        $form->processInput($this->input->post);
        if (count($form->getErrors())) return false;
		
		$feature_items = $form->get('features_repeater')->value;
		foreach ($feature_items as $feature_item){
            $group = $feature_item->feature_group_selector;
            if ($group["id"] != 0){
                $groupPage = wire('pages')->get($group["id"]);
                $this->message($groupPage->title);

                $features = $feature_item->feature_repeater;
                foreach ($features as $feature){
                    $attribute = $feature->feature_attribute_selector;
                    $value = $feature->feature_value_selector;

                    $attributePage = wire('pages')->get($attribute["id"]);
                    $valuePage = wire('pages')->get($value["id"]);

                    $this->message($attributePage->title);
                    $this->message($valuePage->title);
                }
            }
        }
    }

But the only thing that the messages show me is the old values and not the new values. What am I doing wrong here or am I missing a step? 

Anything that points me in the right direction is greatly appreciated!

~Harmen

Edited by Harmen
Solved the issue
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...