Is there a way to add one or more products to a product display via Services?

I can list the products, but on a new product display, I do not need to list the products that are part of a display--there aren't any.

I need to ADD a product to the display.

Comments

DrupalWoody created an issue. See original summary.

DrupalWoody’s picture

Status: Active » Closed (won't fix)

This can be done with rules.

1.) Add a new field to your Product Display. For my project, I used "Item Number."

2.) Add a new field to your Commerce Product variations For my project, I used "Parent Item."

3.) Add new commerce product variations, and make sure to set a Parent Item that matches the Item Number you'll use in the Product Display.

4.) Set up a rule to run on the event "After updating existing content of type Product Display," or "After saving new content of type Product Display." Depending on your workflow needs, you may want the rule to run after both events.

5.) Add any conditions you need to restrict the rule, so that it only runs based on your workflow needs.

6.) Add an action "Fetch Entity by Property."

7.) Set the entity type to "Commerce Products," and then set the Property to "Parent Item." Set the Value data selector to "node:field-item-number." Remove the number from "Limit Result Count" so that ALL your product variations that have the right "Parent Item" setting get used.

8.) Add a loop to your rule. Set the List data selector to "entity-fetched." It won't matter how many entities get fetched, the loop will handle all of them.

9.) Add "Add an item to a list" to the LOOP in the rule. Set the List data selector to "node:field-item-type," or whatever your Product Reference field is called. Set the Item to add data selector to "list-item." Set "enforce uniqueness" to "True." Set "Insert Position" to whatever your workflow needs are.

10.) Save the rule.

Always add your commerce products FIRST. Once you're finished adding them (you can add them in REST API also), then you can add your corresponding product display. If it works, your new product display will automatically contain your new commerce products.

If this does NOT work:

1.) Go to "Structure > Content Types > Product Display" and pick "Manage Fields."

2.) Change the Widget for your "Product reference" field to "Autocomplete text field."

3.) Save your product display content type.

I tested this method, and it worked. A product display t-shirt with sizes running "S," "M," "L," "XL," "2X," and "3X" had lost its commerce products due to a glitch I made when updating products via REST API. I used my item as a guinea pig and edited the "Body" field of the Product Display.

When the product display finished saving, I looked at it in my storefront, and the "Add to Cart" button had reappeared with my sizing options the way I needed them to appear.