0

Code below gives me all associated products.

$this->getProduct()->getTypeInstance()->getUsedProducts()

what should I do in order to get sorted valued based on price in ASC order ?

2
  • is this configurable attribute??
    – Amit Bera
    Apr 25, 2015 at 7:24
  • Hey sourav, is this issue has been resolved
    – Amit Bera
    Apr 29, 2015 at 6:13

4 Answers 4

1

This is sort of one way to do it.. But you'll need to make sure the price-as-string sorts alphanumerically the way you want. It may need some tweaking.

$sortedProducts = array();
foreach ($uesdProducts as $product) {
    $price = $product->getPrice();
    $sortedProducts[$price . '-' . $product->getId()] = $product;
}
ksort($sortedProducts);

Another is to use

$collection = $this->getProduct()->getTypeInstance()
    ->getUsedProductCollection()
    ->addAttributeToSort('price');

Note: untested code.

1
  • 1
    Unless you have 2 products with the same price ;) Apr 29, 2015 at 6:05
1

TRY THIS:

if($_product->getTypeId() == "configurable"):

  $conf=Mage::getModel('catalog/product_type_configurable')
                               ->setProduct($_product);

    $simple_collection =  $conf->getUsedProductCollection()
                               ->addAttributeToSelect('*')
                               ->addFilterByRequiredOptions()
                               ->setOrder('price', 'ASC');

    print_r($simple_collection);  // simple associated products collection

endif;
0

Hope this can be helpful:

public function getUsedProducts($requiredAttributeIds = null, $product = null)
{
    Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
    if (!$this->getProduct($product)->hasData($this->_usedProducts)) {
        if (is_null($requiredAttributeIds)
            and is_null($this->getProduct($product)->getData($this->_configurableAttributes))) {
            // If used products load before attributes, we will load attributes.
            $this->getConfigurableAttributes($product);

And in method getConfigurableAttributes you can see following:

public function getConfigurableAttributes($product = null)
{
    Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
    if (!$this->getProduct($product)->hasData($this->_configurableAttributes)) {
        $configurableAttributes = $this->getConfigurableAttributeCollection($product)
            ->orderByPosition()
            ->load();
        $this->getProduct($product)->setData($this->_configurableAttributes, $configurableAttributes);
    }
    Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
    return $this->getProduct($product)->getData($this->_configurableAttributes);
}

Rewrite this method and replace ->orderByPosition() with method you like

4
  • where rewrite can u tell , pls
    – mcoder
    Apr 25, 2015 at 7:33
  • For debug purpose you can try fix here. If this start working - make model rewrite. Tell me what kind of sorting do you want to add? Sort by attribute_label?
    – zhartaunik
    Apr 25, 2015 at 7:52
  • yeah i have attribute label size , i want to sort by size
    – mcoder
    Apr 25, 2015 at 7:54
  • Sorry my solution is wrong. Its another type of sorting.
    – zhartaunik
    Apr 25, 2015 at 8:09
0

You can this.That can be an alternative solution.

First get all child products ids

ChildIds=Mage::getResourceModel('catalog_product_type_configurable')->getChildrenIds($this->getProduct()->getId());

Filter the product collection by those childs ids and short by attribute

 $collection = Mage::getResourceModel('catalog/product_collection');
 $collection
            ->addMinimalPrice() // optional for add minimal price to collection
            ->addFinalPrice() // optional for add minimal price to collection
            ->addTaxPercents() 
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) // add muitlple attribute to collection
            ->addUrlRewrite()->addIdFilter($ChildIds)->addAttributeToSort(AttributeToSort);
3
  • not works.......also why u used addUrlRewrite
    – mcoder
    Apr 25, 2015 at 7:41
  • mistake.... add addAttributeToSort for filter]
    – Amit Bera
    Apr 25, 2015 at 7:44
  • script stops some error still
    – mcoder
    Apr 25, 2015 at 7:48

Your Answer

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

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