如何在Magento中更新超级属性?

时间:2012-03-06 19:51:14

标签: magento

我试图弄清楚如何更新Magento中可配置产品的超级属性(特别是上涨价格)。

我对此的初步调查让我相信我将不得不对产品的可配置类型实例做一些事情。在Mage_Catalog_Model_Product_Type_Configurable类中查看,我没有看到任何与价格有关的内容,并且看起来这个类无法访问Magento中大多数其他对象所具有的调试功能。

有人可以告诉我如何更新超级属性的上涨价格吗?

1 个答案:

答案 0 :(得分:2)

你看看这个页面吗?

http://www.ayasoftware.com/content/magento-update-fly-super-product-attributes-configuration

< ?php

require_once 'app/Mage.php';
umask(0);
Mage::app("admin");
ini_set("display_errors", 0);

/** This file will update The Super product attributes configuration
 *  The price for each color will be calculated based on the price
 *  of the simple product (PSP) and the price of the configurable product (PCP)
 *  price is  PSP - PCP (PCP < PSP)
 *  Need help contact us at:  support@ayasoftware.com
 *  author : EL HASSAN MATAR
 */
$model = Mage::getModel("catalog/product"); 
$products = $model->getColletction();
$products->addAttributeToFilter('status', 1);//enabled
$products->addAttributeToFilter('visibility', 4);//catalog, search
$products->addAttributeToSelect('*');
$prodIds=$products->getAllIds();
$product = Mage::getModel('catalog/product');

foreach($prodIds as $productid) {

/**
* Load Product you want to get Super attributes of
*/
    $product = Mage::getSingleton("catalog/Product")->load($productid);

    $configurablePrice = $product->getPrice();

    $associatedProducts=$product->getTypeInstance()->getUsedProducts();
    $stack = array();
    for($j=0; $j< sizeof($associatedProducts) ; $j++){
        array_push($stack,  Array("color"=>$associatedProducts[$j]['color'],"size"=>$associatedProducts[$j]['size'], "price"=>$associatedProducts[$j]['price']));
    }

    if ($data = $product->getTypeInstance()->getConfigurableAttributesAsArray(($product))) {

        foreach ($data as $attributeData) {

            $id = isset($attributeData['id']) ? $attributeData['id'] : null;
            $size = sizeof($attributeData['values']);
            for($j=0; $j< $size ; $j++){
                multiArrayValueSearch($stack, $attributeData['values'][$j]['value_index'], $match);
                reset($match); // make sure array pointer is at first element
                $firstKey = key($match);
                $match= array();
                $attributeData['values'][$j]['pricing_value'] = $stack[$firstKey]['price'] - $configurablePrice;
            }

            if($id == 7){   // Check your $id value
                $attribute = Mage::getModel('catalog/product_type_configurable_attribute')
                ->setData($attributeData)
                ->setId($id)
                ->setStoreId($product->getStoreId())
                ->setProductId($productid)
                ->save();
            }
        }
    }
}
function multiArrayValueSearch($haystack, $needle, &$result, &$aryPath=NULL, $currentKey='') {
    if (is_array($haystack)) {
        $count = count($haystack);
        $iterator = 0;
        foreach($haystack as $location => $straw) {
            $iterator++;
            $next = ($iterator == $count)?false:true;
            if (is_array($straw)) $aryPath[$location] = $location;
            multiArrayValueSearch($straw,$needle,$result,$aryPath,$location);
            if (!$next) {
                unset($aryPath[$currentKey]);
            }
        }
    } else {
        $straw = $haystack;
        if ($straw == $needle) {
            if (!isset($aryPath)) {
                $strPath = "\$result[$currentKey] = \$needle;";
            } else {
                $strPath = "\$result['".join("']['",$aryPath)."'][$currentKey] = \$needle;";
            }
            eval($strPath);
        }
    }
}
?>