Magento Special price products page
Magento special price products page. We will be creating a new CMS page that will display all the Special or Sale products. We can make a product as a Special by filling it’s “Special From” and “Special To” price in “Prices” tab in Manage Products individual screen.
So let’s first create CMS Page, by going to CMS > Pages, which we will name it as “Specials”. In the Content tab of that Page, paste the below line of code:
{{block type="catalog/product_special" template="catalog/product/list.phtml" column_count="3" num_products="0"}} |
and save the page.
– Here we are saying Magento to display Product List template by looking at our new block type file, Catalog/Product/Block/Special.php. So let’s create this file, Special.php in local/Mage/Catalog/Product/Block/ directory. You will have to create this directory path if it’s not already there.
<?php | |
class Mage_Catalog_Block_Product_Special extends Mage_Catalog_Block_Product_List | |
{ | |
protected function _getProductCollection() | |
{ | |
if (is_null($this->_productCollection)) { | |
$categoryID = $this->getCategoryId(); | |
if($categoryID) | |
{ | |
$category = new Mage_Catalog_Model_Category(); | |
$category->load($categoryID); // this is category id | |
$collection = $category->getProductCollection(); | |
} else | |
{ | |
$collection = Mage::getResourceModel('catalog/product_collection'); | |
} | |
$todayDate = date('m/d/y'); | |
$tomorrow = mktime(0, 0, 0, date('m'), date('d')+1, date('y')); | |
$tomorrowDate = date('m/d/y', $tomorrow); | |
Mage::getModel('catalog/layer')->prepareProductCollection($collection); | |
$collection->addAttributeToSort('created_at', 'desc'); | |
$collection->addStoreFilter(); | |
$collection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate)) | |
->addAttributeToFilter('special_to_date', array('or'=> array( | |
0 => array('date' => true, 'from' => $tomorrowDate), | |
1 => array('is' => new Zend_Db_Expr('null'))) | |
), 'left'); | |
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0; | |
$collection->setPage(1, $numProducts)->load(); | |
$this->_productCollection = $collection; | |
} | |
return $this->_productCollection; | |
} | |
} |
Magento client denied by server configuration notice
Magento client denied by server configuration: /var/www/magento/app/etc/local.xml .. This is not the error, but just a message type thing displayed in apache error log and firebug console. Nothing to worry here, it’s just a security check from web server and you should ignore it.
If you don’t like it then you can turn it off by writing few lines of code in app/design/adminhtml/default/default/layout/local.xml
<layout> | |
<default> | |
<remove name="notification_security" /> | |
<remove name="notification_survey" /> | |
</default> | |
</layout> |
Clear cache as usual, and you should get rid of this message.
Magento: Can’t see product images in category page
Set product image as small image and thumbnail programatically
If you see your images in product detail page but don’t see it in category page, it’s because your product have images (image attribute filled) but not small_image and thumbnail which are required to display on category page. If there are only few products you want to set small_image and thumbnail, then it’s easy to go to Manage Products > Individual Product > Images tab > Set small image and thumbnail radio button. But when it comes to hundreds/thousands of products, you better want it programatically way.
Make a file in your Magento root and place the below code in it. Then run that file and you have just copied your Product Image to small image and thumbnail as well!
<?php | |
ini_set('display_errors',1); | |
require 'app/Mage.php'; | |
Mage::app(); | |
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*'); | |
foreach ($products as $product) { | |
if (!$product->hasImage()) continue; | |
if (!$product->hasSmallImage()) $product->setSmallImage($product->getImage()); | |
if (!$product->hasThumbnail()) $product->setThumbnail($product->getImage()); | |
$product->save(); | |
} | |
echo 'Finished!'; | |
?> |
Magento: Reset download limit for downloadable product item/order
In Magento, downloadable products are the products which can be downloaded and does not require any shipping. When customer purchases this type of products, they get certain download limits to download their purchased items. This can be set in Products page in admin under “Downloadable Products” tab. In many cases store owner only want to give certain amount of downloads, so customers can’t download beyond that limit. But it may happen that due to bad internet or some interrupts downloads can’t be successful and customer want to reset that limit. For doing this, there is no option in backend to reset the download limits for the downloadable product items or for an entire order, and unfortunately admin use to re-order to achieve this.
Here is the query which will reset download limits for an entire order.
$q = Mage::getModel('sales/order_item')->getCollection()->addFieldToSelect('order_id'); | |
$q->getSelect()->joinRight('downloadable_link_purchased_item as d', 'main_table.item_id = d.order_item_id', | |
array('item_id','number_of_downloads_used')); | |
$q->getSelect()->where('main_table.order_id = '. (int)$orderId); | |
$q->load(); | |
//echo $q->getSelect()->__toString();exit; | |
$items = $q->getColumnValues('item_id'); | |
foreach($items as $id) { | |
Mage::getModel('downloadable/link_purchased_item')->load($id) | |
->setStatus('available')->setNumberOfDownloadsUsed(0)->save(); | |
} |
The raw SQL query for the above Magento Convention query is:
UPDATE sales_flat_order_item s RIGHT JOIN downloadable_link_purchased_item d ON s.item_id = d.order_item_id SET d.number_of_downloads_used = 0 WHERE s.order_id = $orderId;
Continue reading »
Magento: Add product custom attribute options dynamically
Once you have the product attributes in place, and you want to add lot of options to these attributes it becomes time consuming and hectic task. If you have only few attribute options to add, it’s easy to go to backend and add manually. But if you have many attribute options, more than 10 options for many attributes, it’s very tiresome and feels like data entry job. Magento is not built to do stuff manually, so here I will provide you the code snippet which will push all the attribute options to their respective attribute.
Create a file in the root of your Magento installation and copy this code there. I generally name it test.php
require_once 'app/Mage.php'; | |
umask(0); | |
Mage::app('default'); | |
$installer = new Mage_Eav_Model_Entity_Setup('core_setup'); | |
$installer->startSetup(); | |
//Change this.... | |
$aColors = array('PINK','BLACK','BLUE','BROWN','GOLD','GRAY','GREEN','MULTI','NEUTRALS/CREAM/WHITE','ORANGE','PURPLE','RED','RUST','TAUPE','YELLOW'); | |
$attrCode = 'color'; //this should be there already | |
//........ | |
$aColors = array_map('utf8_encode',$aColors); //If you're using special characters | |
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId(); | |
$aOption = array(); | |
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, $attrCode); | |
for($iCount=0;$iCount<sizeof($aColors);$iCount++){ | |
$aOption['value']['option'.$iCount][0] = $aColors[$iCount]; | |
} | |
$installer->addAttributeOption($aOption); | |
$installer->endSetup(); |
Now you just need to give the existing product attribute name AND future attribute options to this script. Run it and rest all script will do for you! 🙂
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)