$ _Product-> getProducturl()URLキーなしでURLパスを与える
質問
いくつかの異なるMagentoサイトのページに特定のカテゴリの製品コレクションを入手しています。コレクションを取得するための私のコードは次のとおりです。
$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;
私の問題は、私たちが実行しているMagentoサイトの1つで ProductUrl()
フェッチされることはURLのようなものです http://www.my site.com/catalog/product/view/id/2309/s/shopcat/category/373/
そして、それ以上のようではありません http://www.site.com/shopcat/product-url-key.html
. 。しかし、他のすべてのサイトでは、私たちが望むように表示されています。
なぜこれがなぜなのか知っていますか?ありがとう!使ってみました getUrlPath()
あまりにも、これは何も戻りませんでした。私はこれをすることでこれを回ることができることを知っています <?php echo $this->getBaseUrl().$shopProduct->getUrlKey().".html"; ?>
しかし、その方法は少し非効率的なようです!
編集21/03/14: 私はまだこの問題を抱えています。私は気づきました getProductUrl()
サイトの一部のテンプレートファイルで必要なURLを取得しますが、他のテンプレートではありません。たとえば、私はホームページに1つのコレクションをロードしていますが、それは私が望むURLを提供しています。だが getProductUrl()
カテゴリビューで同じコードを使用して必要なURLを提供していません。
解決
このようなコレクションを取得してみてください:
$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);
言い換えれば、長い醜いURLの代わりにURLキーを与えるようにモデルに知らせてください $collection->addUrlRewrite();
.
他のヒント
製品URLを取得します
使用できる3つの方法により混乱する可能性があります。これらはすべてmage_catalog_model_productにあります。
public function getUrlPath($category=null)
public function getUrlInStore($params = array())
public function getProductUrl($useSid = null)
説明する最良の方法は、単にいくつかの呼び出しの結果を示すことです。 URLキーがMondrian-Large-Coffee-Table-Set-Multicolourのドメインにある製品を与えられた http://made.local 結果は次のとおりです。
$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'