magentoproductconfigurable

Magento - Updating the price of a configurable product option


Magento version 1.6.1.0. I'm attempting to write some code that will dynamically update the price of a configurable product's option. My ultimate aim is to write a module that will update the price of the configurable product's options based on the price of the children products of the configurable. The attached code pulls out all the configurable products from a catalogue and displays them along with the product options & prices and the children products' name & price. I plan to work out the difference in price between the configurable and each child product and update the price appropriate option to match. So far I have been unable to work out how to update the price of a product option.

Short version: I just need a way to update the price of a configurable product's option. Do you know how to do this?

<?php
require_once './app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(1);

// load in configurable products
$productConfig = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', 'configurable');
foreach ($productConfig as $_product)
{
    // load the configurable product
    $_product = Mage::getModel('catalog/product')->load($_product->getId());
    echo 'Product Name';
    var_dump ($_product->getName());
    var_dump ($_product->getPrice());
    // Collect options applicable to the configurable product
    $productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
    $attributeOptions = array();
    foreach ($productAttributeOptions as $productAttribute)
        {
        var_dump($productAttribute['label']);
        foreach ($productAttribute['values'] as $attribute)
            {
            var_dump($attribute);
            }
        }

    // loop through the child products
    echo 'Child products';
    $col = Mage::getModel('catalog/product_type_configurable')->setProduct($_product)->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
    foreach($col as $simple_product)
    {
        var_dump($simple_product->getName());
        var_dump($simple_product->getPrice());
    } 
}
echo '~fin';
?>

Thank you!


Solution

  • Well, I solved this one after quite a bit of head scratching. I don't know if I did it "right" or not.

    I solved it by manually running some SQL to adjust the pricing in the catalog_product_super_attribute_pricing table. If you're going to do this, you'll need a product_super_attribute_id, which you can get from the catalog_product_super_attribute table if you have a product ID.

    One caveat: If a price does not exist in the backend for an option (if the option exists, but adds £0 to the product price when selected), there will not be a record for the option in the catalog_product_super_attribute_pricing table, you'll need an insert query instead of an update in that case.