Browsing articles in "Magento frontend"
Apr 28, 2013
kalpesh

Magento join EAV collection with Flat table

Joining tables in Magento when it comes to EAV with Flat table is quite complicated. Consider you want to join sales_flat_order table with customer EAV tables to get Customer’s firstname and lastname, it becomes difficult as customer’s name comes from customer_entity_varchar table.

Below code will join sales order flat table with customer EAV to get customer’s full name in the collection along with all the order details.

1
2
3
4
5
6
7
8
9
10
11
12
13
$coll = Mage::getModel('sales/order')->getCollection();

$fn = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'firstname');
$ln = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'lastname');

$coll->getSelect()
    ->join(array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.customer_id', array('firstname' => 'value'))
    ->where('ce1.attribute_id='.$fn->getAttributeId()) 
    ->join(array('ce2' => 'customer_entity_varchar'), 'ce2.entity_id=main_table.customer_id', array('lastname' => 'value'))
    ->where('ce2.attribute_id='.$ln->getAttributeId()) 
    ->columns(new Zend_Db_Expr("CONCAT(`ce1`.`value`, ' ',`ce2`.`value`) AS fullname"));

print_r($coll->getData());
Apr 25, 2013
kalpesh

Magento: Check if any particular customer is currently logged in

Let’s say you want to check if any particular customer is currently logged in to your site or not. Or let’s check how many customers with their customer IDs and other activities are online on your store.

This little script will help you in finding all the currently active OR any particular customer(s) active in your store.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require "app/Mage.php";
umask(0);
Mage::app();

$collection = Mage::getModel('log/visitor_online')->prepare()->getCollection();

//Get all the customers that are logged in......
foreach($collection->getData() as $cust) {
        echo 'Customer ID: '.$cust['customer_id'] . '<br/>';
        echo 'Last URL visited: '.$cust['last_url'] . '<br/>';
        echo 'First visit: '.$cust['first_visit_at'] . '<br/>';
        echo 'Last visit: '.$cust['last_visit_at'] . '<br/>';
        echo '======================<br/>';
}

//Get any particular customer, if he's currently logged in or not.....
$collection->addFieldToFilter('customer_id', 5)->addCustomerData(); //5 is customer ID of customer you want to check
if($collection->count()) {
    echo 'Customer is logged in';
} else {
    echo 'Customer is NOT logged in';
}
Apr 25, 2013
kalpesh

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:

1
{{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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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;
    }
}
Feb 26, 2013
kalpesh

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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!';
?>
Feb 23, 2013
kalpesh

Magento: Product Free/Paid SAMPLE Purchase Order

If you want to allow your customers to order for sample of any product, before purchasing the actual product, it becomes very tough as in a product page you can’t have two products (sample + full) allowed. But it’s not a good idea to display sample products as Individually, it makes sense only in the actual product page just near the Add To Cart button.

Magento Sample product purchase

But the question is, how to have TWO SKUs in a single product detail page? Here is the code that will do exactly what you want now. Open your catalog/product/view.phtml template file and put this code just below the code “$this->getChildHtml(‘addtocart’);”

1
<button type="button" style="float:left;padding-left:5px;" onclick="window.location.href='/order_sample.php?sku=<?php echo $this->htmlEscape($_product->getSku()) ?>'" title="Request Sample for < ?php echo $this->htmlEscape($_product->getName()) ?>" class="button btn-cart" ><span><span>Order Sample - $1</span></span></button>

Continue reading »

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions