Skip to content

Instantly share code, notes, and snippets.

@jonathanselander
Last active December 15, 2015 05:09
Show Gist options
  • Save jonathanselander/5206498 to your computer and use it in GitHub Desktop.
Save jonathanselander/5206498 to your computer and use it in GitHub Desktop.
If anybody ever wants to find the correct prices for the different simple product combinations a configurable product can have in the frontend, ie for exports, this is one way to go
<?php
// The configurable product must be the one in question that we want to
// find the price for
$product = Mage::getModel('catalog/product')
->load(771);
// First, fetch the attribute combination representing the simple product
$attributes = $product->getTypeInstance(true)
->getConfigurableAttributes($product);
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null, $product);
foreach ($childProducts as $simpleProduct) {
$product->setExportPrice($product->getFinalPrice());
foreach ($attributes as $attribute) {
// We need to compare each attribute against every possible simple product
// attribute combination
$attributeCode = $attribute->getProductAttribute()
->getAttributeCode();
$simpleValue = $simpleProduct->getData($attributeCode);
foreach ($attribute['prices'] as $attributePrice) {
if ($simpleValue === $attributePrice['value_index']) {
if (empty($attributePrice['pricing_value'])) {
continue 2;
}
if (!empty($attributePrice['is_percent'])) {
$price = $product->getFinalPrice()*$attributePrice['pricing_value']/100;
} else {
$price = $attributePrice['pricing_value'];
}
// Make sure catalog rules are applied to the additional price
$product->setConfigurablePrice($price);
Mage::dispatchEvent(
'catalog_product_type_configurable_price',
array('product' => $product)
);
$product->setExportPrice($product->getExportPrice()
+ $product->getConfigurablePrice());
// Move on to the next attribute
continue 2;
}
}
}
// Do something with $product->getExportPrice()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment