简体   繁体   English

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

[英]Get Product Options for each Product in the Catalog in Magento

This is using Magento 1.9. 这是使用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. 我正在尝试构建一个脚本,将目录中每个产品的所有产品选项输出到CSV文件中。

I have tried using the code on this page: http://www.atwix.com/magento/configurable-product-attributes/ 我尝试使用此页上的代码: 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. 我无法在互联网上找到其他任何东西来获取带有PHP代码的产品的产品选项。

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. 有人可以向我解释如何使用PHP获取目录中每个产品的所有产品选项数据,因此我可以将所有这些数据输出到CSV文件中。

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. 这样做的目的是将产品选件数据转移到具有相同目录的相同Magento服务器中,只是缺少产品选件数据。 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. 获得所有产品选项的CSV文件后,我将制作另一个脚本以将它们写入相同的Magento服务器。 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());
    }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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