Search code examples
phpmagentomagento-1.9

Magento, getProductUrl() returning same url


I'm working on showing x amount of related products on the shopping cart page.

Here is my current code

$cart = Mage::getModel('checkout/cart')->getQuote();
            $c = count($cart);
            if($c != 0) {
                $c = $c - 1;
                $rand = rand(0, $c);
                $i = 0;
                    foreach ($cart->getAllItems() as $item) {
                        if($i == $c) {
                            $productId = $item->getProduct()->getId();
                            $model = Mage::getModel('catalog/product');
                            $product = $model->load($productId);
                            $allRelatedProductIds = $product->getRelatedProductIds();

                            $rc = count($allRelatedProductIds) - 1;
                            $rand = rand(0, $rc);
                            $relatedProduct = $model->load($allRelatedProductIds[$rand]);
                            echo "<div class='page-title'><h1>Maybe you would like to try one of these</h1></div>";
                     foreach($allRelatedProductIds as $prod) {
                                $p = $model->load($prod);
                                echo "<a href='".$p->getProductUrl()."'>";
                                echo "<div style='float:left; font-size:1.3em; width:33%; line-height:1.25; font-family:\"karlaregular\"'>";
                                echo "<img src='".Mage::helper('catalog/image')->init($p, 'small_image')->resize(300,300) ."' style='width:100%; height:100%;'>";
                                echo "<h2 class='product-name' style='text-align:center;'>".$p->getName()."</h2>";
                                echo "<div class='pricebox' style='text-align:center;'><span class='regular-price'><span class='price'>£".number_format($p->getPrice(), 2)."</span></span></div>";                          
                                echo "</div>";
                                echo "</a>";
                            } 

                        }
                      $i++;
                    }
            }

This almost works 100% problem is that the $p->getProductUrl() is only being populated by the first product in the loop and gives the wrong url to all sequential products.

I cannot see any issues with the above and would someone to take a look.

Thanks


Solution

  • Please try below code

    $cart = Mage::getModel('checkout/cart')->getQuote();
            $c = count($cart);
            if($c != 0) {
                $c = $c - 1;
                $rand = rand(0, $c);
                $i = 0;
                    foreach ($cart->getAllItems() as $item) {
                        if($i == $c) {
                            $productId = $item->getProduct()->getId();
                            $model = Mage::getModel('catalog/product');
                            $product = $model->load($productId);
                            $allRelatedProductIds = $product->getRelatedProductIds();
    
                            $rc = count($allRelatedProductIds) - 1;
                            $rand = rand(0, $rc);
                            $relatedProduct = $model->load($allRelatedProductIds[$rand]);
                            echo "<div class='page-title'><h1>Maybe you would like to try one of these</h1></div>";
                     foreach($allRelatedProductIds as $prod) {
                                $p = Mage::getModel("catalog/product")->load($prod);
                                echo "<a href='".$p->getProductUrl()."'>";
                                echo "<div style='float:left; font-size:1.3em; width:33%; line-height:1.25; font-family:\"karlaregular\"'>";
                                echo "<img src='".Mage::helper('catalog/image')->init($p, 'small_image')->resize(300,300) ."' style='width:100%; height:100%;'>";
                                echo "<h2 class='product-name' style='text-align:center;'>".$p->getName()."</h2>";
                                echo "<div class='pricebox' style='text-align:center;'><span class='regular-price'><span class='price'>£".number_format($p->getPrice(), 2)."</span></span></div>";                          
                                echo "</div>";
                                echo "</a>";
                            } 
    
                        }
                      $i++;
                    }
            }