8

I need to export all products with prices from Magento 1.7.

For simple products this is no problem, but for configurable products I have this problem: The price exported is the price set for the associated simple product! As you know, Magento ignores this price and uses the price of the configurable product plus adjustments for the selected options.

I can get the price of the parent product, but how do I calculate the difference depending on the selected options?

My code looks something like this:

foreach($products as $p)
   {
    $price = $p->getPrice();
            // I save it somewhere

    // check if the item is sold in second shop
    if (in_array($otherShopId, $p->getStoreIds()))
     {
      $otherConfProd = Mage::getModel('catalog/product')->setStoreId($otherShopId)->load($p->getId());
      $otherPrice = $b2cConfProd->getPrice();
      // I save it somewhere
      unset($otherPrice);
     }

    if ($p->getTypeId() == "configurable"):
      $_associatedProducts = $p->getTypeInstance()->getUsedProducts();
      if (count($_associatedProducts))
       {
        foreach($_associatedProducts as $prod)
         {
                            $p->getPrice(); //WRONG PRICE!!
                            // I save it somewhere
                        $size $prod->getAttributeText('size');
                        // I save it somewhere

          if (in_array($otherShopId, $prod->getStoreIds()))
           {
            $otherProd = Mage::getModel('catalog/product')->setStoreId($otherShopId)->load($prod->getId());

            $otherPrice = $otherProd->getPrice(); //WRONG PRICE!!
                            // I save it somewhere
            unset($otherPrice);
            $otherProd->clearInstance();
            unset($otherProd);
           }
         }
                     if(isset($otherConfProd)) {
                         $otherConfProd->clearInstance();
                            unset($otherConfProd);
                        }
       }

      unset($_associatedProducts);
    endif;
  }
12

Here is how you can get the prices of the simple products. The example is for a single configurable product but you can integrate it in your loop.
There may be a problem with performance because there are a lot of foreach loops but at least you have a place to start. You can optimize later.

//the configurable product id
$productId = 126; 
//load the product - this may not be needed if you get the product from a collection with the prices loaded.
$product = Mage::getModel('catalog/product')->load($productId); 
//get all configurable attributes
$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
//array to keep the price differences for each attribute value
$pricesByAttributeValues = array();
//base price of the configurable product 
$basePrice = $product->getFinalPrice();
//loop through the attributes and get the price adjustments specified in the configurable product admin page
foreach ($attributes as $attribute){
    $prices = $attribute->getPrices();
    foreach ($prices as $price){
        if ($price['is_percent']){ //if the price is specified in percents
            $pricesByAttributeValues[$price['value_index']] = (float)$price['pricing_value'] * $basePrice / 100;
        }
        else { //if the price is absolute value
            $pricesByAttributeValues[$price['value_index']] = (float)$price['pricing_value'];
        }
    }
}

//get all simple products
$simple = $product->getTypeInstance()->getUsedProducts();
//loop through the products
foreach ($simple as $sProduct){
    $totalPrice = $basePrice;
    //loop through the configurable attributes
    foreach ($attributes as $attribute){
        //get the value for a specific attribute for a simple product
        $value = $sProduct->getData($attribute->getProductAttribute()->getAttributeCode());
        //add the price adjustment to the total price of the simple product
        if (isset($pricesByAttributeValues[$value])){
            $totalPrice += $pricesByAttributeValues[$value];
        }
    }
    //in $totalPrice you should have now the price of the simple product
    //do what you want/need with it
}

The code above was tested on CE-1.7.0.2 with the Magento sample data for 1.6.0.0.
I tested on the product Zolof The Rock And Roll Destroyer: LOL Cat T-shirt and it seams to work. I get as results the same prices as I see in the frontend after configuring the product by Size and Color

3

Could it be that you need to change $p to $prod in the code below?

 foreach($_associatedProducts as $prod)
         {
                            $p->getPrice(); //WRONG PRICE!!
2

This is how I do it:

$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock('catalog/product_view_type_configurable');
$pricesConfig = Mage::helper('core')->jsonDecode($block->getJsonConfig());

Additionally, you can convert it to Varien_Object:

$pricesConfigVarien = new Varien_Object($pricesConfig);

So basically I am using the same method that is used to calculate prices for your configurable product page in the magento core.

0

Not sure if this would help but if you add this code to the configurable.phtml page it should spit out the configurable products super attributes with the price of each option and its label.

   $json =  json_decode($this->getJsonConfig() ,true);


    foreach ($json as $js){
        foreach($js as $j){

      echo "<br>";     print_r($j['label']); echo '<br/>';

            foreach($j['options'] as $k){
                echo '<br/>';     print_r($k['label']); echo '<br/>';
                print_r($k['price']); echo '<br/>';
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.