Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从类别中加载所有产品,并按Magento中选定的多选属性进行筛选_Php_Magento - Fatal编程技术网

Php 从类别中加载所有产品,并按Magento中选定的多选属性进行筛选

Php 从类别中加载所有产品,并按Magento中选定的多选属性进行筛选,php,magento,Php,Magento,我的Magento 1.9安装中有两个多选属性(带有各自的选项),如下所示: // Load category products having selected "Make" & "Model" $products = Mage::getModel('catalog/category') ->load($load_from_category) ->getProductCollection() ->addAttributeToSelect('*')

我的Magento 1.9安装中有两个多选属性(带有各自的选项),如下所示:

// Load category products having selected "Make" & "Model"
$products = Mage::getModel('catalog/category')
    ->load($load_from_category)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(array('attribute' => $make_attribute_code, 'finset' => array($make_option_id))))
    ->addAttributeToFilter(array(array('attribute' => $model_attribute_code, 'finset' => array($model_option_id))))
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    );
制造

  • 制造商A
  • 制造商B
  • 制造商C
型号

  • A型
  • B型
  • C型
我有一些产品将有一个以上的“制造”和“模型”

如何加载特定类别中的所有产品,并按Magento中具有特定选定多选属性选项的产品进行筛选

我正在尝试加载这样的场景存在的产品:

make = [Manufacturer A] and [Manufacturer C]
model = [Type A] and [Type B]
这就是我迄今为止所尝试的:

// Mini-config
$make_attribute_code = 'make';
$model_attribute_code = 'model';
$filter_by_make = 'Manufacturer A';
$filter_by_model = 'Type A';
$load_from_category = 10117;

// Parse attribute option id of "Make"
$make_option_id = null;
$makeAttributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $make_attribute_code);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($makeAttributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
foreach ($attributeOptions as $attributeOption) {
    if (trim($attributeOption['label']) == $filter_by_make) {
        $make_option_id = (int)trim($attributeOption['value']);
        break;
    }
}

// Parse attribute option id of "Model"
$model_option_id = null;
$modelAttributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $model_attribute_code);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($modelAttributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
foreach ($attributeOptions as $attributeOption) {
    if (trim($attributeOption['label']) == $filter_by_model) {
        $model_option_id = (int)trim($attributeOption['value']);
        break;
    }
}

// Load category products having selected "Make" & "Model"
$products = Mage::getModel('catalog/category')
    ->load($load_from_category)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(array('attribute' => $make_attribute_code, 'eq' => $make_option_id)))
    ->addAttributeToFilter(array(array('attribute' => $model_attribute_code, 'eq' => $model_option_id)))
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    );

如果产品仅选择了一个“制造”和“型号”选项,则显示该选项。有什么想法吗?

我是这样解决的:

// Load category products having selected "Make" & "Model"
$products = Mage::getModel('catalog/category')
    ->load($load_from_category)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(array('attribute' => $make_attribute_code, 'finset' => array($make_option_id))))
    ->addAttributeToFilter(array(array('attribute' => $model_attribute_code, 'finset' => array($model_option_id))))
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    );

你想用代码做什么?