Frage

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!

War es hilfreich?

Lösung

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.

Andere Tipps

I had the same issue and I started doing the same thing: write a module to update configurable product's options.

I recently published it here: Magento Configurable Auto Pricing

I tested it just with EE 1.12 but it should work also with CE and I'd be glad if anyone wants to try it and give me any feedback or even better write his own fixes and commit them :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top