简体   繁体   中英

Get Product Options for each Product in the Catalog in Magento

This is using Magento 1.9.

I am trying to build a script that outputs all the product options for each product in the catalog into a CSV file.

I have tried using the code on this page: http://www.atwix.com/magento/configurable-product-attributes/

For this code:

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');  
foreach ($attribute->getSource()->getAllOptions(true) as $option) {
    echo $option['value'] . ' ' . $option['label'] . "\n";
}

I get no output, what-so-ever.

For this 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'];
    }
}

I get this error:

Fatal error: Call to undefined method Mage_Catalog_Model_Product_Type_Simple::getConfigurableAttributesAsArray() in /home/tws2/public_html/getopts.php on line 71

Ive been unable to find anything else on the internet for getting at the product options of a product with PHP code.

Can someone please explain to me how to use PHP to get at all the product options data for each product in the catalog, so I can output all this data into a CSV file.

The purpose of this is to transfer the product options data over to an identical Magento server that has the same catalog, just missing the product options data. Once I have all the CSV file of product options I was going to make another script to write them into the identical Magento server. This is how Ive been advised to do this task in-house, if you have a better suggestion please let me know.

To get the configurable options of whole catalog, try this

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');

  foreach ($collection as $product) {
   foreach ($product->getProductOptionsCollection() as $o) {
    $values = $o->getValues();

    foreach ($values as $v) {
        $v['product_id'] = $product->getEntityId();
        echo "<pre>";print_r($v->getData());
    }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM