4

I want to have in the product view page and in the category page a list with all size attribute and price for each configurable product. I use this code to display the size and is work perfect but I need to get the price too. How I can add the price for associated products in the following code.

Thank you

$ids = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($_product->getId()); //get the children ids through a simple query
$attrs  = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
foreach($attrs as $attr) {
    if(0 == strcmp("size", $attr['attribute_code'])) {
        $options    = $attr['values'];
        foreach($options as $option) {
            print "{$option['store_label']}<br />";
        }
    }
}

I need to be something like in this image:

enter image description here

Edit: I can get simple products prices with the following code but how I can have Size and Price foreach simple product?

if($_product->getTypeId() == "configurable"):
    $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
    $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
    foreach($simple_collection as $simple_product){
        echo Mage::helper('core')->currency($simple_product->getPrice());
    }
endif;

1 Answer 1

4

considered size as attribute code and $simple_collection as simple products data.

    $_sized = array();
    $_size_id = array();
    foreach ($simple_collection as $simple_product) {              

      $label = $simple_product->getAttributeText('size');
      $_sized[] = $label;

      if (!isset($_size_id[$label])) {
        $_size_id[$label] = $simple_product->getData('size');
      }
    }

    //apply uniqueness
    $_final_sizes = array_unique($_sized);

EDITED ANSWER

    $j=0;
    foreach ($simple_collection as $simple_product) {
      $label = $simple_product->getAttributeText('size');
      $_sized[] = $label;

      if (!isset($_size_id[$label])) {
        $_size_id[$j][] =$label;
        $_size_id[$j][] = Mage::helper('core')->currency($simple_product->getPrice());
$j++;
      }
    }

    echo '<pre>';print_r($_size_id); echo '</pre>';
5
  • 1
    thank you but the prices for each attribute?
    – Robert
    Jan 11, 2017 at 5:22
  • that you already have it i guess
    – sandip
    Jan 11, 2017 at 9:07
  • No I don't have, this is my question, how I can get price for each option, because options is work with my first code and I need to add prices for each option
    – Robert
    Jan 11, 2017 at 9:14
  • @RobertD check updated answer
    – sandip
    Jan 21, 2017 at 8:55
  • @AA Why use both getAttributeText and simple_product->getData?
    – snh_nl
    Jan 15, 2019 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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