All the products in our system are unique, so they can be bought only once.
the obvious solution would be to go through array of products that were in cart on "after checkout" event and disable them, but this doesn't work for me.
I'm not sure what id should I use to load product entity since only line-items entities are available for Rules, here is my Rule:

{ "rules_disable_invoice_on_checkout_comp" : {
    "LABEL" : "Disable invoice on checkout completion",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_checkout" ],
    "ON" : [ "commerce_checkout_complete" ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : {
            "type" : "commerce_product",
            "id" : [ "commerce-order:commerce-line-items:0:line-item-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "data_set" : { "data" : [ "entity-fetched:status" ], "value" : "0" } }
    ]
  }
}

the rule doesn't seem to work (single product in cart!). Any ideas?

Comments

restyler’s picture

Issue summary: View changes

adding code tag

rszrama’s picture

Status: Active » Fixed

You're trying to load a product using a line item ID. What you really want is commerce-order:commerce-line-items:0:commerce-product:product-id. To get to that data selector, you may need to use an "Entity has field" condition on the line item. But it might not work at all.

Also, I'd probably loop over the line items and use the list item data selector instead of just referencing delta 0. What if the customer orders more than one?

restyler’s picture

Mmm.. I've tried to add condition, but I'm not sure that it is located in the right place:

{ "rules_disable_invoice_on_checkout_comp" : {
    "LABEL" : "Disable invoice on checkout completion",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_checkout" ],
    "ON" : [ "commerce_checkout_complete" ],
    "IF" : [
      { "entity_has_field" : {
          "entity" : [ "commerce-order:commerce-line-items:0" ],
          "field" : "commerce_product"
        }
      }
    ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : {
            "type" : "commerce_product",
            "id" : [ "commerce-order:commerce-line-items:0:line-item-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "data_set" : { "data" : [ "entity-fetched:status" ], "value" : "0" } }
    ]
  }
}

There is still no :commerce-product selector: http://pix.am/GjYG/

I will probably add loops later, I'm not sure that I will allow user to have more than 1 product.

restyler’s picture

Status: Fixed » Active
rszrama’s picture

Status: Active » Fixed

I'd go ahead and do the loop. That should clear it up. This may be related to the longstanding issue in Rules that won't let you access the field data of referenced entities.

Status: Fixed » Closed (fixed)

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

muschpusch’s picture

Status: Closed (fixed) » Active

i want to do the same. Accessing a product in rules when only a line item is available.

you may need to use an "Entity has field" condition on the line item

Which field?

My use case is a shipping rate per product but defined as a field on the product entity

rszrama’s picture

Status: Active » Closed (fixed)

Whatever field it is you're looking for. It'd probably be best if you asked for help in IRC, as I can see this being a running conversation. Otherwise the Node One Rules video tutorials may help you.

rszrama’s picture

Issue summary: View changes

tt

crispiatico’s picture

Issue summary: View changes

This solution not work for me because only the Product variations is not available.
I want that my product is not available or publish after purchase after it was purchased.
How can I do this ?

Thankyou

ziobudda’s picture

@crispiatico: have you find a solution ?

crispiatico’s picture

@ziobudda: no i'm not find a solution :-(