Magento Forum

product url key is not create
 
Matthieu MARY
Member
 
Total Posts:  33
Joined:  2008-08-28
France
 

hello everybody,

We have an import module which parse a csv file to load some products.
Products are create fine, but url key is not build.
The way we build the product is the following one:

// $mapping is an hashtable loaded from the csv file
$mapping = array();

$product Mage::getModel('catalog/product');
$defaultAttributeSetId =4;

$product->addData($mapping->getDatas());
$product->setData('tax_class_id'0);
$product->setData('attribute_set_id'$defaultAttributeSetId);
$product->save();
we have only one store in the admin > system > store management list.

I go in the aftersave of the backend urlkey model and in the refreshProductRewrite method.

public function afterSave($object)
    
{
        
/* @var $object Mage_Catalog_Model_Product */
        
if (!$object->getExcludeUrlRewrite() &&
            (
$object->dataHasChangedFor('url_key') || $object->getIsChangedCategories() || $object->getIsChangedWebsites())) {
                Mage
::getSingleton('catalog/url')->refreshProductRewrite($object->getId());
        
}
        
return $this;
    
}
but in, this->getStores is always empty: no stores are defined.
/**
     * Refresh product and categories urls
     *
     * @param int $productId
     * @param int $storeId
     * @return Mage_Catalog_Model_Url
     */
    public function refreshProductRewrite($productId$storeId null)
    
{
        
if (is_null($storeId)) {
            
foreach ($this->getStores() as $store{
                $this
->refreshProductRewrite($productId$store->getId());
            
}
            
return $this;
        
}
        
if ($product $this->getResource()->getProduct($productId$storeId)) {
            $storeRootCategoryId 
$this->getStores($storeId)->getRootCategoryId();
            
$categories $this->getResource()->getCategories($product->getCategoryIds(), $storeId);
            
$this->_rewrites $this->getResource()->prepareRewrites($storeId''$productId);

            if (!isset(
$categories[$storeRootCategoryId])) {
                $categories[$storeRootCategoryId] 
$this->getResource()->getCategory($storeRootCategoryId$storeId);
            
}
            
foreach ($categories as $category{
                $this
->_refreshProductRewrite($product$category);
            
}

            
unset($categories);
            unset(
$product);

//            $this->getResource()->clearCategoryProduct($storeId);
        
}

        
return $this;
    


    
/**
     * Retrieve stores array or store model
     *
     * @param int $storeId
     * @return Mage_Core_Model_Store|array
     */
    
public function getStores($storeId null)
    
{
        
return $this->getResource()->getStores($storeId);
    
}
the resource code is the following one:
/**
     * Retrieve stores array or store model
     *
     * @param int $storeId
     * @return Mage_Core_Model_Store|array
     */
    public function getStores($storeId null)
    
{
        
if (is_null($this->_stores)) {
            $this
->_stores $this->_prepareStoreRootCategories(Mage::app()->getStores());
        
}
        
if ($storeId && isset($this->_stores[$storeId])) {
            
return $this->_stores[$storeId];
        
}
        
return $this->_stores;
    
}
so the problem come from that Mage::app()->getStores() which returns me an emtpy array.
Why?
Do you have an idea? We have a store defined… :(
If I set storeId (at 0) like in adminhtml / controllers/catalog/productController.php ::_initProduct, this is the same problem.
Do you have an idea to have the url key build? why Mage::app()->getStores() return an empty array?
If I open the product in the backend and save it without changing any attribute, my url key is build ...
Please help.
Best regards
 
Magento Community Magento Community
Magento Community
Magento Community
 
hmpierson
Sr. Member
 
Total Posts:  101
Joined:  2008-11-15
 

Have you tried working with the webservices API? Looks like it might be a lot easier for your purpose.

$proxy = new SoapClient('https://yourdomain.com/index.php/api/soap/index/wsdl/1');
$sessionId $proxy->login('apiuser''apikey');

//you can do a dump to see sample output
//var_dump($proxy->call($sessionId, 'product.info', array('sku_of_product')));

$proxy->call($sessionId'product.update', array('sku_of_product', array('url_key'=>'name-of-product''url_path'=>'name-of-product.html')));

//or if you want different rewrites for different stores

//$proxy->call($sessionId, 'product.update', array('sku_of_product', array('url_key'=>'name-of-product', 'url_path'=>'name-of-product.html'), 'storea'));
//$proxy->call($sessionId, 'product.update', array('sku_of_product', array('url_key'=>'name-of-product', 'url_path'=>'name-of-product.html'), 'storeb));

if you have problems accessing the webservices api (I did at first), you can also look at my post:

http://www.magentocommerce.com/boards/viewthread/24991/

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top