質問

カスタムオプションを使用して特定の仮想製品をロードしようとしていますが、代わりに私は空の配列を取得し続けます。

My属性は、属性セット 'boot_part'の 'part_identifier'です。いくつかのオプションを持つドロップダウン型属性です。私が引っ張ろうとしているオプションは 'look'です。

これは私のコードです:

$attribute = 'msh_boat_part_identifier';

$boatLookProducts = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter($attribute, array('like'=>'Look'))
    ->load()
    ->getItems();

foreach($boatLookProducts as $boatLookProduct)
{
    $return[] = array(
        "id" => (int)$boatLookProduct->getEntityId(),
        "name" => $boatLookProduct->getName(),
        "type" => "profile",
        "shortDesc" => $boatLookProduct->getShortDescription(),
        "desc" => $boatLookProduct->getDescription(),
        "active" => ($boatLookProduct->getStatus() === "2" ? false : true)
    );
}
echo Mage::helper('pb')->ConvertToJson($return);
.

コレクションのマイクエリでは、配列を

に変更した場合
->addAttributeToFilter($attribute, array('like'=>'%%'))
.

すべてのProRuctsをうまく返しますが、私は私が欲しい特定のものを手に入れることができません!

役に立ちましたか?

解決

part_identifierはドロップダウン属性です。オプションラベルでコレクションをフィルタリングすることはできません。

オプションでコレクションをフィルタリングするには、そのオプションラベルのID(実際のオプション値)を取得する必要があります。

そのオプションIDでコレクションをフィルタリングできます。

// use your own attribute code here 
$attributeCode = 'your_attribute_here';
$attributeOption = 'Look';

$attributeDetails = Mage::getSingleton('eav/config')
    ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
$options = $attributeDetails->getSource()->getAllOptions(false); 
$selectedOptionId = false;
foreach ($options as $option){ 
    // print_r($option) and find all the elements 
    echo $option['value']; 
    echo $option['label'];
    if ($option['label'] == $attributeOption) {
        $selectedOptionId = $option['value'];   
    }
}

if ($selectedOptionId) {
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter($attributeCode, array('eq' => $selectedOptionId))
}
.

他のヒント

これは遅く、尋ねる時には有効ではありません。

$attributeOptionId = Mage::getSingleton('eav/config')
   ->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'your_attribute_code')
   ->getSource()
   ->getOptionId('Option Text Value/Label');
.

その後、通常のコレクションフィルタで使用する

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top