1

I have a rule

Redirect to cart after adding a registration

this rule works exactly as planned.


New scenario and new rule, If the user empties their cart of all products, I want to redirect to previous page (the sending page).

New rule

Redirect user to originating page after removing all items from cart

I have the Event and the Conditions but I don't see how to massage the originating address that was used to get to the cart. The originating form action is <input type="submit" id="edit-submit" name="op" value="Add to cart" class="form-submit">.

{ "rules_redirect_user_to_originating_page_after_removing_all_items" : {
    "LABEL" : "Redirect user to originating page after removing all items from cart",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "Cart" ],
    "REQUIRES" : [ "rules", "commerce_cart" ],
    "ON" : { "commerce_cart_product_remove" : [] },
    "IF" : [ { "data_is_empty" : { "data" : [ "quantity" ] } } ],
    "DO" : []
  }
}

Can anyone help?

  • do you have cart block that it's available on every page ? where you want implement this scenario , in '/cart` page? – Yusef Mar 25 '17 at 12:09
1

In your first rule "Redirect to cart after adding a registration" you probably are using a "Page redirect" action. If so you can do the following.

Edit this action and set "APPEND DESTINATION PARAMETER" to true (see screenshot). This will add the referring page as a query parameter to your url e.g. example.com/cart?destination=node/3

Then in your second rule you can retrieve this destination parameter by using the function drupal_get_destination. Use the return value of that function in the drupal_goto function and the user will be redirected to the url they were coming from.

{ "rules_redirect_user_to_originating_page_after_removing_all_items" : {
    "LABEL" : "Redirect user to originating page after removing all items from cart",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "Cart" ],
    "REQUIRES" : [ "php", "rules", "commerce_cart" ],
    "ON" : { "commerce_cart_product_remove" : [] },
    "DO" : [
      { "php_eval" : { "code" : "drupal_goto (drupal_get_destination());" } }
    ]
  }
}
  • this definitely worked and I learned an advanced aspect to Rules. Is there any security issue in displaying this publicly? Technically it is within the commerce context of the site. – fndtn357 Mar 25 '17 at 12:25
  • Is there any security issue in displaying this publicly? What do you mean by this? – undersound Mar 25 '17 at 12:39
  • If I can see the path in the address bar are there any hackable parts or ways to hack the address for any evil intentions? just paranoid ... – fndtn357 Mar 25 '17 at 21:01
  • drupal_goto() calls url() which does sanitizing but always double check for yourself :) api.drupal.org/comment/59556#comment-59556 – undersound Mar 26 '17 at 20:24
  • This solution worked too well. After registration and approving a product in the cart, the reroute kicked me back to the previous page. Got tons of notice from the Pantheon site audit about bad habits and having PHP format active. So ... I am working on something different for now. I appreciate the reminder though about making sure I bring what I need into scope for getting Rules access to it. – fndtn357 Mar 27 '17 at 11:21
1

using php_eval and drupal_goto is not good solution ( although you can get drupal_get_destination there, but if user remove lineitems on /cart what happened ? again redirect to empty cart page ) I suggest Use Page Redirect to Url Action and redirect user to your front page or your products page.

"DO" : [ { "redirect" : { "url" : "<front>" } } ]
  • but if user remove lineitems on /cart what happened? Could you explain this in more detail as I'm curious of what I am overlooking here. thnx – undersound Mar 25 '17 at 12:37
  • @undersound if user be in /cart path and remove all line items ,after all items removed this rule triggered and condition satisfied but he/she redirect to /cart :|. where do you want to navigate him/her when remove all items form his/her cart in /cart page ? – Yusef Mar 25 '17 at 12:55
  • yeah I think I know what you mean, although in your second explanation it looks like you are talking about the same two uses cases. I was re-reading the question from the OP though and playing with it a bit more (adding multiple products) and then indeed, if you add multiple products it will be a problem where you want to redirect the user to, the first or the second product for instance. OP is also talking about redirecting to cart after registration so I'm not entirely sure if one or multiple products get added in that process. – undersound Mar 25 '17 at 19:48
  • @undersound my suggestion is if cart will getting empty after removing card event, redirect user to /porducts or front page.( I mean a fix page) – Yusef Mar 28 '17 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.