簡體   English   中英

在Magento的目錄中獲取每個產品的產品選項

[英]Get Product Options for each Product in the Catalog in 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服務器。 建議我在內部執行此任務,如果您有更好的建議,請告訴我。

要獲取整個目錄的可配置選項,請嘗試以下操作

$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