2013-07-20 4 views
1

현재 Magento 용 제품 구성자 모듈을 프로그래밍 중입니다. 핵심 기능 외에도 모든 기능이 제대로 작동합니다. 선택한 속성에 대해 데이터베이스에서 올바른 제품을 선택합니다. 내 속성은 구성 자의 양식 필드가 생성되는 속성 그룹에 속하며, 사용하는 값은 관리 필드입니다. 상점 언어는 스웨덴어입니다.Magento - addFieldToFilter가 올바른 제품을 선택하지 않았습니다.

샵에는 간단한 제품이 연관된 구성 가능한 제품이 하나 있습니다. 간단한 제품은 상점에서 개별적으로 표시되지 않습니다. 현재 내 속성의 모든 조합이 존재하지는 않지만 적어도 기존의 조합에 대해 올바른 제품을 반환해야합니다. 처음에는 항상 null이 반환되었지만 지금은 모든 제품을 반환합니다. 그냥 제품을 선택하도록 어떻게 할 수 있습니까?

먼저 "제품 목록에 사용됨"을 활성화 할 때까지는 속성이 쿼리에 표시되지 않았습니다.

public function getProduct($attributes) 
{ 

    Mage::Log($attributes); 

    //Get Product Collection 
    $collection = Mage::getModel('catalog/product')->getCollection(); 

    $collectionCount = count($collection); 

    Mage::Log($collectionCount); 

    //Filter for Selected Product 
    $collection->addFieldToFilter('doorconfig_enable',array('eq' => 'Yes')); 

    foreach ($attributes as $key => $value) 
    { 
     $collection->addFieldToFilter($key,array('eq' => $value)); 
    } 

    $selection = $collection->getSelect()->__toString(); 

    Mage::Log($selection); 

    $collectionCount = count($collection); 

    Mage::Log($collectionCount); 

    $product = $collection->getFirstItem(); 

    return $product; 

} 

$ 속성 매개 변수가 올바르게 제출 된 POST 데이터가 포함되어 있습니다 :

[doorconfig_color] => 000000 
[doorconfig_type] => lines 
[doorconfig_size] => 2500x1800 
[doorconfig_remote] => no 
[doorconfig_digitalkeypad] => No 
[doorconfig_extraremotecontrol] => No 
[configdoor_addemergencylock] => No 
[doorconfig_insideopeningbutton] => No 
[doorconfig_window] => No window 

을 "어떤 도움 :)

에 미리

덕분에이 내 getProduct 방법입니다 configdoor_addemergencylock "은 친애하는 동료들에 의한 오타이지만 데이터베이스 (사람이 궁금해하는 경우)에서 올바르게 읽습니다. 나는 SQL 좋은 아주 젠토에 경험하지 아닙니다, 나는 내 쿼리인지 모르는 옳고 그름 : 당신은 당신의 제품 컬렉션을 필터링이 시도 할 수

2013-07-20T09:47:19+00:00 DEBUG (7): SELECT 1 AS `status`, `e`.`entity_id`, 
`e`.`type_id`, `e`.`attribute_set_id`, `price_index`.`price`, `price_index`. 
`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, 
LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) 
AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, 
`price_index`.`tier_price`, `e`.`doorconfig_color`, `e`.`doorconfig_color_value`, 
`e`.`doorconfig_type`, `e`.`doorconfig_type_value`, `e`.`doorconfig_size`, 
`e`.`doorconfig_size_value`, `e`.`doorconfig_remote`, `e`.`doorconfig_remote_value`, 
`e`.`doorconfig_digitalkeypad`, `e`.`doorconfig_digitalkeypad_value`, 
`e`.`doorconfig_extraremotecontrol`, `e`.`doorconfig_extraremotecontrol_value`, 
`e`.`configdoor_addemergencylock`, `e`.`configdoor_addemergencylock_value`, 
`e`.`doorconfig_insideopeningbutton`, `e`.`doorconfig_insideopeningbutton_value`, 
`e`.`doorconfig_window`, `e`.`doorconfig_window_value` FROM `catalog_product_flat_1` 
AS `e` INNER JOIN `catalog_product_index_price` AS `price_index` 
ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' 
AND price_index.customer_group_id = 0 WHERE (e.doorconfig_color = '000000') 
AND (e.doorconfig_type = 'lines') AND (e.doorconfig_size = '2500x1800') 
AND (e.doorconfig_remote = 'no') AND (e.doorconfig_digitalkeypad = 'No') 
AND (e.doorconfig_extraremotecontrol = 'No') AND (e.configdoor_addemergencylock = 'No') 
AND (e.doorconfig_insideopeningbutton = 'No') AND (e.doorconfig_window = 'No window') 

답변

0

$collection = Mage::getModel('catalog/product')->getCollection(); 
$collection->addAttributeToSelect(*); 
$collection->addAttributeToFilter('doorconfig_enable',array('eq' => 'Yes')); 
+0

조언은 addFieldToFilter은 정확했다 대신 addAttributeToFilter를 사용하지만, doorconfig_enable 위해 내가했다 Roman Shopin은 다음과 같이 제안했다 : $ collection-> addAttributeToFilter ('doorconfig_enable', array ('eq'=> 1));'ID를 통해 루프하면 다른 속성이 여전히 작동하지 않겠지 만 : – ericstumper

+0

foreach ($ attributes as $ key => $ value) { $ collection-> addAttributeToSelect ($ key); $ 모음 -> addAttributeToFilter ($ 키 어레이 ( '당량'=> 마법사 :: getResourceModel ('탈/제품') -> getAttribute를 ($ 키) -> getSource와() -> getOptionId ($ 값) ) ); } – ericstumper

0

어떤 유형의 "doorconfig_enable"속성입니까? 부울 값이면 "0"또는 "1"을 사용하십시오. "doorconfig_enable"이 select 속성 인 경우 옵션 ID를 사용해야합니다. 그래서 : 제대로처럼 필터링하는 부울 값을 사용

$collection = Mage::getModel('catalog/product')->getCollection() 
    ->addAttributeToSelect('doorconfig_enable') 
    ->addAttributeToFilter('doorconfig_enable',array('eq' => 1)); 

Mage::log($collection->getSize(),null,'custom.log'); 

이 옵션 ID를 얻기 위해 필요한 경우, 체크 아웃 this

+0

doorconfig_enable은 예/아니오 속성이므로 부울입니다. 나는 값으로 1을 사용하여 시도하고 그것은 작동합니다. 그러나 루프에는 여전히 운이 없다.나는 선택된 모든 속성 옵션에 대해 ID를 얻으려고했다 : – ericstumper

+0

'foreach ($ attributes = $ key => $ value) { $ collection-> addAttributeToSelect ($ key); $ 모음 -> addAttributeToFilter ($ 키 어레이 ( '당량'=> 마법사 :: getResourceModel ('탈/제품') -> getAttribute를 ($ 키) -> getSource와() -> getOptionId ($ 값) ) ); } – ericstumper

+0

그러나 쿼리의 모든 속성 ID가 다음과 같이 반환 된 것으로 보입니다. – ericstumper

관련 문제