0
這是一個漫長的週末,我的大腦不能正常工作,我想不止一個標籤來篩選產品集合。在「標籤/ product_collection」我可以一個值只提供給「addTagFilter」,所以我想通過我自己的發言中,其中()Magento的:添加一個以上的「標籤」,以收集過濾器
relation.tag_id = 6 AND relation.tag_id = 9
但其返回沒有,即使有6級的產品,同時具有標籤6和9,我可以將查詢更改爲IN(6,9),並返回所有具有AND和刪除AND並選擇只是6或9的產品,但對於我來說,我無法工作爲什麼AND不回來?!下面的完整代碼。
$collection = Mage::getResourceModel('tag/product_collection');
$collection->addAttributeToFilter('status', array('eq' => 1));
$collection->addAttributeToFilter('type_id','simple');
$collection->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->getSelect()->where("relation.tag_id = 6 AND relation.tag_id = 9","");
在此先感謝。
===編輯=== 好吧,以及我沒有太多的運氣試圖鍛鍊這個集合上的'SQL'方法,所以如果任何人都覺得可以隨意列出它,我們所有的人都會贊成這個出來的。與此同時,我有一個輕微迴避的做法。如果你有1000個產品,我不會推薦這個,我可以想象它有點慢。
// Load our collection
$collection = Mage::getResourceModel('tag/product_collection');
$collection->addAttributeToFilter('status', array('eq' => 1));
$collection->addAttributeToFilter('type_id','simple');
$collection->addAttributeToSelect('sku')
->addAttributeToSelect('name');
// Loop the collection
foreach ($collection as $_product)
{
// Load ALL tags for product
$model = Mage::getModel('tag/tag');
$tags = $model->getResourceCollection()
->addPopularity()
->addStatusFilter($model->getApprovedStatus())
->addProductFilter($_product->getId())
->setFlag('relation', true)
->addStoreFilter(Mage::app()->getStore()->getId())
->setActiveFilter()
->load();
// loop through all the tags if we find one set a flag to skip returning this product.
$arr = array();
foreach($tags as $_t){ $arr[$_t->getId()] = $_t->getName(); }
$filter_tags = count($_REQUEST['tags']) > 0 ? $_REQUEST['tags'] : array();
$tag_found = 0;
$on_filter = count($filter_tags) > 0 ? 1 : 0;
foreach($arr as $key => $value){ array_key_exists($key,$filter_tags) ? $tag_found++ : 0; }
// if all if good send the product to an array to be returned
if($on_filter && $tag_found){ // add $_product to a return }
}
感謝阿米特,這方式基本上做同樣的事情,我的上面的代碼,我可以在SELECT中看到添加AND SQL絕對罰款,但它仍然不會選擇正確的結果,也許它有辦法收集相關標籤的方式嗎? ! :s –
因此,當您使用單個的relation_tag_id條件時,如果是結帳產品id,它會返回一些結果,可能是沒有tag_id 6和9的產品。 – MagentoDiary