Magento Resource | E-Commerce Generation Explorer

TAG | block

Here are codes which you can put on the block to show your best seller products.

Bestseller.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
//bestseller module - grabs from all products, returns in order of total quantity ordered
class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract
{
public function __construct()
{
parent::__construct();
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel(’reports/product_collection’)
->addOrderedQty()
->addAttributeToSelect(array(’name’, ‘price’, ’small_image’, ’short_description’, ‘description’, ‘author’))
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder(’ordered_qty’, ‘desc’);
Mage::getSingleton(’catalog/product_status’)->addVisibleFilterToCollection($products);
Mage::getSingleton(’catalog/product_visibility’)->addVisibleInCatalogFilterToCollection($products);
//$products->setPageSize(6)->setCurPage(1);
$this->setProductCollection($products);
}
}
?>

These blocks do not represent all there is to making use of them. These should all work with “List.phtml” in the Catalog module design files, but you might want to create your own template.

The easiest way to implement these and make them usable is to add them to a Mage folder within the Local code folder (rather than core):
app/code/local/Mage/Catalog/Block/Product/Bestseller.php

This lets you skip the step of creating a custom module in order to use a simple block who’s only purpose is to list a category of products (or bestsellers in this case).

Codes from http://www.exploremagento.com

·

Jun/09

25

What’s in a block?

Many developers are familiar with the MVC (Model View Controller) design pattern that is seemingly ubiquitous throughout web frameworks. Looking through the code in Magento, however, we see many other components besides the M’s the V’s and the C’s.

Each module  (a “module” meaning the separate directories within the “app/code/core/Mage” directory that comprise of Magento’s different functional areas) contains the usual Controller and Model. You’ll see that within each module, there are no Views (more on this later). You’ll also see extra tidbits, such as “helper” , “etc” and “sql”. These are (and are not) standard within the Zend Framework context and will not be discussed in this article. In these modules are also the sort of files which we work with very often. The all powerful block! This article will attempt to (hopefully accurately) describe just what a Block is and how it’s used.

A top priority of (good) Object Oriented Programming is the decoupling of code. In a nut shell, this means that code should have the least amount of dependency on other code as possible. (Slightly more accurately, objects should not rely too heavily on other objects to exist and function). Blocks are part of Magento’s solution to keep their application loosely coupled.

(more…)

·

Theme Design by devolux.nh2.me