5

When we create or modify a product -- the url_key doesn't seem to associate with the store_id of the product. For example:

$product = Mage::getModel('catalog/product')->load($prodid);
$product->setUrlKey('product-url-key')
$product->setStoreId(1); 
$product->save();

But here lies the problem:

I would expect url_key in the Table catalog_product_entity_url_key to have a store_id of 1, but it gets set to 0. How else should a product be saved to that the url_key gets the proper store_id?

enter image description here

Background on this problem:

We are writing scripts for updating programs and have been getting a lot of these errors:

Uncaught exception 'Mage_Eav_Model_Entity_Attribute_Exception' with message 'Product with the 'product-url-key-added' url_key attribute already exists.' in /var/www/mage02/app/code/core/Mage/Core/Model/Config.php:1348

Looking further into the code, this problem seems to be created because of how the store_id is getting set. We can make the problem go away by manually updating the store_id.. however, on the re-save -- the store_id for the url_key gets set back to 0.

Magento Version used: 1.13.1.0 (Fresh Install)

5

Try setting the store id before loading the product. Something like this:
$product = Mage::getModel('catalog/product')->setStoreId(1)->load($prodId);

| improve this answer | |
  • Marius. Its still hard to determine the issue but I think you're on to something. I did go through the code and explicitly setStoreId() and the issues I was having became less frequent. This issue seems to be primarily related to EE 1.13.1.0 and doesn't exist in EE 1.13.0.2 or CE 1.8. Thanks for your help! – JustinP Jan 8 '14 at 1:39
  • 1
    Have you found a permanent solution for 1.13.1.0? – srgb Apr 14 '14 at 21:42
  • 1
    Any solutions to this on 1.13.1.0? – Kervin Ramen May 23 '14 at 6:23
  • 1
    A better solution is: Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); – B00MER Oct 7 '14 at 9:28
  • Thanks! Setting the current store to the admin store solved the problem for me why importing products from the CLI wasn't working! – Giel Berkers Jul 31 '15 at 15:15
10

Researching what B00MER and Marius where saying, it appears that in EE 13.1+ the offending function is _isAvailableUrl in app/code/core/Enterprise/Catalog/Model/Product/Attribute/Backend/Urlkey.php

There is a comment in li 179:180

179         // we should allow save same url key for product in current store view
180         // but not allow save existing url key in current store view from another store view

However, the select statement right before this reads:

173         $select = $this->_connection->select()
174             ->from($this->getAttribute()->getBackendTable(), array('entity_id', 'store_id'))
175             ->where('value = ?', $object->getUrlKey())
176             ->limit(1);
177         $row = $this->_connection->fetchRow($select);

This select statement just queries all records of catalog_product_entity_url_key and doesn't take account the current store id. So I modified the file like so which takes into account the current store id of the record being saved. This appears to fix the issue.

diff --git a/app/code/core/Enterprise/Catalog/Model/Product/Attribute/Backend/Urlkey.php b/app/code/core/Enterprise/Catalog/Model/Product/Attribute/Backend/Urlkey.php index 3ad4bea..c4319bd 100644
--- a/app/code/core/Enterprise/Catalog/Model/Product/Attribute/Backend/Urlkey.php
+++ b/app/code/core/Enterprise/Catalog/Model/Product/Attribute/Backend/Urlkey.php
@@ -173,6 +173,7 @@ class Enterprise_Catalog_Model_Product_Attribute_Backend_Urlkey
         $select = $this->_connection->select()
             ->from($this->getAttribute()->getBackendTable(), array('entity_id', 'store_id'))
             ->where('value = ?', $object->getUrlKey())
+            ->where('store_id = ?', $object->getStoreId())
             ->limit(1);
         $row = $this->_connection->fetchRow($select);
| improve this answer | |

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.