Friday 25 December 2015

How to display latest added products first in Magento

If you want to display latest added product first. Then, use below code:  


<?php 


 $_productCollection=$category->getProductCollection()
                         ->addAttributeToSort('entity_id', 'desc');

                     

 // Use this code for sort product as "Ascending" and "Descending" (Red Color Code)


?>




How to display Featured Product on Home Page according to Category Id in Magento?

If you want to display "Featured Products" on home page. You can use below code in product listing code.


    <?php

 
 $categoryid = 8;   // Here you can replace from your category id
                   $category_cb = new Mage_Catalog_Model_Category();     $category_cb->load($categoryid);

$this->getProducts();

$_productCollection=$category_cb->getProductCollection()
->addAttributeToSort('entity_id', 'desc')
->addAttributeToSelect('*') ->addAttributeToFilter(array(
array('attribute' => 'featured', 'eq' => true),
));
 

?> 





How to display Featured Product on Home Page By Category Id in Magento?

Hello Friends,


If you want to display Featured Product on Home Page according to Category Id, So Please see below code. Which Display Featured Product By Category Id.

For Display Featured Product.

First Create attribute in Magento Admin Panel.

For add attribute, 

1. You go to admin Panel

2. Mouse over on "Catalog" Menu (third menu)

3. Mouse over "Attribute" and Click on Manage Attribute (See Screenshot)





4. Open listing of all Attribute.

5. Now Click on "Add New Attribute" on right side.



6. After click "Add New Attribute" , open new page. Here, you can add your attribute. Please see below screenshot for example.



7. After Fill form, click on "Save Attribute". Now, Your "Featured" Attribute is added.

8. Now, make Products as "Featured(option 'Yes')" which, You want to assign as a Featured Product.

Now, You can paste below code, where you want to display Featured Products.

         <?php
 $categoryid = 11;   // Here you can replace from your category id
                   $category_cb = new Mage_Catalog_Model_Category();     $category_cb->load($categoryid);

$this->getProducts();

$_productCollection=$category_cb->getProductCollection()
->addAttributeToSort('entity_id', 'desc')
->addAttributeToSelect('*') ->addAttributeToFilter(array(
array('attribute' => 'featured', 'eq' => true),
));
?> 


Thanks and Enjoy 





Thursday 24 December 2015

After Patched SUPEE-6788, product not displaying on home page in Magento


This error is error of permissions, which display products on home page. So follow below steps.


After patched,  add some section in your admin.


Open
System -> permission -> blocks

Here, You must add the home page blocks for allowed permissions. Like, if you want to display new products, so add New Block "catalog/product_new" in Block Name and "Is Allowed" is Yes.

After Add New Block,  flush cache and reload home page. Now, New Products are displaying on HomePage



Tuesday 22 December 2015

How to replace text in a particular column in a table in phpmyadmin?

Use the REPLACE function with a UPDATE statement:


UPDATE table_name
SET column_name= REPLACE(
column_name, 'search_text', 'replacement_text')



How to get category name by product id in magento?

You can get category name by product id by following code. Where You want display category name, put the code.


<?php $product = Mage::getModel('catalog/product')->load($_product->getId());
$cats = $product->getCategoryIds();
foreach($cats as $categoryId) {
    $category = Mage::getModel('catalog/category')->load($categoryId);
    echo $category->getName();
}
?>



How to display image in magento?

You can display image in magento by following code:



<img src="<?php echo $this->getSkinUrl('images/dummy.jpg');?>" />




Monday 14 December 2015

Magento: Fatal error call to undefined function curl_setopt() in curl.php

If your magento page display this error so please check CURL is enable or not in php.ini file.

If not then enable
curl in php.ini file.



Please find "extension=php_curl.dll" in php.ini file. And remove ";" from starting. Like below: 


;extension=php_curl.dll
to
extension=php_curl.dll 





How to Display the recently added products first in Magento?

 Display the recently added products first in Magento


Please copy the file Toolbar.php from 


app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php

And  Create a Directory structure in local and paste the Toolbar.php as shown below 

app/code/local/Mage/Catalog/Block/Product/List/Toolbar.php

and Find the below code at around line 232


if ($this->getCurrentOrder()) {
  $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}


Now Replace the above code with below code:

if ($this->getCurrentOrder()) {
  if(($this->getCurrentOrder())=='position'){
      $this->_collection->setOrder('entity_id','desc');
  }
  else {
   $this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
  }
}








How to create admin user by database?

Paste Following code into your database and then Login to your Magento admin panel by using new credentials.


INSERT INTO `admin_user` (
`user_id` ,
`firstname` ,
`lastname` ,
`email` ,
`username` ,
`password` ,
`created` ,
`modified` ,
`logdate` ,
`lognum` ,
`reload_acl_flag` ,
`is_active` ,
`extra` ,
`rp_token` ,
`rp_token_created_at`
)
VALUES (
NULL , 'CHANDRA', 'BHUSHAN', 'cb8022@gmail.com', 'cb8022', MD5( 'cb#8022' ) ,
CURRENT_TIMESTAMP , NULL , NULL , '0', '0', '1', NULL , NULL , NULL
)



INSERT INTO admin_role SELECT
 NULL `role_id`,
 0 `parent_id`,
 2 `tree_level`,
 0 `sort_order`,
 'U' `role_type`,
 (SELECT `user_id` FROM admin_user WHERE `username` = 'cb8022') `user_id`,
 'Administrators' `role_name`;

How to clean log via Magento Admin Panel?

Log cleaning via magento admin panel, method is easier for non technical store owners who don’t know to familiar with the Magento store’s database. So, user can activate log cleaning option in Magento admin panel by following steps:


1. Log on to your Magento Admin Panel.
2. Go to System => Configuration
3. On the left side,  under Advanced click on System (Advanced = > System)
4. Under system you will see “Log Cleaning” option
5. Fill the desired “Log Cleaning” option values and click Save.




How to clean logs through database in Magento?

If you want to delete Magento logs through database. So truncate the some table which have logs data. So execute following sql query in database:




SET foreign_key_checks = 0;
TRUNCATE dataflow_batch_export;
TRUNCATE dataflow_batch_import;
TRUNCATE log_customer;
TRUNCATE log_quote;
TRUNCATE log_summary;
TRUNCATE log_summary_type;
TRUNCATE log_url;
TRUNCATE log_url_info;
TRUNCATE log_visitor;
TRUNCATE log_visitor_info;
TRUNCATE log_visitor_online;
TRUNCATE report_viewed_product_index;
TRUNCATE report_compared_product_index;
TRUNCATE report_event;
TRUNCATE index_event;
TRUNCATE catalog_compare_item;
SET foreign_key_checks = 1;




Friday 11 December 2015

What is difference between shopping cart price rules and catalog price rules Magento?

Magento : Difference between  shopping cart price rules and catalog price rules



Shopping Cart Price Rules



Shopping cart price rules can be use for Attribute wise discount, product discount/sale discount, Customer group wise discount, Country wise discount under the certain conditions. Shopping cart price rules use coupon code but not necessary, you can create rule without coupon code also. Shopping cart price rules always apply on shopping cart when conditions are meet. Discount appears on the shopping cart page under the subtotal.





Catalog Price Rules


Catalog price rules use for products discounts or products sales discount under certain conditions. Catalog price rules do not use coupon codes, because they are applied before the product add to cart. Discount appears on products listing.





Thursday 10 December 2015

How to get subcategories of current category in magento?

Display all the subcategories of any category in Magento.


If you have a category ID (current category OR any category) then you can easily display all the subcategories of that particular category.


Below code will load all the subcategories of the current category. 


If you want to get all the subcategories of any specific category, so assign category id in $categoryID. 



<?php 
         
       $catID = $current_category->getId(); //or any specific category id, e.g. 5

       $children = Mage::getModel('catalog/category')->getCategories($catID);

       foreach ($children as $category) 

         {
          
           echo $category->getId();
      
           echo $category->getName();
    
           echo $category->getRequestPath();
     
          print_r($category->getData());

}

?>

How to get sub categories by parent category id in Magento?

You can try below code for display all sub category of one particular category by category id.



<?php 

$category_id = 13;   // you can change category id according to you


$parentCat = Mage::getModel('catalog/category')->load($category_id); 


$subCat = explode(',',$parentCat ->getChildren()); 


$collection  = $parentCat
             ->getCollection()
             ->addAttributeToSelect("*")
             ->addFieldToFilter("entity_id", array("in", $subCat) );


foreach($collection as $catname)

{

     echo $catname->getName();


}


?> 

How to get custom option values in Magento?

If you want custom options value in magento. Then

You can try following code.




<?php
             
          $customoptions = $item->getProductOptions();
                 
           foreach($customoptions ['options'] as $key=>$valuecb)
             {

               echo $valuecb['label'];

              echo $valuecb['option_value'];

  }
}


?> 

How to get Custom Options Programmatically any where in Magento?

How to get Custom Options Programmatically any where in Magento?

You can get custom options by following code. 


$product = Mage::getModel("catalog/product")->load($_product->getId());


$custom = $product->getOptions();


foreach($
custom as $customoptionKey => $customoptionVal){


    foreach($
customoptionVal->getValues() as $valuesKey => $valuesVal){


        echo $
customoptionVal->getId();


    }

}

Friday 4 December 2015

How to Create a custom Database connection in Magento?

Create a connectdb.php file in your root folder. And paste the following code. Now 

you are able to access Data base on your file.




1. require_once ("app/Mage.php");

2. $app = Mage::app('default');

3. $config  = Mage::getConfig()->getResourceConnectionConfig("default_setup"); 

4. $dbinfo = array("host" => $config->host, "user" => $config->username,"pass" => $config->password, "dbname" => $config->dbname); 

5. $hostname = $dbinfo["host"];

6. $user = $dbinfo["user"];

7. $password = $dbinfo["pass"];

8. $dbname = $dbinfo["dbname"];

9. $con = mysql_select_db($dbname,mysql_connect($hostname,$user,$password));





What is Difference between Mage::getSingleton() and Mage::getModel() in Magento

What is difference between getSingleton and getModel in magento. It's very important question for interview in Magento. Every Interviewer asked this question.



Mage::getSingleton()

Mage::getSingleton() is first check the object are created or not. If the object is created then return existing object. If the object is not created then it will create. So Mage::getSingleton() faster than Mage::getModel().

1. $getproduct1  =  Mage::getSingleton('catalog/product');

2. $getproduct2  =  Mage::getSingleton('catalog/product');


$getproduct1 and $getproduct2  both are have same memory and return only one object each time.



Mage::getModel()

Mage::getModel() is always create new object.

1. $getproduct1  =  Mage::getModel('catalog/product');

2. $getproduct2  =  Mage::getModel('catalog/product');


$getproduct1 and $getproduct2  both have different object and also have different memory .