Magento: Random "Featured Product" - magento

My client needs a a small box on CMS pages and on Category landing pages that will show thumbnail/price/short description of a random item related to that category (separate from the grid view)
Any thoughts on what would be the best way to accomplish this?
Thanks,
-Sam

go to template/catalog/product/view/ and make a new phtml file random_product.phtml with the following code
<?php
$catId = $this->getCat_id();
$cat=Mage::getModel("catalog/category")->load($catId);
$prodCollection = $cat->getProductCollection();
$pids=array();
foreach($prodCollection as $product)
{
array_push($pids,$product->getId());
}
$randProductId=array_rand($pids);
echo $randProductId;
?>
now if your category id is for example 10, make a static block and paste the following code in the contents
{{block type="catalog/product" cat_id="10" template="catalog/product/view/random_product.phtml"}}
now when you will view the static block, you will see a random product id every time you refresh.
THen,you can write your own custom html in the phtml file after loading the product.
To load your product from here you can do $product = Mage::getModel('catalog/product')->load($randProductId); then call methods such as $product->getName() etc to get the details you need to output.

Related

Magento get stock quantity at related products, upsells, crosssells in frontend

I need to get the stock quantity for each product in the section where "Related products" is showed in frontend. Using Magento 1.9.
This function will not help me show the actual qty in my related products section:
Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
The function shows stock qty in the product view page and the catalog collection but does not work for related products.
What to do?
I am not sure if you are actually calling the above piece of code inside a PHTML file.
Assuming you are using the RWD theme and calling it inside a PHTML directly (which is not as per Magento Standards), the file location would be app/design/frontend/rwd/default/template/catalog/product/list/related.phtml
Inside, the FOREACH loop, call the below piece of code (screenshot attached for reference - frontend display):
<?php echo Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty(); ?>
The ideal solution would be overriding the CORE file:
app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
like below:
app/code/local/Namespace/Modulename/Model/Stock/Item.php
in order to add a new function:
<?php
class Namespace_Modulename_Model_CatalogInventory_Stock_Item extends Mage_CatalogInventory_Model_Stock_Item
{
public function getItemStockQty($product)
{
return $this->loadByProduct($product)->getQty();
}
}
And inside the PHTML file, under the FOREACH LOOP call this function as below:
<?php echo $this->getItemStockQty($_item); ?>
Screenshot
Hope this helps.
Happy Coding...

How to add Rating of a product in products listing?

I am displaying products of a particular category to the homepage content section.
I have made a separate .phtml file to display my homepage.
Now I want to show the ratings of a product (products are already rated). How do I show it?
If you look at the category listing template it's pretty easy to work out how category pages render out the review summary to show the rating block.
First load the product in question:
$product = Mage::getModel('catalog/product')->load($id);
Then create a product listing block to give access to the correct methods:
$block = Mage::app()->getLayout()->createBlock('catalog/product_list');
Finally run the getReviewsSummaryHtml() method and pass it the product to get the summary HTML.
$html = $block->getReviewsSummaryHtml($product, 'short');
You can do this.
$_product = Mage::getModel('catalog/product')->load($id);
if ($_product->getRatingSummary() && $rating = $this->getReviewsSummaryHtml($_product, 'short')) :
echo $rating;
else:
echo "<a href='$_product->getProductUrl()'>" . $this->__('Be the first to review this') . "</a>";
endif;

Directly go to Product Detail page on click the category?

In my store, one of the category has only one product. Is it possible to take the user directly to the product detail page of this one product whenever they click this category in the nav bar? i want to use some code to get this?
Put this in the product grid or list foreach loop in /app/design/frontend/default/[template]/template/catalog/product/list.phtml
<?php if ($_productCollection->count() == 1) {
$url = $_product->getProductUrl();
Mage::app()->getFrontController()->getResponse()->setRedirect($url); }
?>

Magento - Limit number of products returned on specific pages?

I have some specific pages I am linking to from this page - http://www.formagdev1.com/shop-online.html
Top of page, New to the store and Customer Favorites both link to custom pages, but we'd like to limit the # of products displayed on those pages. Exclusive is fine, it can return a page with pagination as it currently does.
So if you click on New to the store, you'll get to a page that dumps an array with all the products with pagination. I would like to limit the amount of products on this page to 25, with no pagination. Same with Customer Favorites page.
I am using Grid Mode only, and the code I have currently building the array is -
<?php $_collectionSize = $_productCollection->count(); ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid">
<?php endif ?>
The issue I am having is, if I just remove the toolbar top and bottom from the custom page, it removes it from ALL list of grid type pages, so the simple idea in this case doesn't seem to be working.
Is there a way to limit the number of products shown to 25 for these 2 specific pages, with NO pagination?
Any ideas?
Thanks!
Bill
Try limit $collection with:
->setPage(1, 25)
But works only if collection not already initialized.
Mage_Catalog_Block_Product_List is the Block that handles the list display. You could add a function here that defines if this is a no pagination category, say function isNoPagination(). You can then edit the catalog/product/list.phtml template to only display the toolbar when !$this->isNoPagination() and set max collection size to 25 when !$this->isNoPagination().
The function isNoPagination could be based on for example getLayer()->getCategoryId().
Thanks for the suggestions everyone. I ended up just doing this using XML in the deisng of the page, workes as I need it to now :-)

Best way to link to categories from static blocks

What is the best way to link to categories and products from static blocks.
I ask because sometimes we will update URL-keys for categories, etc, which will break links in static blocks to given categories and products.
Is there a better way to link using ids etc so the links will be active regardless of seo-friendly urls?
Currently, I use links such as:
<a href="store.com/generators/generators.html?"generator_package=6>link</a>
When the urls for the categories change the above link would be broken.
How about this? Let me know the result, maybe we can handle other ways.
+++ EDIT - New Code Sample +++
First create a static block which is this block should reference a phtml file that you can put your logic into this file.
{{block type="catalog/navigation" name="catalog.category" template="catalog/category/ListCategory.phtml"}}
Create ListCategory.phtml in the /app/design/frontend/[YOUR_THEME]/template/catalog/category/ directory.
<?php
// get current category id
$curCategory = $this->getCurrentCategory()->getId();
// instance of Mage_Catalog_Model_Category
$category = Mage::getModel(catalog/category)->load((int)$curCategory);
// child category id array
$childCategories = $category->getChildren();
?>

Resources