{ "rules_update_product_display_for_existing_commerce_product" : {
    "LABEL" : "Update Product Display for existing Commerce Product",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "commerce" ],
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_product_update" ],
    "DO" : [
      { "entity_query" : {
          "USING" : {
            "type" : "node",
            "property" : "field_product",
            "value" : [ "commerce-product:product-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "product_display" : "Fetched entity" } }
        }
      },
      { "data_set" : {
          "data" : [ "product-display:0:title" ],
          "value" : [ "commerce-product:title" ]
        }
      },
      { "data_set" : {
          "data" : [ "product-display:0:author" ],
          "value" : [ "commerce-product:creator" ]
        }
      },
      { "entity_save" : { "data" : [ "product-display:0" ] } }
    ]
  }
}

i want to update a field in product display node by updating related product.

but in rules i can't acces to custom fields.

i have read this: http://www.drupalcommerce.org/faq/rules-field-data-selection but there are not entity type "product display" to set in conditions.

any advice?
thanks.

Comments

rszrama’s picture

Status: Active » Fixed

Yeah, there is no direct connection from the product to its display, because theoretically it can be referenced from any number of sources. However, if you know you have a one product -> one node setup, then you can use the Rules action to "Fetch entity by property" to load a node referencing your product so long as you know the name of the field used to reference the product from the node. Then you can update it to your heart's content. : )

dgastudio’s picture

thank u for your help.

Editing action set "Commerce products update" (action set becouse i want to use it in VBO)
add action:

Fetch entity by property
Entity type: node
Property: product (reference link in product display)
Value: product:product-id
Limit result count: 10

Save

Add a new action
Data selector: entity-fetched:0 (1,2,3)

Available fields

entity-fetched:0:nid (Node ID)
entity-fetched:0:vid (Revision ID)
entity-fetched:0:is-new (Is new)
entity-fetched:0:type (Content type)
entity-fetched:0:title (Title)
entity-fetched:0:language (Language)
entity-fetched:0:url (URL)
entity-fetched:0:edit-url (Edit URL)
entity-fetched:0:status (Status)
entity-fetched:0:promote (Promoted to frontpage)
entity-fetched:0:sticky (Sticky in lists)
entity-fetched:0:created (Date created)
entity-fetched:0:changed (Date changed)
entity-fetched:0:author (Author)
entity-fetched:0:author:... (Author)
entity-fetched:0:source (Translation source node)
entity-fetched:0:source:... (Translation source node)
entity-fetched:0:log (Revision log message)
entity-fetched:0:revision (Creates revision)
entity-fetched:0:comment (Comments allowed)
entity-fetched:0:comment-count (Comment count)
entity-fetched:0:comment-count-new (New comment count)
entity-fetched:0:body (The main body text)
entity-fetched:0:body:... (The main body text)
entity-fetched:0:views (Number of views)
entity-fetched:0:day-views (Views today)
entity-fetched:0:last-view (Last view)

my custom fields doesnt appear.

that i'm doing wrong?

P.D. In actions set settings i have:

Data type: Node
Usage: parameter
Data type: commerce product
Usage: parameter

rszrama’s picture

Your custom fields don't appear because of the reasons stated at the FAQ article you linked in the original post. Also, you should use the Rules export feature for your rule instead of copying / pasting from the interface... the format is easier to read in here.

dgastudio’s picture

oh, sorry.

my action set :

{ "rules_commerce_products_update" : {
    "LABEL" : "Commerce products update",
    "PLUGIN" : "action set",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "node" : { "label" : "node", "type" : "node", "parameter" : false },
      "product" : { "label" : "product", "type" : "commerce_product" }
    },
    "ACTION SET" : [
      { "entity_query" : {
          "USING" : {
            "type" : "node",
            "property" : "field_product",
            "value" : [ "product:product-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      }
    ],
    "PROVIDES VARIABLES" : [ "node" ]
  }
}

but in action set, i can't add a condition as specified in FAQ article.

so, that can i do?

frixos12’s picture

subscribing

rszrama’s picture

Ahh, good call. Yeah, I should've picked up on that... duh. >_<

What you need to do is create a Rules component that will act as a little subroutine in Rules. You'd want it to be a full "rule" component, not just conditions or actions. When you set it up, give it a title like "Update the product display node" and make it accept one node parameter. Then you'll be able to add the condition to check for the fields and the action to update them as necessary.

Once you have the component made, it will be available to call as an action from the Rule you've just exported above. Pass in the display node you've loaded, and voila! It should all work. ; )

frixos12’s picture

I was following this issue and i would like to ask some questions.

Lets say i have a term reference field (product categories) in both product display node and commerce product entity.

After updating or saving a commerce product, i want to update product-display : term-refrence field with commerce-product:term-reference field.

As you said, create "rule" component, not just conditions or actions. When you set it up, give it a title like "Update the product display node" and make it accept one node parameter. Then you'll be able to add the condition to check for the fields and the action to update them as necessary.

These conditions and actions should be made inside the component or in the rule that calls this component?

Also if you have unlimited values for your taxonomy term field (checkboxes) how can you pass all the values to the product display :term-reference field??

Thank you

pit_zavra’s picture

Hi kervi i am wondering if you made that rule could you share it please? I tried to make it from your conversation with rszrama , but because i am a newbie i couldn't follow, there was a lot of pieces of code that worked an other that didn't work and i got confused.

dgastudio’s picture

yes, of course.

finally i opted to use php instead of strange conditions and options.

so, i have used php to set field value from fetched entity.

{ "rules_copy_field_value" : {
    "LABEL" : "Copy values from product on update",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "php" ],
    "ON" : [ "node_presave" ],
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "product" : "product" } } } }
    ],
    "DO" : [
      { "entity_query" : {
          "USING" : {
            "type" : "commerce_product",
            "property" : "product_id",
            "value" : [ "node-unchanged:field-product:product-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "data_set" : { "data" : [ "node:title" ], "value" : [ "entity-fetched:0:title" ] } },
      { "data_set" : {
          "data" : [ "node:field-product-desc" ],
          "value" : { "value" : { "value" : "empty", "format" : "full_html" } }
        }
      },
      { "data_set" : {
          "data" : [ "node:field-product-desc:value" ],
          "value" : "\u003C?php if (!empty( $entity_fetched[0]-\u003Efield_product_desc)) print $entity_fetched[0]-\u003Efield_product_desc[\u0027und\u0027][0][\u0027value\u0027]?\u003E\u000D\u000A"
        }
      },
      { "data_set" : {
          "data" : [ "node:field-product-info" ],
          "value" : { "value" : { "value" : "empty", "format" : "full_html" } }
        }
      },
      { "data_set" : {
          "data" : [ "node:field-product-info:value" ],
          "value" : "\u003C?php if (!empty($entity_fetched[0]-\u003Efield_product_info)) print $entity_fetched[0]-\u003Efield_product_info[\u0027und\u0027][0][\u0027value\u0027]?\u003E"
        }
      },
      { "data_set" : {
          "data" : [ "node:field-product-pack" ],
          "value" : { "value" : { "value" : "empty", "format" : "full_html" } }
        }
      },
      { "data_set" : {
          "data" : [ "node:field-product-pack:value" ],
          "value" : "\u003C?php if(!empty( $entity_fetched[0]-\u003Efield_product_pack)) print $entity_fetched[0]-\u003Efield_product_pack[\u0027und\u0027][0][\u0027value\u0027]?\u003E\u000D\u000A"
        }
      },
      { "data_set" : {
          "data" : [ "node:field-product-additional-info" ],
          "value" : { "value" : { "value" : "empty", "format" : "full_html" } }
        }
      },
      { "data_set" : {
          "data" : [ "node:field-product-additional-info:value" ],
          "value" : "\u003C?php if(!empty($entity_fetched[0]-\u003Efield_product_additional_info)) print $entity_fetched[0]-\u003Efield_product_additional_info[\u0027und\u0027][0][\u0027value\u0027]?\u003E"
        }
      }
    ]
  }
}

basically, this code is doing the next thing.

is fetching related product entity, after that, clears the value of product display field and copying the value of product field to product display field.

all this happens on save product display, so i used Admin VBO module with "Save content" option added.

sorry for my english.

pit_zavra’s picture

Thank you very much hope it works on my side :-)

dgastudio’s picture

u are welcome.

pit_zavra’s picture

Sorry do you have enabled any other module for php?
While i try to input this rule i get a "Missing the required module php" error.

pit_zavra’s picture

Also i searched for admin vbo module and the only one i found was admin vbo views.
I think that php error was fixed by enabling php filter.

dgastudio’s picture

While i try to input this rule i get a "Missing the required module php" error.
yes, u have to enable php filter.

Also i searched for admin vbo module and the only one i found was admin vbo views.
yes, u have to install this one and View Bulk Operations module.

pit_zavra’s picture

Thank you

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.