2017-06-01 59 views
0

我擁有帶色板屬性的可配置產品,在本例中爲顏色。獲取具有產品的屬性的色板數

我需要知道有多少種顏色(數字)有產品,或者有多少單個產品構成此可配置產品。

事實上,我需要知道產品中何時有多種顏色。

回答

0

最後我發現它,也許它會幫助別人的未來:

$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product); 

//in this case the attribute color that I needed is in [0] position 
$available_colors = sizeof($productAttributeOptions[0]["values"]); 

if ($available_colors >1): 
    //custom code 
endif; 

編輯:該解決方案適用於一個產品,或者至少幾個產品,但如果你需要它在每個產品中運行該代碼真的很慢的產品列表。有時它驅動到超時並關閉數據庫連接,所以Web崩潰時出現錯誤。

最後,我得到了一個解決方案,也許它不是最好的,但它是相當快比我之前使用的一個:

$_idsForTheQuery = $_productCollection->getAllIds(); 

    $read = Mage::getSingleton('core/resource')->getConnection('core_read'); 
    $sql_query = "SELECT parent_id, COUNT(parent_id) AS colors FROM 
       (SELECT cpr.parent_id FROM `eav_attribute` a 
       LEFT JOIN `catalog_product_entity_int` cpei ON cpei.attribute_id=a.attribute_id 
       LEFT JOIN `catalog_product_relation` cpr ON cpr.child_id=cpei.entity_id 
       WHERE attribute_code = 'color' AND cpr.parent_id IN (".implode (", ", $_idsForTheQuery).") 
       GROUP BY cpr.parent_id, cpei.value) colors 
       GROUP BY parent_id"; 

    $results = $read->query($sql_query); 

    $number_of_colors_by_id_array = array(); 
    foreach($results as $r) 
    { 
     $number_of_colors_by_id_array[$r["parent_id"]] = $r["colors"]; 
    } 

,然後在產品的foreach循環

<?php if ($number_of_colors_by_id_array[$_product->getId()]>1): ?> 
    <div class="aditional-colors-message"> 
     <?php echo __('more colors available'); ?>   
    </div> 
<?php endif; ?>