0

I am trying to show products that are added in a custom attribute in the backend but it is only showing the first item

<?php
$bundle_products = $_product->getbundle_products();
if ($_product->getAttributeText('bundle') == yes):
$product_ids = array($bundle_products);
$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', array('in' => $product_ids));

foreach($collection as $eachproduct){
echo '<a href="' . $eachproduct->getProductUrl() . '"><img  src="' . $eachproduct->getImageUrl() . '" title="' . $eachproduct->getName() . '" height="50" width="50"></a>';
$combined_price += $eachproduct->getFinalPrice()/100*10;
}
endif;
?>

If I hard code it in as: $product_ids = array(3,138,194); It works fine. Am I missing something?

If I echo $bundle_products It outputs 3,138,194

1

Use below code:

<?php
    $bundle_products = $_product->getbundle_products();
    if ($_product->getAttributeText('bundle') == yes):
    //$product_ids = array($bundle_products);
    $product_ids = explode(',',$bundle_products);
    $collection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('entity_id', array('in' => $product_ids));

    foreach($collection as $eachproduct){
    echo '<a href="' . $eachproduct->getProductUrl() . '"><img  src="' . $eachproduct->getImageUrl() . '" title="' . $eachproduct->getName() . '" height="50" width="50"></a>';
    $combined_price += $eachproduct->getFinalPrice()/100*10;
    }
    endif;
    ?>

You are almost did all things right. Only you need to convert the string into array. that's why i use explode(',',$bundle_products).

Hope it will solve your problem. Happy learning

|improve this answer|||||
  • Thank you so much :) will have to have a look into explode. – Mike Tim Turner Apr 26 '16 at 9:36
  • And let me know if that work – Asish Hira Apr 26 '16 at 9:41
  • Yes it worked. I have to wait a few minutes to accept the answer. good work. – Mike Tim Turner Apr 26 '16 at 9:52

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.