Friday 30 March 2012

Programmatically Add Product In Cart in Magento

Adding a Product to the Cart via Querystring


For Simple Product :

$this->getUrl('checkout/cart').'add?product='.$pro_id.'&qty='.$qty.';
where : $pro_id= 'Product id' ; $qty = 'Product Quentity'

For Simple Product with Custome Option:

$this->getUrl('checkout/cart').'add?product = '.$pro_id.'&qty = '.$qty.'&options['.$pro_value.'] = '.$pro_opt_val.'
where : $pro_value= 'Product option_id';$pro_opt_val= 'Product option_type_id';

For Bundle Product :

$this->getUrl('checkout/cart').'add/product/['.$pro_id.']/?bundle_option[['.option_id.']][]=['.selection_id.']

like if bundle product's id is 2790 than :
      $_product = Mage::getModel('catalog/product')->load(2790);
      if ($_product->getTypeId() == 'bundle') 
     {
        $w = Mage::getSingleton('core/resource')->getConnection('core_write');
        $result = $w->query("select `option_id`, `selection_id` from `catalog_product_bundle_selection` where        `parent_product_id`=$id");
        $route = "checkout/cart/add";
        $params = array('product' => $id);
        while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
            $params['bundle_option['. $row[option_id] .']'][] = $row['selection_id'];
       };
        header("Location: ". Mage::getUrl($route, $params));
   }

For Adding a Configurable Product to the Cart via Querystring:

http://www.your_domain.com/checkout/cart/add?product = 68&qty = 1&super_attribute[528] = 55& super_attribute[525] = 56


Here  68  is the product entity id displayed in magento admin panel.  528 and 525  is the attribute associated with this product and  55 and 56  is the selected value of that attribute. 

Another way to add product in cart :

For Simple Product :

$params = array(
    'product' => 23,
    'qty' => 2,
);
$cart = Mage::getSingleton('checkout/cart'); 
$product = new Mage_Catalog_Model_Product();
$product->load(23); 
$cart->addProduct($product, $params);
$cart->save(); 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);



For  Configurable  Product  :



$params = array(
    'product' => 23,
    'super_attribute' => array(
         528 =>55,
    ),
    'qty' => 2,
);

$cart = Mage::getSingleton('checkout/cart'); 
$product = new Mage_Catalog_Model_Product();
$product->load(23); 
$cart->addProduct($product, $params);
$cart->save(); 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Programmatically Create New Magento Admin User


<?php
/*
 * Create New Admin User
 */


//define USERNAME, EMAIL and PASSWORD below whta ever you want and uncomment this 3 lines (remove # sign)


#define('USERNAME','admin');
#define('EMAIL','xyz@xyz.com');
#define('PASSWORD','admin123');




if(!defined('USERNAME') || !defined('EMAIL') || !defined('PASSWORD')){
echo 'Edit this file and define USERNAME, EMAIL and PASSWORD.';
exit;
}


//load Magento
$mageFilename = 'app/Mage.php';
if (!file_exists($mageFilename)) {
echo $mageFilename." was not found";
exit;
}
require_once $mageFilename;
Mage::app();


try {
//create new user write you firstname,lastname
$user = Mage::getModel('admin/user')
->setData(array(
'username'  => USERNAME,
'firstname' => 'Gz',
'lastname' => 'Chauhan',
'email'     => EMAIL,
'password'  => PASSWORD,
'is_active' => 1
))->save();


} catch (Exception $e) {
echo $e->getMessage();
exit;
}


try {
//create new role
$role = Mage::getModel("admin/roles")
->setName('Inchoo')
->setRoleType('G')
->save();

//give "all" privileges to role
Mage::getModel("admin/rules")
->setRoleId($role->getId())
->setResources(array("all"))
->saveRel();


} catch (Mage_Core_Exception $e) {
echo $e->getMessage();
exit;
} catch (Exception $e) {
echo 'Error while saving role.';
exit;
}


try {
//assign user to role
$user->setRoleIds(array($role->getId()))
->setRoleUserId($user->getUserId())
->saveRelations();


} catch (Exception $e) {
echo $e->getMessage();
exit;
}


echo 'Admin User sucessfully created!<br /><br /><b>THIS FILE WILL NOW TRY TO DELETE ITSELF, BUT PLEASE CHECK TO BE SURE!</b>';
@unlink(__FILE__);


?>

create php file like newuser.php and past above code in that file, put newuser.php file in magento root and run script like http://yourdomain.com/newuser.php thats it, now you can login in magento admin using username and password that you enter above code

Wednesday 17 August 2011

How to disable / remove Secret Key from Admin URL in Magento?


A new secret key is created every time you login to Magento Admin. So, there will be a unique key (32 chars long) for each session of your Magento admin login. This key is appended to the admin URL as http://your-admin-url/key/743c37b1…adf6588/


This is basically added for security reason. In their release note, Magento say that they added secret key to URL for CSRF (Cross-site request forgery) Attack Prevention.


Sometime you may want to access admin URL without the secret key. For this, you can disable the secret key from admin URL.

Here is how you do it:-

- Login to admin
- Go to System -> Configuration -> ADVANCED -> Admin -> Security -> Add Secret Key to URLs
- Select No
- Save Config

You are done. You will not see the secret key in admin URL nowonwards.

Hope this helps.

Magento: How to change Admin URL Path?

Here is a quick guide on how to change admin url path in Magento. This need to be done for security reason to be safe from hacking/cracking issue. Basically, this is done not to let any general user to access the admin page.

Generally, we do have ‘admin‘ as the administrator path for Magento. So, the admin URL will be http://www.example.com/admin/

This article will show you, how you can change the admin url. Let’s say from ‘admin‘ to ‘backend‘. So, the new admin URL will be http://www.example.com/backend/
Here is how we do it:-

- Open app/etc/local.xml
- Find the following:-

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

- Change

<frontName><![CDATA[admin]]></frontName>
to your desired name. Like below:-
<frontName><![CDATA[backend]]></frontName>

- Save the file
- Refresh the Cache from Magento Admin (System -> Cache Management)

Now, you should be able to access admin panel from http://www.example.com/backend/ instead of http://www.example.com/admin/

Magento: Disable Admin Notification Popup

Every time you login to Magento Admin panel, by default you will always encounter a notification popup message. If you not want to Pop-up when Login in Admin than simply do
 - Login to admin panel
- Go to System –> Configuration –> Advanced
- Disable Mage_AdminNotification module.
You are done :) 

Saturday 9 July 2011

how to get store information in magento?

Use below code for get store data :
Get store data
 
Mage::app()->getStore();
 
Store Id
 
Mage::app()->getStore()->getStoreId();
 
Store code
 
Mage::app()->getStore()->getCode();
 
Website Id
 
Mage::app()->getStore()->getWebsiteId();
 
Store Name
 
Mage::app()->getStore()->getName();
 
Is Active
 
Mage::app()->getStore()->getIsActive();
 
Store Home Url
 
Mage::app()->getStore()->getHomeUrl();

Friday 8 July 2011

How to export Categories with Ids in Magento?

Using the following given quick PHP script you can export categories with Ids in Magento.You just need to save te below given code in a PHP file “CatList.php” in the base Magento directory of your store, and visit the URL in your web browser.

<?php
 
 define('MAGENTO', realpath(dirname(__FILE__)));
 require_once MAGENTO . '/app/Mage.php';
 Mage::app();
 
 $category = Mage::getModel ( 'catalog/category' );
 $tree = $category->getTreeModel ();
 $tree->load ();
 
 $ids = $tree->getCollection ()->getAllIds ();
 
 if ($ids) {
  $file = "var/import/catlist.csv";
  file_put_contents($file,"catId, catName\n");
  foreach ( $ids as $id ) {
    $string = $id . ', ' .$category->load($id)->getName() . "\n";
   file_put_contents($file,$string,FILE_APPEND);
  }
 }
?>

You can run this script as http://www.domain.com/CatList.php. You can find the csv file in the var/import/ directory.