First off, great module, this is exactly what we needed for our client, so thanks bojanz.

Summary of Problem

I needed to have an order form on the product page that shows each product variation on it's own line with a quantity field and an add to cart button. To start, I created a page display view to test things out which I got working correctly. I was able to add any number of any product variation to the cart using the page display of the view. I was redirected to the same page (as expected) and was shown the add to cart confirmation popup.

I then created a block display so I can have this order form on the individual product pages. To limit the product variations to only those related to the product being viewed, I created a contextual filter of the Nid and provided a default value (since this is a block display, there is no source for contextual filters) of "Content ID from URL".

The block displays correctly, however, the form action is incorrect, instead of using the aliased URL of the current page as I would expect, the form action was set to "/node/nid/nid". When the add to cart button was clicked, the page refreshed, and I was brought to the product page at "/node/nid/nid" instead of the aliased version of the page that I clicked "Add To Cart" on. I was not shown the add to cart confirmation popup, and my cart is empty. I have attached the view export if it would be helpful to see.

Proposed Solution

I searched through the issue queue and identified this patch that enables the block display, and the use of views_embed_view. While looking at the code I was able to identify the following line as the issue:

$form['#action'] = url($view->get_url(), array('query' => $query));

Which updates the form action by using the views method get_url and appending any query string parameters if they were present. The problem is that the $view->get_url() is returning the above mentioned url ("/node/nid/nid").

The fix for me was to use the request_uri() function to get the aliased URL for the page current being viewed. I switched the above line with this:

$form['#action'] = url(trim(request_uri(),'/'), array('query' => $query));

I am using trim to remove the leading (and trailing if present) slash. Once this switch was made, my block display view began working correctly. I am not sure if there are any unknown effects of using the request_uri instead of the $view->get_url().

EDIT: The above does not work correctly when there are special characters in the URL, so I have switched to using

$form_action = drupal_get_destination();
$form['#action'] = url($form_action['destination'],array('query'=> $query,'html' => TRUE));

The above returns the correct path "node/nid".

To Do

For my purposes this appears to work just fine, I will be playing around with it some more over the next couple of days and will update if I find any issues with the switch.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

T.Mardi’s picture

I can confirm this change works.

Before changing the line, if I left quanties at zero and hit 'add to cart' I was getting redirected to "/node/nid/nid". Now it shows the correct url of the node being viewed.

Thanks.

gdarends’s picture

I seem to have the same issue.
My setup is as follows:
I created a block view.
Then I used viewfield to attach the view to the product display content type.

Is it not safe to always set the action, instead of only setting it specifically for block and views_embed_view()?
In my case I also get the same issue, but my case does not meet the conditional block. ($view->override_path is not empty) This then does not set the action, but still getting node/nid/nid.

gdarends’s picture

Also, I'm not sure what the optional html => TRUE in the array is for, but I haven't seen anything in the url api documentation about it.

gdarends’s picture

Status: Needs review » Active
FileSize
1.05 KB

I propose the following patch. Which should work regardless of wether it's a page view or block/views_embed_view().
Was tested as a page view and also in my case as a block view attached to a node type using viewfield module.

gdarends’s picture

Issue summary: View changes

Updating with latest insight into issues using request_uri().

gdarends’s picture

Status: Active » Needs review

Status: Active » Needs review
mglaman’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
1.02 KB

Fixes issue. Attaching re-roll so it applies to latest dev.

coatezy’s picture

Any idea when this patch might be merged?

joelpittet’s picture

Status: Reviewed & tested by the community » Needs work

Seems when you have filters added to your view, this patch will break the form URL.

Knocking it back to needs work.

+++ b/commerce_add_to_cart_extras.module
@@ -29,14 +29,11 @@ function commerce_add_to_cart_extras_form_alter(&$form, &$form_state, $form_id)
+    $form['#action'] = url($form_action['destination'], array('query'=> $query));

I think it's because form_action['destination'] includes the previous query arguments.

mglaman’s picture

Status: Needs work » Needs review
FileSize
988 bytes

To be honest I'm not sure why I didn't just use current_path. This should fix it.

maxplus’s picture

Hi,
#10 works great for me, my URL now keeps the same after a add to cart action.
Before patch #10, my URL always got longer and longer after every add to cart action...

joelpittet’s picture

@maxplus, would you mind setting the status to RTBC(reviewed and tested by the community) if you've tested it and it's working. I'll gladly commit this.

maxplus’s picture

Status: Needs review » Reviewed & tested by the community

  • joelpittet committed bdfae1a on 7.x-1.x authored by mglaman
    Issue #1985340 by mglaman, gdarends, skadu, joelpittet, maxplus:...
joelpittet’s picture

Status: Reviewed & tested by the community » Fixed

Thanks i've pushed to to -dev.

Status: Fixed » Closed (fixed)

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