Showing posts with label Magento Frontend. Show all posts
Showing posts with label Magento Frontend. Show all posts

Thursday, October 10, 2013

Script for to remove the cache

make one cache-clear.php file into your root folder and paste following code:
  require_once (“app/Mage.php”); 
 umask(0); 
 Mage::run(); 
 Mage::app()->getCache()->clean(); 
 exit(“done”); 
 ?> 
can call that file with :http://yourdomainname/cache-clear.php

How To Setup Multiple Magento Stores and Web-site

There are numerous ways to setup multiple Magento stores that all share the same codebase and backend, but what method you use depends on your needs.
This article is written with cPanel in mind, though the methodologies listed below apply no matter what control panel you’re using. You will need Magento 1.4.x or greater installed too.
Jump To Section
(A)        Multi-Web-Site, Multi-Domain Setup :-
Follow Multi Web-Site and Multi Store Instructions.
1: Categories
First, will need to create our Categories.  Since all three websites will be sharing the same catalog, we will be using the default root Category in Catalog -> Categories -> Manage Categories and will be creating our categories under that root category (i.e. Clothing, Electronics, etc.).
These categories (Clothing, Electronics) should be set as both “Is Active” from the General Information tab and “Is Anchor” from the Display Settings tab for them to appear on the frontend of your Magento shop.  (***NOTE: If the websites will not be sharing the same catalog, a Root Category must be created for each website.  Thus, if there are 3 websites, there will be 3 Root Categories with subcategories under them.)
2: Store Configuration in the Magento Admin
1. Now that we have created our Categories, it’s time to create our websites by going to System -> Manage Stores and clicking the “Create Website” button.
    Name – domain name of our new website
    Code – a parameter that will be used in configuring the Apache web server to point to that particular domain name
2. Once the website has been created, we’ll create the store corresponding to this website by clicking on the “Create Store” button in System -> Manage Stores.
    Website – website to which this store will be associated
    Name – the same as the website name
    Root Category – the root category that will be used for this store. (Refer to Step 1 for Details)
3. Then, we create the store view which is the interface that the customer will be able to access on the frontend.  Click the “Create Store View” button in System -> Manage Stores.
    Store – store to which this view will be associated
    Name – name of this store view (i.e. English Version, German Version, etc.)
    Code – code for this store view
    Status – if enabled, this store view will be accessible from our frontend, otherwise, it will not be accessible
4. After the Store has been created, we need to configure the Unsecure Base URL and Secure Base URL under System -> Configuration -> General -> Web.  Before we set their respective base URLs, we first need to ensure that the configuration scope is set to the domain1.com website to define which site we are working on.
Then, we modify the base URLs for both Unsecure:
and Secure:
with the corresponding domain name by unchecking the ”Use default [STORE VIEW]” checkbox and then save the configuration.
5. Now we just repeat Steps 2-4 for the other two websites, domain2.com and domain3.com by replacing the fields with their respective information.
3: Store Configuration in the Server
(B)  Two Way of Store Configuration :- Choose as Like You .
First Way :-
1: Store Configuration in the Server Side.
1. Now we re-configure the Apache configuration file, httpd.conf, for all domains to set the DocumentRoot to our Magento directory.  In this case, the directory is /var/www/http

    ServerAdmin webmaster@domain1.com
    DocumentRoot /var/www/http
    ServerName domain0.com

    ServerAdmin webmaster@domain2.com
    DocumentRoot /var/www/http
    ServerName domain1.com

    ServerAdmin webmaster@domain3.com
    DocumentRoot /var/www/http
    ServerName domai2.com
2. Edit the .htaccess file at /var/www/http/.htaccess and add the following lines below:
SetEnvIf Host www\.domain1\.com MAGE_RUN_CODE=domain1_com
SetEnvIf Host www\.domain1\.com MAGE_RUN_TYPE=website
SetEnvIf Host ^domain1\.com MAGE_RUN_CODE=domain1_com
SetEnvIf Host ^domain1\.com MAGE_RUN_TYPE=website
SetEnvIf Host www\.domain2\.com MAGE_RUN_CODE=domain2_com
SetEnvIf Host www\.domain2\.com MAGE_RUN_TYPE=website
SetEnvIf Host ^domain2\.com MAGE_RUN_CODE=domain2_com
SetEnvIf Host ^domain2\.com MAGE_RUN_TYPE=website
SetEnvIf Host www\.domain3\.com MAGE_RUN_CODE=domain3_com
SetEnvIf Host www\.domain3\.com MAGE_RUN_TYPE=website
SetEnvIf Host ^domain3\.com MAGE_RUN_CODE=domain3_com
SetEnvIf Host ^domain3\.com MAGE_RUN_TYPE=website
3. Restart Apache Server
If you are on a Red Hat based distribution, you’ll be able to type service apache restart.  For other distributions, you’ll want to type apachectl restart.  (***NOTE: The second option here is different than “apachectl graceful” which will run a graceful restart and reload configuration files, without terminating current connections.  We don’t have any visitors to our site yet, so it’s okay to do a “apachectl restart”.)
4: We’re Ready to Go!
After we’ve complete all of these steps we should now see all 3 domains in our backend:
All that’s left now is to add products to the catalog and give each site a custom theme if we wish to do so.  Many people are taking advantage of Magento’s powerful multi-store functionality, whether it’s to set up stores with multiple languages, different catalogs and even the same catalogs (as in our example) with different front ends as a marketing vehicle.  No matter which you decide to use Magento’s powerful features for, we hope this post will provide a good starting point to help you get there!
Second Way :-
1: Web-Site &Store Configuration in the Index.php .
Complete Create  Root category And Website, store, store view then process  again. Open index.php in magento  Root Folder some changes Doc file according.
First,  Find this line code and replace.
Find  = $mageFilename = MAGENTO_ROOT . ‘/app/Mage.php’;
Replace = $mageFilename = MAGENTO_ROOT . ‘../app/Mage.php’;
Second ,  Find this line code and replace.
Find  =  Mage::run($mageRunCode, $mageRunType);
Replace  =
$i=$_SERVER['REQUEST_URI'];
switch ($i):
    case ‘www.domain1.com’:
         Mage::run(“domain1″,”website”);
        break;
    case ‘www.domain2.com’:
         Mage::run(“domain2″,”website”);
        break;
    case ‘www.domain3.com’:
         Mage::run(“domain3″,”website”);
        break;
    default:
       Mage::run(“default”,”website”);
endswitch;
After Completed two major change save index.php file in root folder. Delete all cache and Re-indexing admin panel. After restart apache server. Then check our Multi-domain website is ready for running.
Magento’s provide  powerful multi-store functionality . I hope this instruction  will provide a good starting point to help you get there!
End .

Saturday, October 5, 2013

Magento add custom textbox,select box to existing system -> configuration tab.

if we want to add custom fields like textbox,select box to existing system -> configuration tab -> catalog.

<?xml version="1.0"?>
<config>
   <sections>
        <catalog>
            <groups>
                <frontend>
                    <fields>
                        <mayankpatel translate="label">
                            <label>Custom Textbox Value</label>
                            <frontend_type>text</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </mayankpatel>
                      </fields>
                </frontend>
            </groups>
        </catalog>
    </sections>
</config>

Tuesday, October 1, 2013

Magento replace quantity text box to select box

In the addtocart.phtml file find the following code(around line 33)
<input name="qty" type="text" class="input-text qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" />



Replace with this code:

This code shows the “Available Qty for Product”.
<select class="input-text qty" name="qty" id="qty">
  <?php $i = 1 ?>
  <?php do { ?>
    <option value="<?php echo $i?>">
      <?php echo $i?>
      <?php $i++ ?>
    </option>
    <?php } while ($i <= (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()) ?></select>


This code shows the “Maximum Qty Allowed in Shopping Cart”.

<select class="input-text qty" name="qty" id="qty">
  <?php $i = 1 ?>
  <?php do { ?>
    <option value="<?php echo $i?>">
      <?php echo $i?>
      <?php $i++ ?>
    </option>
    <?php } while ($i <= (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getMaxSaleQty()) ?></select>

Magento – Delete all cancelled magento orders

$collection = Mage::getResourceModel('sales/order_collection')
            ->addAttributeToSelect('*')
            ->setPageSize(1000)
            ->addFieldToFilter('status', 'canceled')
     ->load();

foreach ($collection as $col) {
  Mage::log($col->getIncrementId() . ' order deleted ');
    try {
         $col->delete();
    } catch (Exception $e) {
        throw $e;
    }
}

How to Add Custom Class to top links

To Add Custom Class to top links  in Magneto

1) Open XML files for Top links And Add following tag

<reference name=”top.links”>
<action method=”addLink” translate=”label title” module=”customer”><label>Account</label><url helper=”customer/getAccountUrl”/><title>Account</title><prepare/><urlParams/><position>10</position><liParams/>
<aParams>class=”top-link-account”</aParams></action>
</reference>

SELECT,INSERT,UPDATE,AND DELETE DATA (CRUD) IN MAGENTO

I have a database table named “test” with the following fields given below:

SELECT DATA

$model = Mage::getModel('test/test');
$collection = $model->getCollection();
foreach($collection as $item){
    print_r($item->getData());
    print_r($item->getTitle());
}

INSERT DATA

$data = array('title'=>'hello EWA','content'=>'This is for the Testing on the Insert data','status'=>1);
$model = Mage::getModel('mytestimonial/mytestimonial')->setData($data);
try {

       $insertId = $model->save()->getId();
        echo "Data has been saved successfully. Insert ID: ".$insertId;
    } catch (Exception $e){
     echo $e->getMessage();
}

UPDATE DATA

// $sid = $this->getRequest()->getParam('id');
$sid = 5;
$data = array('title'=>'hello EWA','content'=>'This is for the Testing on the Update data','status'=>0);
$model = Mage::getModel('mytestimonial/mytestimonial')->load($sid)->addData($data);
try {
        $model->setId($sid)->save();
        echo "Data has been updated successfully.";
       
    } catch (Exception $e){
        echo $e->getMessage();
}

DELETE DATA

$sid is the database table row id to be deleted.

// $sid = $this->getRequest()->getParam('id');
$sid = 5;
$model = Mage::getModel('mytestimonial/mytestimonial');
try {
        $model->setId($sid)->delete();
        echo "Data deleted successfully.";
       
    } catch (Exception $e){
        echo $e->getMessage();
}

DISPLAY RELATED PRODUCTS ANYWHERE IN MAGENTO

<?php

//Start loading a product resources
$product = Mage::getModel('catalog/product')->load($productId);

//save the product into the registry
Mage::register('product', $product);

//display $product's related products
echo $this->getLayout()->createBlock('catalog/product_list_related')->setTemplate('catalog/product/list/related.phtml')->toHtml();

?>

DISPLAY LIST OF ALL MANUFACTURERS IN MAGENTO

<?php
$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);
     
echo "<pre>";
print_r($manufacturers);
echo "</pre>";

?>

RELATED PRODUCTS ON PRODUCT DESCRIPTION PAGE IN MAGENTO

Step 1. Open catalog.xml file for this navigate to the app/design/frontend/default/your_custom_theme/layout/ or you can find this file app/design/frontend/base/default/layout/ open it in the editor of your choice.

Step 2. In catalog.xml search for the catalog_product_view section
<block type="catalog/product_list_related" name="catalog.product.related" before="-" template="catalog/product/list/related.phtml"/>

Step 3. Search for the catalog_product_view section again and copy and paste the code as like given below:
<reference name="content">
<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
<block type="catalog/product_list_related" name="catalog.product.related" as="related" template="catalog/product/list/related.phtml"/>
</block>
</reference>

Step 4. Now navigate to the app/design/frontend/default/your_custom_theme/template/catalog/product/view.phtml or you can find this file app/design/frontend/base/default/template/catalog/product/view.phtml open the file in the editor of your choice, insert the given below code in the desired position where you want to display the related product block in the product description page.
<?php echo $this->getChildHtml('related'); ?>

Step 5. For editing the look of the related products Navigate to the app/design/frontend/default/your_custom_theme/template/catalog/product/list/related.phtml or you can find this file app/design/frontend/base/default/template/catalog/product/list/related.phtml, from here you can change the look and feel of the related products.

Step 6. Now refresh the cache from the Magento admin and delete the browser cache and refresh the page.

Magento add attribute to filter conditions for products collection

General Product Collection:
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToFilter('visibility',array("neq"=>1)); //except not visible individually
$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()');   //uncomment for getting the products in random order
$collection->addStoreFilter();
if(!empty($collection))
{
foreach ($collection as $_product):
echo $_product->getName();   //get product name
endforeach;
}else
{
echo 'There is no products';
}


Get All Products of a category or Product collection filter by the category

$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($category);


$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image'))->addAttributeToFilter('name', array('like' => 'Nok%'))->load();

a) Equals: eq

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'status'))->addAttributeToFilter('status', array('eq' => 1))->load();

b) Not Equals – neq

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image','sku'))->addAttributeToFilter('sku', array('neq' => 'test-product'))->load();

c) In – in

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku')) ->addAttributeToFilter('id', array('in' => array(20,24,100)))->load();

d) Not In – nin

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('id', array('nin' => array(20,24,100)))->load();

e) Like – like

$products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku')) ->addAttributeToFilter('name', array('like' => 'Nok%'))->load();

f) Not Like – nlike

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku')) ->addAttributeToFilter('name', array('nlike' => 'Nok%'))->load();

g) NULL – null

$products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))   ->addAttributeToFilter('description', 'null') ->load();

h)  Not NULL – notnull

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('description', 'notnull')->load();

i) Greater Than – gt

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('id', array('gt' => 10))->load();

j) Less Than – lt

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('id', array('lt' => 10))->load();

k) Greater Than or Equals To- gteq

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('id', array('gteq' => 20))->load();

l) Less Than or Equals To – lteq

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku')) ->addAttributeToFilter('id', array('lteq' => 20))->load();

Display All Active Categories

$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter();

Display Active Categories Of Any Particular Level in Magento

$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter()
->addLevelFilter(1)
->addOrderField('name');

Display Top level categories only in Magento

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Magento display root categories and subcategories

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
    <?php foreach($_categories as $_category): ?>
    <li>
    <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
    <?php echo $_category->getName() ?> <!--//Top Level Category Listing-->
    </a>
    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
    <?php $_subcategories = $_category->getChildrenCategories() ?>
    <?php if (count($_subcategories) > 0): ?>
    <ul>
    <?php foreach($_subcategories as $_subcategory): ?>
    <li>
    &nbsp;&nbsp;&nbsp;<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> <!--//Sub Category Listing-->
    <?php echo $_subcategory->getName() ?>
    </a>
    </li>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    </li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

Friday, September 27, 2013

Magento print select query for products

To print select query : app\code\core\mage\catalog\model\resources\collection\abstract.php

Monday, September 23, 2013

Magento - Sort by Date Added

copying app/code/core/Mage/Catalog/Block/Product/List.php into app/code/local and adding some sorting code at the end of its _getProductCollection() method:


 / sort by created_at date or entity_id
if(!isset($_GET['order'])) {
    $this->_productCollection->getSelect()->reset( Zend_Db_Select::ORDER );
    $this->_productCollection->getSelect()->order('e.entity_id desc');
}
return $this->_productCollection;