获取Magento目录中每个产品的产品选项

时间:2015-06-23 10:57:59

标签: php magento

这是使用Magento 1.9。

我正在尝试构建一个脚本,将目录中每个产品的所有产品选项输出到CSV文件中。

我尝试过使用此页面上的代码:http://www.atwix.com/magento/configurable-product-attributes/

对于此代码:

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

我没有输出,无论如何。

对于此代码:

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

我收到此错误:

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

我一直无法在互联网上找到任何其他东西来获取带有PHP代码的产品的产品选项。

有人可以向我解释如何使用PHP获取目录中每个产品的所有产品选项数据,这样我就可以将所有这些数据输出到CSV文件中。

这样做的目的是将产品选项数据传输到具有相同目录的相同Magento服务器,只缺少产品选项数据。一旦我拥有了产品选项的所有CSV文件,我就会制作另一个脚本,将它们写入相同的Magento服务器。这就是我被建议在内部完成这项任务的方法,如果你有更好的建议请告诉我。

1 个答案:

答案 0 :(得分:2)

要获取整个目录的可配置选项,请尝试此

$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());
    }
   }
}
相关问题