Rules Link Event extends Rules by adding an "link accessed" event - this allows you to create arbitrary links which trigger rules.

For example: you can create a URL shop/buy/% which adds an item to the users cart, applies a discount, then redirects the user to the checkout page.

This module is primarily used by those using Drupal Commerce who need to create custom Add to Cart Links, though it could be expanded to be used in many other applications (Please contribute patches!).

Related Modules

Rules Link (See #1365658: Difference with Rules link module)

Creating an add to cart link

Before following this tutorial be sure that you have Rules link event and Drupal Commerce installed on your site.

Go to admin/config/workflow/rules/links to create a link. For this example enter the following into the text area:
purchase|cart/add/%|Quick add to cart link
Be sure to click "Save Configuration" and then go to admin/config/workflow/rules/reaction/import to import the following rule:

{ "rules_add_to_cart_link" : {
    "LABEL" : "Add to cart link",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "Cart Links" ],
    "REQUIRES" : [ "rules", "commerce_cart", "rules_linkevent" ],
    "ON" : [ "rules_linkevent_purchase" ],
    "DO" : [
      { "commerce_cart_product_add_by_sku" : {
          "USING" : {
            "user" : [ "site:current-user" ],
            "sku" : "[path:last]",
            "quantity" : "1",
            "combine" : 1
          },
          "PROVIDE" : { "product_add_line_item" : { "product_add_line_item" : "Added product line item" } }
        }
      }
    ]
  }
}

Click Import and you're all set to create links throughout your site using cart/add/your_sku to create links that add product to the shopping cart. Be sure to replace "your_sku" with the SKU of the product you would like to have added to the cart when the link is clicked.

Note: You must flush caches before links will work.

Other Examples

Please contribute other examples!

Comments

pfhenley86’s picture

Could someone help me figured out how to use the link after following these steps? Do I just add 'cart/add/your_sku' anywhere on the site and it will add the item to the cart? Right now I have a link on my home page to do 'cart/add/SANTA_LTR_1'. Right now it doesn't add anything to the cart. I am not sure if I am doing it wrong or I am using the wrong SKU machine name. The SKU is SANTA-LTR-1. Is this what I need for [path:last] or do I need the node ID?

pfhenley86’s picture

never mind I got it with cart/add/SANTA-LTR-1