0

I'am using this code to get the shipping rates of the products, in the view.phtml, but I wish to only show the first item in the collection, not all of them. How could I achieve this?

The code: Link

<?php
if($_product->isSaleable())
{
$quote = Mage::getModel('sales/quote');
$quote->getShippingAddress()->setCountryId('*');
$quote->addProduct($_product);
$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$rates = $quote->getShippingAddress()->getShippingRatesCollection();

foreach ($rates as $rate)
{
echo $rate->getPrice();
}
}
?>
  • Credits to this link is not works – Amit Bera Jan 27 '15 at 12:37
  • I'am sorry, it is now edited to the correct link – Thomas Nielsen Jan 27 '15 at 13:35
1

To access the first element of a collection:

 $collection->getFirstItem(); 

Although I would really recommend storing this value in an attribute for each product, and refreshing this attribute with a cron every night for example. Loading this piece of code on every product page will be very resource intensive.

0

For sake of simplicity dump the foreach loop, and reference the index and key:

echo $rates[0]['price'];

Messy way of doing it:

foreach ($rates as $rate) {
   echo $rate->getPrice();
   break;
}

Loading collections takes a little while, I'd certainly cache the result set at least. But Magento being Magento, you should really have a full page cache and that way the above code only has to be run once.

  • FPC is a way to improve a store's performance, not hide some unnecessary heavy code – Mayers Jan 27 '15 at 13:27
  • Absolute rubbish, that's the primary purpose for caching, whether it is a small item thats computationally heavy or a complete html output from a collection of computationally heavy functions. – evensis Jan 27 '15 at 15:22
  • FPC should never be a solution for bad code. Running this on a product page, even just once, isn't a good solution if you have a way to do it more elegantly. – Mayers Jan 27 '15 at 15:30
  • Of course it's not, but his code isn't bad. It's just the foreach loop that's redundant in this case, it's a computationally heavy function that needs to be stored somewhere. You're proposing adding data to the database that you've just pulled from the database, there's nothing elegant about it, normalization is there for a reason. This should be cached, you don't have to muck around with cron jobs, as you just give it a TTL, far easier. – evensis Jan 27 '15 at 15:48
  • Your logic is shocking. "Why bother ... when you can just cache it". All I am saying is, if you have a way not to run it through the frontend at all, not only you avoid slow page load times, but you also avoid potential fatal errors breaking your product page. – Mayers Jan 27 '15 at 16:01
0

My goal was to show the cheapest shipping type available on the products. I'am using amasty's shipping per item extension, which makes an attribute with the price, and the flatrate shipping within Magento.

To achieve my goal, I created an attribute(Shipping Type) to all my products called Flatrate and Shipping per product. The flatrate attribute has the number 4752.

Then I did this:

// Shipping
$flatrate = array("20 €");
$amshipping = $this->htmlEscape($_product->getAmShippingPeritem());
$shippingtype = $_product->getShipping_type();

<table class="priceCalc">
        <tr>
            <td class="left">Shipping price
            </td>
            <td class="rightshipping">
             <?php 
             if ($shippingtype == 4752) {
             echo $flatrate[0]; }
             else
             echo round($amshipping).' €';
             ?>
            </td>
        </tr>
</table>

This is not the most pretty way, but so far it works.

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.