Sunday, November 28, 2010

Magento development help

Magento Admin Login Problem

comment few lines in following files to solve the admin login problem.

app\code\core\Mage\Core\Model\Session\Abstract\varien.php

$this->getCookie()->getDomain(),
$this->getCookie()->isSecure(),
$this->getCookie()->getHttponly()

then clear cache from var/cache folder

It will work ------------


Magento Home page Layout settings

Go to Admin Panel ->CMS->Pages

Here you can edit XML description .

Tutorial: Setup a new custom theme for your new Magento store



1.Upload these folders to your website:
/skin/frontend/default/NewTheme /app/design/frontend/default/NewTheme
Your folder will be called something other than gNewThemeh.


2.Login to the Admin


3.Navigate to System > Configuration > Design


4.Under the Themes section:
Type the name of your name theme in Templates, Skin and Layout. In this example the name of our theme would be NewTheme.
Type default in the Default textbox
Click Save Config at the top of the screen
Congratulations your new theme is now ready to go!

How to see block structure in front end

We can manage this from admin panel


Go to admin->system->configuration ->developer

select Main website view in Current Configuration Scope:

Under debus section select template path as yes.

it will show the folder structure in front end

Add Home Link with functional active state to Menu Bar (Alternative Method)

Find the file called top.phtml in app/design/frontend/default/yourtheme/template/catalog/navigation/ and make the following change:
  1. class="header-nav-container">
  2. class="header-nav">
  3. class="no-display">
  4. class="home">"getUrl('')?>">








Add Category listing in left sidebar



CATEGORIES



Add Information Pages in left sidebar


getChildHtml() ?>

renderCategoriesMenuHtml(0,'level-top') ?>
getCollection(); ?>


INFORMATION


Add Manufacturer dropdown

/public_html/app/code/core/Mage/Catalog/Block/manufacturer.php

class Mage_Catalog_Block_Product_Manufacturer extends Mage_Core_Block_Template
{
public function getAllManu() {
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', 'manufacturer');
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$manufacturers = $attribute->getSource()->getAllOptions(false);
return $manufacturers;
}
}

In left sidebar


getAllManu() as $manufacturer): ?>




OR

MANUFACTURERS



addFieldToFilter('attribute_code', array('eq'=>'manufacturer'))
->addStoreLabel(Mage::app()->getStore()->getId())
->load();

foreach($collection as $a){
$manufArray = $a->getSource()->getAllOptions(false);
foreach($a->getSource()->getAllOptions(false) as $option)
$manufArray[$option['value']] = $option['label'];
?>


Magento: Setup multiple currency shop


You can easily setup a multiple currency shop in Magento. By multiple currency shop, I mean giving the user to browse products in different currencies. Magento will provide a currency dropdown in category and product listing page. When you select your desired currency, the entire products price will be changed to your selected currency.

Here is a step-by-step procedure to setup multiple currency shop in Magento:-

- Go to System –> Configuration –> Currency Setup

- Under ‘Currency Options‘, select Allowed currencies.

The selected currencies will be displayed in currency dropdown in category and product listing page. Remember that your Base currency and Default display currency selection should also be selected in Allowed currencies.

- Click ‘Save Config‘ button.

- Go to System –> Manage Currency Rates

- Select Import Service. By default it is ‘Webservicex’.

- Click ‘Import‘ button. This will update the currency rates values.

- Click ‘Save Currency Rates‘ button.

- Go to category listing page. You will see currency selection dropdown list in left sidebar at top.

Now, user of your shop can browse product with price in their desired currency.

dditional Scenario:-

Suppose, you have two currencies (British pound and U.S. dollar) in your Magento shop. You also have two stores (Store A and Store B). You want to show British pound in Store A and U.S. dollar in Store B.

Here is the solution:-

- Follow the steps above to setup multiple currency Magento shop.
- You will have multiple currency shop.

- Follow these steps to show store specific currency:-

- Go to System -> Configuration -> GENERAL -> Currency Setup
- In left sidebar at top, you will see “Current Configuration Scope
- Select your desired store from the selection list
- Now, under “Currency Options“, you will see “Default Display Currency
- Select your desired currency from the selection list

You are done. In frontend, select the store for which you did the above changes. You will see the price in your desired currency.

Hope this helps. Thanks.




Currency drop down selector in header, Magento

Step 1: Create a new phtml file and directory


You will need to create a new directory, named “directory” and create a new file called “currency-top.phtml“:

/app/design/frontend/template/default/YOUR-TEMPLATE-NAME/template/directory/currency-top.phtml and write the following code in it :-

getCurrencyCount()>1): ?>






Step 2: Add curency code to directory.xml





Step 3: Add call to header file

getChildHtml('store_language') ?> //for showing language if you wish
getChildHtml('currency_top') ?>






Magento Framework Query

1.Where define database name,user name,and password of website

open magento folder

magento/app/etc/locale.xml

write in locale.xml









false






localhost
root
mysql_password
magento_demo
1








2)How to get the Current url path of any page in Magento

$currentUrl = $this->helper(‘core/url’)->getCurrentUrl();

3)How to get list of all categories in Magento

$categories = Mage::getModel('catalog/category')
->getCollection()

->addAttributeToSelect('*');

4)How to get name of all modules in present Magento Installation

$modules =Mage::getConfig()->getNode('modules')->children();

5)Problems in magento Installation

Most common problem during magento installation is timeout

problem which often occurs . A very simple solution to it is to open your php.ini and navigate to these lines

a)max_execution_time = 60

Change its value to 1800 and second line you will encounter is

max_input_time = 60

Change its value to 1800

Now reinstall magento and most probably time out problem will not trouble you

6)Adding javascript and css on pages ->The Magento way

$headBlock = $this->getLayout()->getBlock(‘head’);
$headBlock->addJs(‘anyfoldername/anyjavascriptname.js’);

7)removing javascript and css on pages ->The Magento way

jscalendar/calendar.js

$this->getLayout->getBlock(‘head’)->removeItem(‘js’, ‘calendar/calendar.js’);

8)Model query in magento or How to insert record in database in magento


class Sapera_Nag_Model_Nag extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init(‘nag/nag’);
}

public function nagmani()
{
$collection = Mage::getResourceModel(‘nag/nag_collection’);
$collection->getSelect()->where(‘status = ?’, 1)->order(‘title’);
return $collection;
}

public function nagVish($title,$nagin_title,$filename,$content,$status)
{
$write = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
$write->query(“INSERT INTO nag(title,nagin_title,filename,content,status,created_time,update_time)VALUES (‘”.$title.”‘,’”.$nagin_title.”‘,’”.$filename.”‘,’”.$content.”‘,’”.$status.”‘, ’2010-11-03 17:08:29′, ’2010-11-04 17:08:32′)”);
return $write;
}

public function nagVish2($title,$nagin_title,$filename,$content,$status)
{
$getModel=Mage::getModel(‘nag/nag’);
$getModel->setNaginTitle($nagin_title);
$getModel->save();
return $getModel;
}

public function getSortColorCategories() {
$collection = Mage::getResourceModel(‘vmfabriccolorcat/vmfabriccolorcat_collection’);
$collection->getSelect()->where(‘status = ?’, 1)->order(‘sort_order’);
return $collection;
}

public function getFabricPartImage($fabricId, $designId, $partId) {
$collection = Mage::getResourceModel(‘vmsuitfabricdesignimage/vmsuitfabricdesignimage_collection’);
$collection->getSelect()->where(‘fabric_id = ?’, $fabricId)->where(‘design_id = ?’, $designId)->where(‘part_id = ?’, $partId);
$data = $collection->toArray();
$fabricImage = (isset($data['items'][0]['filename'])?$data['items'][0]['filename']:”);
return $fabricImage;
}

}

9)-view query in magento or coding of action script in magento

__(‘Module List’) ?>



/*
This shows how to load specific fields from a record in the database.
1) Note the load(15), this corresponds to saying “select * from table where table_id = 15″
2) You can then just use the get(fieldname) to pull specific data from the table.
3) If you have a field named news_id, then it becomes getNewsId, etc.
*/

/* fetch all ID Record*/

$raj = Mage::getModel(‘nag/nag’)->nagmani();

foreach($raj as $ne){
echo $ne['nag_id'].’—’;
echo $ne['content'].’—
’;
}

/* fetch single ID Record*/
/*
$news = Mage::getModel(‘nag/nag’)->load(2);
echo $news->getNewsId();
echo $news->getTitle();
echo $news->getContent();
echo $news->getStatus().’——–1
’;

/*
This shows an alternate way of loading datas from a record using the database the “Magento Way” (using blocks and controller).
Uncomment blocks in /app/code/local/Namespace/Module/controllers/IndexController.php if you want to use it.

*/

/* record show single record from controller*/
$object = $this->getNag();
echo ‘id: ‘.$object['nag_id'].’
’;
echo ‘title: ‘.$object['title'].’
’;
echo ‘nagin_title: ‘.$object['nagin_title'].’
’;
echo ‘content: ‘.$object['content'].’
’;
echo ‘status: ‘.$object['status'].’
’;

/*
This shows how to load multiple rows in a collection and save a change to them.
1) The setPageSize function will load only 5 records per page and you can set the current Page with the setCurPage function.
2) The $collection->walk(‘save’) allows you to save everything in the collection after all changes have been made.
*/

$i = 0;
$collection = Mage::getModel(‘nag/nag’)->getCollection();
$collection->setPageSize(5);
$collection->setCurPage(2);
$size = $collection->getSize();
$cnt = count($collection);
foreach ($collection as $item) {
$i = $i+1;
$item->setTitle($i);
echo $item->getTitle();
}
$collection->walk(‘save’);

/*
This shows how to load a single record and save a change.
1) Note the setTitle, this corresponds to the table field name, title, and then you pass it the text to change.
2) Call the save() function only on a single record.
*/

$object = Mage::getModel(‘nag/nag’)->load(1);
$object->setTitle(‘This is a changed title’);
$object->save();

?>

10:) block query in magento

class Sapera_Nag_Block_Nag extends Mage_Core_Block_Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}

public function getNag()
{
if (!$this->hasData(‘nag4′))
{
$this->setData(‘nag4′, Mage::registry(‘nag4′));
}
return $this->getData(‘nag4′);
}
}

11- controller coading in magento

class Sapera_Nag_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{

/*
* Load an object by id
* Request looking like:
* http://site.com/nag?id=15
* or
* http://site.com/nag/id/15
*/
if(isset($_REQUEST['submit']))
{
echo $title=$_REQUEST['title'];
echo $nagin_title=$_REQUEST['nagin_title'];
echo $filename=’index.jpg’;
echo $content=$_REQUEST['content'];
echo $status=$_REQUEST['status'];
echo $submit=$_REQUEST['submit'];
$rajh = Mage::getModel(‘nag/nag’)->nagVish($title,$nagin_title,$filename,$content,$status);
}
$nag_id = $this->getRequest()->getParam(‘id’);
if($nag_id != null && $nag_id != ”) {
$nag = Mage::getModel(‘nag/nag’)->load($nag_id)->getData();
} else {
$nag = null;
}
/*
* If no param we load a the last created item
*/
if($nag == null) {
$resource = Mage::getSingleton(‘core/resource’);
$read= $resource->getConnection(‘core_read’);
$nagTable = $resource->getTableName(‘nag’);

$select = $read->select()
->from($nagTable,array(‘nag_id’,'title’,'nagin_title’,'content’,'status’))
->where(‘status = ?’,1)
->order(‘created_time DESC’) ;

$nag = $read->fetchRow($select);
}
Mage::register(‘nag4′, $nag);

$this->loadLayout();
$this->renderLayout();
}
}

11-Short path for site folder

1-media url= pup/media:-

img src=”{{media url=”buying.png”}} http://uni-50.com/pup/media/buying.png

————————
2-store url= pup/index.php/nag

  • Product Item


  • http://uni-50.com/pup/index.php/nag

    12:-How to know whether a customer is logged in or not

    Answer) the code below will give a boolean value which will tell whether customer is logged in. The advantage of code is it can be used in all files of magento to get whwther a customer is logged in or not

    Mage::getSingleton(‘customer/session’)->isLoggedIn();

    13)Retrieve Customer’s name which is login

    The query below will give you name of customer which is logged in.The advantage of code is it can be used in all files of magento to get whwther a customer is logged in or not

    Mage::helper(‘customer’)-> getCustomerName();

    14)How to know whether Paypal Module is Enabled or not
    The below code tells whether paypal module is enabled or not
    $modules =Mage::getConfig()->getNode(‘modules’)->();$modulesArray =(array)$modules;
    if(isset($modulesArray['Mage_Paypal')){
    echo "Paypal module exists.";}else{
    echo"Paypal module doesn't exist.";
    }
    This code is to be placed inside a block tag in xml whereever appropriate

    15)How to get current category object and current product object in magento coding

    By below code we can get current category object and then we can get various parameters

    Mage::registry('current_category');

    By below code we can get current product object and then we can get various parameters

    return Mage::registry('current_product')

    Sometimes you may want to change default text in Magento, for example from "My cart" to "My basket", or even change the text into your native language.


     





    Sometimes you may want to change default text in Magento, for example from "My cart" to "My basket", or even change the text into your native language.

    16)How to enable translate Inline option and it's use in magento

    In Magento you can do that easily with a built-in tool: Translate inline. It enables you to make dynamic changes to default text without spending hours working with lines of code.
    To enable Translate inline text, firstly, go to Admin panel ->System ->Configuration ->Developer ->Translate inline.
    Then select Yes for Enable for Frontend
    Now go to frontend and refresh the page. You should see red dotted lined around the text Roll mouse over them, a book icon will appear. Click on this and an editor lightbox window will pop up. Change the text as you want and click “submit”. After that, refresh the page. Remember that if your magento site is in other languages, you should tick Store View Specific.
    And you’ve all done.

    17)How to use collection functions in magento ?

    or

    How to write database query interacting magento ?

    Here some database interaction functions present in the class Mage_Eav_Model_Entity_Collection_Abstract. These collection functions are used to select data from Magento database.

    $collection = Mage::getModel('catalog/product')->getCollection();
    Now if we want to select all attributes :-
    $collection->addAttributeToSelect('*');
    Now if we want to select some specfic attributes
    $collection->addAttributeToSelect(''name', 'url_key', 'color');
    Now we want to give some condition like to produce a collection with status equals to one
    select only those items whose status = 1
    $collection->addAttributeToFilter('status', 1);
    Using Like statement in select queries
    $collection->addAttributeToFilter('sku', array('like' => '%ogi%'));
    Using greater than command in select
    $collection->addAttributeToFilter('id', array('gt' => 15));

    18)How to write custom sql queries in magento ?
    By the below way we can write custom sql queries for select
    query
    getConnection('core_read');
    $query = 'SELECT * FROM ' . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity');
    $results = $read->fetchAll($query);

    print_r($results);
    19)How do we move a certain Block in magento from one position to other

    This article is written for newsletter block and can be applied simlarly for various other magento blocks.By viewing the following guideline, you can change the position of block newsletter from 'left' to 'right', 'footer' to 'header' or 'right' to 'left' and viceversa. The changes are all carried out within the layout.xml located in app\design\frontend\default\your theme\layout\newsletter.xml
    1. You just change the reference name from ‘left’ to ‘right’, or ‘right’ to 'left'


    2. Move block newsletter to 'footer' or 'header'
    Fistly, you change the reference name to 'footer' or to 'header'
    The code is something like




    Now you can change refrence name to right , top etc:-

    20)How to Enabling Template path hint and block hints

    One very useful way of debugging magento code is to enable path hints for blocks and template. By enabling it we will be able to some hint with red background and white text with template path hint on left of a block and on right path of block name responsible for generating the block
    Now we come to how to achieve it. we log in to admin by proper credential then on top menu click on "system" and a dropdown pops up and go extreme down to click "configuration". Then after page opens see the left vertical menu and go extreme down and click on
    "developer". then a page loads with certain options. Now go to "Current Configuration Scope" at top left and in that dropdown choose your websites store view. Now on right clicon debug and change the value of "Template Path Hints" and "Add Block Names to Hints" to yes by drop down and clear the cache and then open index.php of your website

    No comments:

    Post a Comment