Browsing articles in "Magento"
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.

$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.

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:

{{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;
    }
}
Apr 18, 2013
kalpesh

pdo_mysql extension is not installed

PHP Mysql Error: pdo_mysql extension is not installed.

Magento needs PDO Mysql extension for database connection and related things, so if you don’t have pdo_mysql extension enabled Magento will complain about this and will not proceed further installation. If you are not sure what PDO is, it’s high time for you to look at http://php.net/manual/en/ref.pdo-mysql.php

Coming back to error, to resolve this you will need to edit your php.ini file where it says:
;extension=pdo_mysql.so

Just uncomment the line by removing front semincolon, so it becomes
extension=pdo_mysql.so

Save it and restart the server, the error should go.

If you are on Windows, then that line should read:

extension=php_pdo_mysql.dll

If you don’t find pdo_mysql in php.ini, install php5-mysql by running the command:

sudo apt-get install php5-mysql (on Ubuntu)

sudo yum install php-mysql (on Redhat, Fedora, CentOS)

Apr 18, 2013
kalpesh

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.

Pages:«1...13141516171819...29»

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