How to get all super attribute options for a confi

2020-07-18 03:17发布

I have configurable products in my system consisting of color and size. I've written the following code to get the data but it is far too slow. Before adding this bit of code the page load time is below 2 seconds and after adding it jumps to 15 seconds. Surely there is faster way to get this information (I have 2 super attributes with about 10 options each)

My code:

$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute) {
    foreach ($productAttribute['values'] as $attribute) {
        $attributeOptions[$productAttribute['label']][$attribute['value_index']] = $attribute['store_label'];
    }
}

标签: php magento
1条回答
等我变得足够好
2楼-- · 2020-07-18 03:35

The operation getConfigurableAttributesAsArray is very slow. This is because it performs a load on the attribute collection.

Best way to fix this is using a custom query which only fetches the data you need. Something like this:

    $read = Mage::getSingleton('core/resource')->getConnection('core_read');

    $result = $read->query(
        "SELECT eav.attribute_code FROM eav_attribute as eav 
        LEFT JOIN catalog_product_super_attribute as super ON eav.attribute_id = super.attribute_id
        WHERE (product_id = " . $this->getProduct()->getId() . ");"
    );

    $attributeCodes = array();
    while ($row = $result->fetch()) {
        $attributeCodes[] = $row['attribute_code'];
    }

This will fetch all attribute codes. You can change the query so that will fetch the data you need.

查看更多
登录 后发表回答