获取产品变体

时间:2016-07-23 07:09:07

标签: magento module magento-1.9 configurable-product variation

我创建了一个模块,用于将产品发送到另一个具有json结构的服务器,如此

{
name: "Black Nolita Cami",
code: "877",
sku: "wbk002c",
price: "150.0000",
sale_price: "wbk002c",
discount: "",
quantity: "0.0000",
weight: null,
url: "black-nolita-cami.html"}

但我有另一个字段变体,我已将所有产品变体放在那里如何操作?

1 个答案:

答案 0 :(得分:1)

这可能会让你开始朝着正确的方向前进......

$productSku = "ABCDE";
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku( $productSku );
$product->load($productId);

/**
 * In Magento Models or database schema level, the product's Custom Options are
 * executed & maintained as only "options". So, when checking whether any product has
 * Custom Options or not, we should check by using this method "hasOptions()" only.
 */
if($product->hasOptions()) {
    echo '<pre>';

    foreach ($product->getOptions() as $o) {
        $optionType = $o->getType();
        echo 'Type = '.$optionType;

        if ($optionType == 'drop_down') {
            $values = $o->getValues();

            foreach ($values as $k => $v) {
                print_r($v);
            }
        }
        else {
            print_r($o);
        }
    }

    echo '</pre>';
}