특정 속성 드롭 다운 값으로 제품 컬렉션로드
-
13-12-2019 - |
문제
사용자 정의 옵션을 사용하여 특정 가상 제품을로드하려고합니다. 대신 빈 배열을 계속받습니다.
내 속성은 'boat_part'속성 세트에서 'part_identifier'입니다. 여러 옵션이있는 드롭 다운 유형 속성입니다.내가 끌려는 옵션은 '봐'입니다.
여기에 내 코드가 있습니다 :
$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');
.
다음 콜렉션 필터에서 평소와 같이 사용합니다
제휴하지 않습니다 magento.stackexchange