Question

Je reçois une collection de produits d'une certaine catégorie sur une page dans quelques sites différents Magento. Mon code pour obtenir la collection est:

        $category = new Mage_Catalog_Model_Category();
        $category->load($id);
        $collection = $category->getProductCollection();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('status', 1);
        $collection->addFieldToFilter(array(array('attribute'=>'visibility', 'neq'=>"1" )));
        $collection->getSelect()->limit(12);

        foreach ($collection as $shopProduct) :

            echo $shopProduct->getProductUrl();

        endforeach;

Mon problème est que sur l'un des sites Magento nous courons le ProductUrl() être récupéré est une URL comme http://www.my site.com/catalog/product/view/id/2309/s/shopcat/category/373/ et non plus comme http://www.site.com/shopcat/product-url-key.html. Mais sur tous les autres sites, il est apparaissant comme nous le voulons.

Quelqu'un sait pourquoi cela pourrait être? Merci! J'ai essayé d'utiliser getUrlPath() aussi, mais cela ne retourne rien. Je sais que je peux contourner cela en faisant quelque chose comme <?php echo $this->getBaseUrl().$shopProduct->getUrlKey().".html"; ?>, mais cette méthode semble un peu inefficace!

EDIT 21/03/14: Je suis encore avoir ce problème. Je me suis rendu compte récupère getProductUrl() l'URL voulu sur certains fichiers de modèle du site, mais pas d'autres. Par exemple, je charge une collection sur la page d'accueil et il me donne les URL que je veux. Mais getProductUrl() ne me donne pas les URL que je veux avec le même code sur une vue de la catégorie.

Était-ce utile?

La solution

Essayez d'obtenir la collection comme ceci:

$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('status', 1);
$collection->addFieldToFilter(array(array('attribute'=>'visibility', 'neq'=>"1" )));
//Where the magic happens
//this will add the url rewrite.
//addUrlRewrite can also be left without a parameter to generate url without category.
$collection->addUrlRewrite($category->getId()); 
$collection->getSelect()->limit(12);

En d'autres termes, laissez le savoir modèle pour donner la clé url au lieu de l'URL longue avec laide $collection->addUrlRewrite();.

Autres conseils

Obtenir une URL Produits

confusion potentiellement en raison des 3 méthodes que vous pouvez utiliser, qui sont tous en Mage_Catalog_Model_Product:

public function getUrlPath($category=null)
public function getUrlInStore($params = array())
public function getProductUrl($useSid = null)

La meilleure façon d'expliquer est de montrer simplement les résultats de plusieurs appels. Compte tenu d'un produit dont la clé URL est-mondrian grande table basse-set-multicouleur sur le domaine de http: //made.local les résultats sont les suivants:

$product->getUrlPath();
    'mondrian-large-coffee-table-set-multicolour'

$product->getUrlPath($category);
    'tables/mondrian-large-coffee-table-set-multicolour'

// you cannot stop this method adding ___store to the URL, even by setting _store_to_url to false
$product->getUrlInStore();
    'http://made.local/tables/mondrian-large-coffee-table-set-multicolour?___store=default'

// you cannot stop this method adding ___store to the URL, even by setting _store_to_url to false
// note - see the "using _ignore_category" section below for an arguable bug with using this param
$product->getUrlInStore(array('_ignore_category' => true));
    'http://made.local/mondrian-large-coffee-table-set-multicolour?___store=default'

$product->getProductUrl();
    'http://made.local/tables/mondrian-large-coffee-table-set-multicolour'

$product->getProductUrl(true);
    'http://made.local/tables/mondrian-large-coffee-table-set-multicolour'
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top