Magento の list.phtml ビューから (属性値によって) アイテムを強制的に除外する方法はありますか? カートに入れることができ、個別に表示できる必要がありますが、検索やカテゴリへの移動では見つからない製品があります。
3 に答える
getProductCollection
のメソッドを拡張できますMage_Catalog_Model_Category
(必要に応じて、特定のテンプレートを置き換えるカスタム Block クラスを作成します)。
これが というモジュール内で行われると仮定すると、次の<YourNamespace>_<YourModule>
ようになります。
class <YourNamespace>_<YourModule>_Model_Category extends Mage_Catalog_Model_Category {
public function getProductCollection() {
$collection = parent::getProductCollection()
foreach($collection as $key => $item) {
if (<YOUR_REMOVE_CRITERIA_HERE>) {
$collection->removeItemByKey($key);
}
}
}
}
<YOUR_REMOVE_CRITERIA_HERE>
構成オプションからコレクション内のアイテム (製品) の属性まで何でもかまいません。
より簡単な解決策は、製品をカテゴリの製品リストに表示したくない場合は、カテゴリからそれらを削除することです.
お役に立てれば幸いです。
lg、
フロー
新しいモジュールを作成せずに、これを理解することになりました。/app/code/local/mage/catalog/block/product で list.php へのオーバーライドを作成し、次のコード行を含めました。
$collection->clear()
->addAttributeToFilter('my_attribute_name', array('gteq'=> 524 )) //custom attrib ID
->addAttributeToFilter('visibility', array('eq'=> 4 )) // Visibility is equal to "Catalog/Search"
->load();
すぐ後:
protected function _beforeToHtml()
{
$toolbar = $this->getToolbarBlock();
// called prepare sortable parameters
$collection = $this->_getProductCollection();
// use sortable parameters
if ($orders = $this->getAvailableOrders()) {
$toolbar->setAvailableOrders($orders);
}
if ($sort = $this->getSortBy()) {
$toolbar->setDefaultOrder($sort);
}
if ($dir = $this->getDefaultDirection()) {
$toolbar->setDefaultDirection($dir);
}
if ($modes = $this->getModes()) {
$toolbar->setModes($modes);
}
そして前に:
// set collection to toolbar and apply sort
また、これと同じロジックを新しい製品ページの new.php に追加して、表示してはならない製品を除外できるようにしました。
ブラッド
プレゼンテーション層にビジネスロジックを追加するのは良い習慣ではありませんが、とにかく、app \ design \ frontend \ iln \ default \ template \ catalog \ product\list.phpで製品リストを制限する簡単な方法です。
<?php $_selling_type = $_product->getResource()->getAttribute('selling_type')->getFrontend()->getValue($_product); ?>
if ($_selling_type == 'No Price')
{
echo ('what ever you want');
}