Saturday 31 October 2015

How to get Most Viewed Products of Current Product Category in Magento - Magento

Get most viewed products of current product categorywise in magento

$storeId = Mage::app()->getStore()->getId();
$category_ids = $_product->getCategoryIds();

$products = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_ids',array('finset'=>$category_ids))
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->addViewsCount()

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

foreach($products as $product){
    echo $product->getName();
    echo $product->getPrice();
}

Please make sure your passwords match in magento – Magento

From Magento 1.9.1.0 version the users are getting errors at the checkout or from the 'Create an Account link'. When user register,user keep getting the password mistmatch error even though the password is correctly entered.

Form the Magento 1.9.1.0 the validate fuction in the Mage_Customer_Model_Customer class has changed following line.

before 1.9.1.0

$confirmation = $this->getPasswordConfirmation();


After (or from) 1.9.1.0

$confirmation = $this->getConfirmation();


So i was having the login popup extension where in the create function it sets the confirmpassword as follows

$customer->setConfirmation($this->getRequest()->getPost('confirmation'));


and before magento 1.9.1.0 the validate function was taking this setConfirmation value in the validate function as $this->getConfirmation(); but due to change in the validate function to $this->getPasswordConfirmation(); some users are getting the error as “Please make sure your passwords match”.

Due to the method change in core code on Magento 1.9.1.0, if any of the 3rd party extension like 'onepagecheckout' or 'quicklogin' which was using this method, will not work as expected.

To ensure that extensions work as expected with Magento 1.9.1.0, we need to check the version in code & accordingly call the required method –

So i had changed following code in my extension file as follows

$info = Mage::getVersionInfo();
$version = "{$info['major']}.{$info['minor']}.{$info['revision']}.{$info['patch']}";

if($version >= '1.9.1.0'){
$customer->setPasswordConfirmation($this->getRequest()->getPost('confirmation')); // just check the name of confirm password field in the registration form accordlingly add the name in the get Post
} else {
$customer->setConfirmation($this->getRequest()->getPost('confirmation')); // just check the name of confirm password field in the registration form accordlingly add the name in the get Post
}

List all or get all enabled or disabled products in magento

Lets look at query to get disabled product or enabled products collection in magento

to get list of enabled products use following query

$productsCollection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToFilter('status', 1)


to get list of disabled products use following query

$productsCollection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToFilter('status', 2)

Friday 30 October 2015

How to override createPost Action in Account Controller in Magento

Here we are going to override createpost function in magento

Lets create a small module for overriding the account controller

Step 1:- Create YourPackageName_Customer.xml file at app/etc/

<?xml version="1.0"?>
<config>
  <modules>
    <YourPackageName_Customer>
      <active>true</active>
      <codePool>local</codePool>
    <YourPackageName_Customer>
  </modules>
</config>


Step 2:- Create module's config.xml file at app/code/local/YourPackageName/Customer/etc/

Let's take a look at the code that goes into the config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <YourPackageName_Customer>
            <version>0.0.1</version>
        </YourPackageName_Customer>
    </modules>
    <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <yourpackagename_customer before="Mage_Customer">YourPackageName_Customer</yourpackagename_customer>
                    </modules>
                </args>
            </customer>
        </routers>
    </frontend>
</config>


In the config node its child has to define whether we are changing the frontend or admin file and we define router node that will override core customer module with its arguments. In the bottom node goes the current module frontend name (<yourpackagename_customer in my example) with the "before" or "after" attribute name with the value of which module is being overriden (Mage_Customer) and our own module name inside the tags (YourPackageName_Customer).

Step 3:- Create AccountController.php controller file at app/code/local/YourPackageName/Customer/controllers/AccountController.php

If you noticed, there are no strict files defined. With this we have defined only the path that will look for the controller files by their names. So if we would like to override app/code/core/Mage/Customer/controllers/AccountController.php a file with the same name will have to be created (app/code/local/YourPackageName/Customer/controllers/AccountController.php).

Class definition inside our new file:

<?php
    include_once("Mage/Customer/controllers/AccountController.php");
    class YourPackageName_Customer_AccountController extends Mage_Customer_AccountController
    {
        public function createPostAction()
        {
            // echo "done"; exit;
        }
    }
?>

How to override create new Product Controller in Magento Admin Panel - Magento

Here we are going to override create new product function in magento during admin product creation

Lets create a small module for overriding the product controller

Step 1:- Create YourPackageName_Adminhtml.xml file at app/etc/

<?xml version="1.0"?>
<config>
  <modules>
    <YourPackageName_Adminhtml>
      <active>true</active>
      <codePool>local</codePool>
    <YourPackageName_Adminhtml>
  </modules>
</config>

Step 2:- Create module's config.xml file at app/code/local/YourPackageName/Adminhtml/etc/

Let's take a look at the code that goes into the config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <YourPackageName_Adminhtml>
            <version>0.0.1</version>
        </YourPackageName_Adminhtml>
    </modules>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <yourpackagename_adminhtml before="Mage_Adminhtml">YourPackageName_Adminhtml</yourpackagename_adminhtml>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

In the config node its child has to define whether we are changing the frontend or admin file and we define router node that will override core adminhtml module with its arguments. In the bottom node goes the current module frontend name (<yourpackagename_adminhtml in my example) with the "before" or "after" attribute name with the value of which module is being overriden (Mage_Adminhtml) and our own module name inside the tags (YourPackageName_Adminhtml).

Step 3:- Create ProductController.php controller file at app/code/local/YourPackageName/Adminhtml/controllers/Catalog/ProductController.php

If you noticed, there are no strict files defined. With this we have defined only the path that will look for the controller files by their names. So if we would like to override app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php a file with the same name will have to be created (app/code/local/YourPackageName/Adminhtml/controllers/Catalog/ProductController.php).

Class definition inside our new file:

<?php
    include_once("Mage/Adminhtml/controllers/Catalog/ProductController.php");
    class YourPackageName_Adminhtml_Catalog_ProductController extends Mage_Adminhtml_Catalog_ProductController
    {
        public function newAction()
        {
            // echo "done"; exit;
        }
    }
?>

Tuesday 20 October 2015

Magento - dbModel read resource does not implement Zend_Db_Adapter_Abstract

We came across the following error in magento after git pull of the code from the git and later when i refreshed the site i got the error and when i saw the reports file for error i got this error "dbModel read resource does not implement Zend_Db_Adapter_Abstract"

the solution for this to get rid of this error is to clear the cache and delete the cache folder from the var/ .

Done refresh the site your error has gone

How to override Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Type_Select block in Magento

Sometimes you need to change the behavior of Magento core functionality and add our custom code and it's a bad habit and bad programming if core files are modified and it's not recommended at all.Magento brought in an excellent way how to override/overwrite those files for adding our custom code.

Overriding Magento Blocks

Here we will see how to change a basic block of Magento. Suppose that in Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Type_Select class we need to add some changes have to be done. What we would like to have here is to add our own file that will extend the original file with all of its methods. All the job that has to be done is to add the code below in the current module's config.xml:

Step 1:- Start rewriting the Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Type_Select block in config.xml of your custom module

<config>
<global>
<blocks>
<adminhtml>
            <rewrite>
                <catalog_product_edit_tab_options_type_select>
                    [YourPackageName]_[YourModuleName]_Block_Adminhtml_Catalog_Product_Edit_Tab_Options_Type_Select
                </catalog_product_edit_tab_options_type_select>
            </rewrite>
        </adminhtml>
</blocks>
</global>
</config>

Step 2:- create the file and add your code modification in this class of the respective method from the core block class.

As the class name says, a file has to be created in app/code/local/YourPackageName/YourModuleName/Block/Adminhtml/Catalog/Product/Edit/Tab/Options/Type/Select.php and a class has to be defined:

class [YourPackageName]_[YourModuleName]_Block_Adminhtml_Catalog_Product_Edit_Tab_Options_Type_Select extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Type_Select{
// your code
}

How to solve Jquery and Prototype conflicts in Magento.

Magento uses Prototype JS.Most of us prefer working with jQuery because of many reasons.So when we use Jquery in magento we get the error. this error is basically due to the conflict between prototype and jquery libraries.

When we call a jQuery with $ we will notice a conflict because Prototype.js also uses $ variable for itself whereas jQuery tries to register the variable causing conflict.So, if you have to use jQuery within a Magento store which rely heavily on Prototype.js just put your jQuery in compatibility mode.

The default call for jQuery function goes like this

$(document).ready(function(){
$("#someid").click(function(){

});
});

The above function will not work within a Magento store due to conflict with Prototype.js. You need to solve this conflict.

Instead follow the steps

1) Create one js file called noConflict.js with the following content.

var $j = $.noConflict();

and include this file after your jquery.js in the magento

2) And you can use this jquery wih the following code

<script type="text/javascript">
    $j(document).ready(function(){
        $j("#someid").click(function(){

});  
    });
</script>

Since we are using jQuery in compatibility mode it’s wiser not to call $ variable instead make a call to $j which is reference of jQuery.noConflict();. Simple isn’t it? Keep in mind that call to jQuery should be after the call to Prototype.

Friday 16 October 2015

How to change magento default admin url – Magento

To protect your magento website backend against hackers we recommend you to change the default url of the magento admin.It is easy to change the admin url from default http://www.yourdomainname.com/admin or http://www.yourdomainname.com/index.php/admin to yourcustom admin url

In the following steps we are going to see how to have custom admin url name of our magento backend

Follow these steps to change the admin URL/path.

Step 1 – Change Path

Open the local.xml from the app/etc/ directory and locate the following content

<admin>
<routers>
<adminhtml>
<args>
<frontName><![CDATA[admin]]></frontName>
</args>
</adminhtml>
</routers>
</admin>

Now replace admin from <frontName><![CDATA[admin]]></frontName> with your new admin url key.This should be something hard to guess,then save the file.I assume you has changed to <frontName><![CDATA[yournewadminpath]]></frontName>.

Step 2 – Refresh Cache

Now Refresh the cache of the site from the backend or you can mannually delete the content of the var/cache dir from your ftp.

Step 3 – Complete

You have done now.you can check by visiting http://www.yourdomainname.com/yournewadminpath (yournewadminpath is the name of urlkey which you gave on the step 1).

if you try http://www.yourdomainname.com/admin you will get 404 Page not found.

Note: Do NOT use the Magento Backend in the Magento Admin Panel to change the admin URL, as this is known to cause problems.