按子产品属性过滤产品集合


9

我有分配给子产品的2属性之类的情况

1)person_height_from和2)person_height_to具有DropDown类型属性

仅在子产品中分配,而在母产品中不分配,

我想使用此属性过滤类别页面中的产品集合

喜欢 length = 175

 $collection->addAttributeToFilter('person_height_from', array('lteq' => $length));

 $collection->addAttributeToFilter('person_height_to', array('gteq' => $length));

这是否可能只获得类别产品中子产品分配高于值的那些父产品

您的帮助将不胜感激

Answers:


0

请使用以下代码:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter(
        array(
            array('attribute'=> 'length', 'like' => '175')
        )
    );

$collection->getSelect()
    ->joinLeft(
        array('link_table' => 'catalog_product_super_link'),
        'link_table.product_id = e.entity_id',
        array('product_id', 'parent_id')
    );

$collection->getSelect()->group('link_table.parent_id');

foreach ($collection as $product) {
    $productIds[] = $product->getParentId();
}

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', array('in' => $productIds));

在上面的代码中,我首先找到了长度为175的子产品。此后,我找到了与子产品相关联的父ID,然后根据父产品ID过滤了产品集合。


1
这是否有可能仅在1个sql查询中完成,我在商店中就有1个以上的lac产品,它将对性能产生巨大影响:(
liyakat

0

我在这里用子产品的2个属性来进行内部查询搜索

$collection = $observer->getEvent()->getCollection();

// check if  query is already in sql then no need to add per event
$cloneSelect = clone $collection->getSelect();
$wherePart = $cloneSelect->getPart(Zend_Db_Select::WHERE);
$excludedWherePart = 'AND (e.entity_id IN';
foreach ($wherePart as $key => $wherePartItem) {
    if (strpos($wherePartItem, $excludedWherePart) !== false) {
        return $this;
    }
}

$resource = Mage::getSingleton('core/resource');
$_readAdapter = $resource->getConnection('core_read');
$status = Mage::getSingleton('catalog/config')->getAttribute('catalog_product', 'status');
$heightTo = Mage::getSingleton('catalog/config')->getAttribute('catalog_product', 'person_height_to');
$heightFrom = Mage::getSingleton('catalog/config')->getAttribute('catalog_product', 'person_height_from');
$categoryId = Mage::registry("current_category")->getId();
$storeId = Mage::app()->getStore()->getId();

$query = $_readAdapter->select()
    ->from(array('e' => $collection->getTable('catalog/product')),array())
    ->joinInner(array(
        'at_status' => $status->getBackendTable()),
        'at_status.entity_id = e.entity_id AND at_status.store_id = 0 AND at_status.attribute_id = ' .$status->getAttributeId(),
        array()
    )
    ->joinInner(array(
        'at_stock' => $resource->getTableName('cataloginventory/stock_item')),
        'at_stock.product_id = e.entity_id AND at_stock.is_in_stock = ' . 1,
        array()
    )
    ->joinInner(array(
        'cat_index' => $collection->getTable('catalog/category_product_index')),
        'cat_index.product_id = e.entity_id AND cat_index.store_id = ' . $storeId . ' AND cat_index.category_id = ' . $categoryId,
        array()
    )
    ->joinInner(array(
        'at_person_height_to' => $heightTo->getBackendTable()),
        'at_person_height_to.entity_id = e.entity_id AND at_person_height_to.store_id = 0 AND at_person_height_to.attribute_id = ' . $heightTo->getAttributeId(),
        array()
    )
    ->joinInner(array(
        'at_person_height_from' => $heightTo->getBackendTable()),
        'at_person_height_from.entity_id = e.entity_id AND at_person_height_from.store_id = 0 AND at_person_height_from.attribute_id = ' . $heightFrom->getAttributeId(),
    array()
    )
    ->where(
        "e.type_id = 'simple' AND at_person_height_from.value <= " . $length . " AND at_person_height_to.value >= " . $length
    )
    ->join(
        array('link_table' => 'catalog_product_super_link'),
        'link_table.product_id = e.entity_id', array('parent_id')
    );

$productIds =  array_map('intval', array_unique($_readAdapter->fetchCol($query)));

// add filter here with parent ids
$collection->addAttributeToFilter('entity_id', array('in' => $productIds));

希望这对其他人有帮助并获得更好的主意。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.