Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Created September 13, 2013 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tegansnyder/6553587 to your computer and use it in GitHub Desktop.
Save tegansnyder/6553587 to your computer and use it in GitHub Desktop.
Creating a Free Shipping Category in Magento. Step 1: create a basic Catalog Category in the Magento admin. Take note of the category_id. Step 2: Copy the Core file app/code/Core/Mage/Catalog/Block/Product/List.php to your local directory app/code/local/Mage/Catalog/Block/Product/List.php Step 3: Modify the _getProductCollection() function like …
<?php
/*
Rest of the file above this
app/code/local/Mage/Catalog/Block/Product/List.php
*/
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$layer = $this->getLayer();
/* @var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
}
/* My Addition */
$cat_data = $layer->getCurrentCategory()->getData();
$cat_id = $cat_data['entity_id'];
if ($cat_id == 471) { // WHAT IS THE CATEGORY ID YOU WNAT TO SHOW ONLY FREE SHIPPING PRODUCTS
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('free_shipping_discount', array('eq' => '1083')) // I named my attribute free_shipping_discount. Value 1083 = 'Yes'. Trick you can see Attribute ID values in the Magento Admin.
->addAttributeToSelect('*');
$collection->load();
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$layer->setProductCollection($collection);
$this->_productCollection = $collection;
} else {
// stock Magento
$this->_productCollection = $layer->getProductCollection();
}
/* End My Addition */
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
}
return $this->_productCollection;
}
/*
Rest of the file below this
app/code/local/Mage/Catalog/Block/Product/List.php
*/
?>
@tegansnyder
Copy link
Author

<?php
$category = Mage::getModel('catalog/category')->load(471); // your category

$visibility = array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG);

$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('free_shipping_discount', array('eq' => '1083'))
->addCategoryFilter($category)
->addAttributeToFilter('visibility', $visibility)
->addAttributeToSelect('*');

$collection->load();

Mage::getModel('catalog/layer')->prepareProductCollection($collection);
?>

@tegansnyder
Copy link
Author

<?php
//best sellers by category

$category = Mage::getModel('catalog/category')->load(471); // shop category

$visibility = array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG);

$current_page = Mage::getBlockSingleton('page/html_pager')->getCurrentPage();

$collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addCategoryFilter($category)
    ->addAttributeToFilter('visibility', $visibility); 

$collection->getSelect()
    ->joinLeft(
        'sales_flat_order_item AS order_item',
        'e.entity_id = order_item.product_id',
        'SUM(order_item.qty_ordered) AS ordered_qty')
    ->group('e.entity_id')
    ->order('ordered_qty DESC')
    ->limit(25);

$collection->load();

Mage::getModel('catalog/layer')->prepareProductCollection($collection);

$this->_productCollection = $collection;

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment