Reputation: 6408
I am trying to figure out how to update a super attribute for a configurable product in Magento (specifically the upcharge price).
My initial investigation into this has lead me to believe that I will have to do something with the Configurable Type Instance of the product. Looking inside the Mage_Catalog_Model_Product_Type_Configurable class, I am not seeing anything that would have to do with price and it appears that this class does not have access to the debug function that most other objects in Magento have.
Can someone tell me how I would go about updating the upcharge prices of a super attribute?
Upvotes: 0
Views: 3470
Reputation: 2206
Have you look at this page?
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: [email protected]
* 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);
}
}
}
?>
Upvotes: 2