This did work fine, but now when adding an item to the cart, I get the following notices and the 'sku' becomes empty.

Notice: Undefined index: default_product in _commerce_product_urls_build_query_string() (line 148 of /home/marvind/public_html/edsonintl/sites/all/modules/commerce_product_urls/commerce_product_urls.module).
Notice: Trying to get property of non-object in _commerce_product_urls_build_query_string() (line 148 of /home/marvind/public_html/edsonintl/sites/all/modules/commerce_product_urls/commerce_product_urls.module).

chain-and-wire-kit?sku=2S15B22
becomes
chain-and-wire-kit?sku

I am using Commerce Product URLs 7.x-1.2 with Commerce 7.x-1.11

I did some digging around and found that the first line in the notes for commerce 7.x- 1.11 is the removal of 'default_product' from the cache. The patch unsets the 'default_product'.

Here is part of their patch and I assume unset($form_state['default_product']); is the problem.

 /**
+ * After build callback for Add to Cart forms
+ */
+function commerce_cart_add_to_cart_form_after_build(&$form, &$form_state) {
+  // Remove the default_product entity to mitigate cache_form bloat and performance issues.
+  if (isset($form_state['default_product'])) {
+    $form_state['default_product_id'] = $form_state['default_product']->product_id;
+    unset($form_state['default_product']);
+  }
+  return $form;
+}

How do we make this code in Commerce Product URLs compatible and not slow things down?

146  // Add back the current product's query string variable.
147 if (variable_get('commerce_product_urls_url_key', 'id') == 'sku') {
148    $parsed_url['query']['sku'] = $form_state['default_product']->sku;
149  }
150  else {
151    $parsed_url['query']['id'] = $form_state['default_product']->product_id;
152  }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Marvin Daugherty’s picture

Issue summary: View changes
Marvin Daugherty’s picture

Issue summary: View changes
Marvin Daugherty’s picture

Issue summary: View changes
Alauddin’s picture

Same issue here with Product URL module and Commerce 7.x-1.11

Temporary solution :

Disabling the checkbox
x Update page URL on attribute change

at module config /admin/commerce/config/product_urls

at least allows cart access, even though it throws the errors...otherwise I was getting 500 server errors.

IckZ’s picture

subscribe!

IckZ’s picture

I'm just testing the following change in line ~160

from

  else {
    $parsed_url['query']['id'] = $form_state['default_product']->product_id;
  }

to

   else {
     if(isset($form_state['default_product'])) {
	$parsed_url['query']['id'] = $form_state['default_product']->product_id;
    } else {
	$parsed_url['query']['id'] = $form_state['default_product_id'];
    }
  }

in my dev-environment. Any remarks?

joelpittet’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Status: Active » Needs review
FileSize
774 bytes

How about just exit early if that key doesn't exist?

Alauddin’s picture

Hi guys,

Tested #7 patch to fresh dev...did not stop the notices.
Tested #6 as well with fresh dev..no luck.

still getting the two notices in log each time I add an item to cart

Notice: Trying to get property of non-object in _commerce_product_urls_build_query_string() (line 157 of .../public_html/sites/all/modules/commerce_product_urls/commerce_product_urls.module).

Notice: Undefined index: default_product in _commerce_product_urls_build_query_string() (line 157 of .../public_html/sites/all/modules/commerce_product_urls/commerce_product_urls.module).

akosipax’s picture

I don't think #7 is the right solution -- this would remove the url query parameters that supposedly gets appended to the page url by the module.
#6 doesn't work for sites that are configured to use the SKU in the url's query parameters.

Here's a patch that I think would work for everyone.

rballou’s picture

The patch in #9 seems to be working for me. Thanks!

stuwat’s picture

Yes, patch in #9 appears to be working great. Thank you for that.

vasike’s picture

Title: Add to cart triggers notices » Add to cart triggers notices - Current product does not exist on submission
FileSize
874 bytes

Indeed i confirm the issue and also the #9 patch gets rid of the notices.

The issue:
Default product data does not exist after form is built.
It is unset within "commerce_cart_add_to_cart_form_after_build()".
And "_commerce_product_urls_build_query_string()" assumes that $form_state['default_product'] exists.

There is another patch that builds this value/data ($form_state['default_product']) within extra form submit handler - commerce_product_urls_commerce_cart_add_to_cart_form_submit().
Using the submitted data - $form_state['values']['product_id'].

I think this could be considered critical issue, as the notice is "very" public.

  • maciej.zgadzaj committed d5d4355 on 7.x-1.x authored by vasike
    Issue #2448143 by vasike: Add to cart triggers notices - Current product...
maciej.zgadzaj’s picture

Priority: Major » Normal
Status: Needs review » Fixed

Merged #12 - thanks vasike!

Status: Fixed » Closed (fixed)

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