So guys, I'm doing this task for something very important to me but now I'm completely stuck.
I need to fulfill this task:
Create a module to output a nice list of the products that have a value in the position field. The list should be sorted according to the value in the position field, lowest number shows first. The list should contain the product name and a picture of the product. When you click on the product name or picture you should get to the product detail view. Then Add a button somewhere on the page that reverses the sort order when you click it.
So, to sum up, I have 5 products with a custom attribute(this is a textfield and the code is position_new), 3 of the products have values in this field(1,2,3) and 2 don't. I need to display only the products which have value in this field, then sort the products by the values in this field(position_new).
So far I've managed to create a similiar module that displays the products that contain value in the custom attribute, in this case color, but I can't sort the list of products and create the button. It's been two days only researching this but so far I found nothing helpful and I'm running against time.
This is my code, and if someone can help me with this will have my forever gratitude and will completely change my life, so please please anyone.
app/etc/modules/Atwix_Cmsattr.xml
<?xml version="1.0"?>
<config>
<modules>
<Atwix_Cmsattr>
<active>true</active>
<codePool>local</codePool>
</Atwix_Cmsattr>
</modules>
</config>
app/code/local/Atwix/Cmsattr/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Atwix_Cmsattr>
<version>0.1.0</version>
</Atwix_Cmsattr>
</modules>
<global>
<blocks>
<atwix_cmsattr>
<class>Atwix_Cmsattr_Block</class>
</atwix_cmsattr>
</blocks>
<models>
<atwix_cmsattr>
<class>Atwix_Cmsattr_Model</class>
</atwix_cmsattr>
</models>
</global>
</config>
app/code/local/Atwix/Cmsattr/Block/List.php
<?php
class Atwix_Cmsattr_Block_List extends Mage_Catalog_Block_Product_Abstract
{
protected $_itemCollection = null;
public function getItems()
{
$color = $this->getColor();
if (!$color)
return false;
if (is_null($this->itemCollection)) {
$this->_itemCollection = Mage::getModel('atwix_cmsattr/products')->getItemsCollection($color);
}
return $this->_itemCollection;
}
}
app/code/local/Atwix/Cmsattr/Model/Products.php
<?php
class Atwix_Cmsattr_Model_Products extends Mage_Catalog_Model_Product
{
public function getItemsCollection($valueId)
{
$collection = $this->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('color', array('eq' => $valueId));
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
return $collection;
}
}
app/desing/frontend/rwd/default/template/atwix/cmsattr/list.phtml
<?php $_items = $this->getItems() ?>
<div class="block">
<div class="block-title">
<strong><span><?php echo $this->__('Red Products') ?></span></strong>
</div>
<div class="category-products">
<ul class="products-grid products-grid--max-3-col first last odd">
<?php foreach ($_items as $_item): ?>
<li class="item last">
<a href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(210) ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
</a>
<div class="product-info" style="padding-bottom: 88px; min-height: 151px;">
<h2 class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></h2>
<?php echo $this->getPriceHtml($_item, true) ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
to$collection = Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);