Stop add to cart based on rules

Created on 25 June 2012, over 12 years ago
Updated 5 April 2023, over 1 year ago

So I'm trying to use the event 'before adding a product to cart' to check (1) the sku of the product being added (2) the quantity of products in the commerce order (3) the sku's already within the commerce order. If the conditions return true then I wan't to stop the 'add to cart' process.

In the actions I've set a 'show a message on the site' so I know the conditions are working correctly. I've also added a 'set data value' where I set quantity to 0. But this does nothing. I'm surprised there isn't a condition where you can raise an exception which bombs out the whole process. Surely on an event like 'before adding a product to cart' you expect to be able to raise a 'no'.

Unfortunately I don't really know anything about rules or how commerce integrates with it otherwise I would try and write something.

Can anyone shed any light on the logic of this? Is this a reasonable request?

Thanks

πŸ’¬ Support request
Status

Active

Version

1.0

Component

Rules integration

Created by

πŸ‡¬πŸ‡§United Kingdom jdelaune

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States MegaphoneJon

    [I apparently just made a commit via a comment? Someone please remove that.]

    I'm a little late to this party, but if anyone else has this issue, here's some code I wrote to remove a line item from an order. Use this with the "After saving a new commerce line item" event. I wish you could prevent it being added on presave but the requisite data isn't available at that time.

    function wrltweaks_rules_action_info() {
      return [
        'wrltweaks_remove_line_item_from_order' => [
          'label' => t('Remove item from order'),
          'group' => t('Commerce Line Item'),
          'parameter' => [
            'commerce_line_item' => [
              'type' => 'commerce_line_item',
              'label' => t('Line item'),
            ],
            'commerce_order' => [
              'type' => 'commerce_order',
              'label' => t('Order'),
            ],
          ],
        ],
      ];
    }
    
    function wrltweaks_remove_line_item_from_order($lineItem, $order) {
      $lineItemId = $lineItem->line_item_id;
      commerce_cart_order_product_line_item_delete($order, $lineItemId, TRUE);
    }
    
Production build 0.71.5 2024