Its possible to remove a line item from cart (or dont add the line item) trough rules if the site date is greater than product date?

I'm stuck on action.

Event:
 Before adding a product to the cart
Conditions:
 
Entity has field
Parameter: Entity: [commerce-product], Field: field_date

Data comparison
Parameter: Data to compare: [site:current-date], Operator: is greater than, Data value: [commerce-product:field-date]

Action: Dindt find... (looking for something similar to: remove line item / dont add line item)

Comments

PedroMiguel’s picture

Component: Cart » Rules integration
rszrama’s picture

Status: Active » Fixed

Yep. In this case you'd want to use the event "Calculating the sell price of a product." Add a rule that checks the date, and if the product is no longer purchasable, it should use the action "Set the unit price to some amount" to set the price of the product to NULL - i.e. don't put anything in there, not even 0. Products with NULL prices aren't purchasable and will be removed from the Cart.

Status: Fixed » Closed (fixed)

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

jdelaune’s picture

Status: Active » Closed (fixed)

Sorry to re-open this. I'm using the action "Set the unit price to a specific amount" but the value field is a mandatory field. So I can't leave it blank to set the value to NULL.

Is there a better way to do this?

jdelaune’s picture

Status: Closed (fixed) » Active
rszrama’s picture

Ahh, yes, use the "Set a data value" action instead of the line item action. Then you can target the line item's amount column only and leave the value box empty.

jdelaune’s picture

Hmm I've tried setting both 'commerce-line-item:commerce-unit-price:amount' and 'commerce-line-item-unchanged:commerce-unit-price:amount' to nothing but it still doesn't remove it from the cart. Am I setting the wrong thing?

PedroMiguel’s picture

There is mine to you have an example:

{ "rules_remove_after_date" : {
    "LABEL" : "Remove after date",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_product_reference" ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "NOT data_is_empty" : { "data" : [ "commerce-line-item:line-item-id" ] } },
      { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } },
      { "entity_has_field" : {
          "entity" : [ "commerce-line-item:commerce-product" ],
          "field" : "field_date"
        }
      },
      { "data_is" : {
          "data" : [ "commerce-line-item:commerce-product:field-date" ],
          "op" : "\u003c",
          "value" : [ "site:current-date" ]
        }
      }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "commerce-line-item:commerce-unit-price:amount" ] } }
    ]
  }
}

Please note field_date is a costum field.

This work perfect.

jdelaune’s picture

Strange. This is what I have, and the code is returning true because the message gets displayed. However setting the amount to nothing doesn't seem to remove the item from the cart.

{ "rules_only_one_banner" : {
    "LABEL" : "Only One Banner",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "commerce_order", "rules", "commerce_product_reference" ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "commerce_order_contains_product" : {
          "commerce_order" : [ "commerce-line-item:order" ],
          "product_id" : "5060059402342",
          "operator" : "\u003e",
          "value" : "1"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "You cannot add more than one free banner to your shopping cart at a time",
          "type" : "error",
          "repeat" : 0
        }
      },
      { "data_set" : { "data" : [ "commerce-line-item:commerce-unit-price:amount" ] } }
    ]
  }
}
jdelaune’s picture

nvm I found a way around the problem using something else.

PedroMiguel’s picture

@jdelaune, please share your way, can help others...