Sunday 31 May 2015

How to get customer session in external file Magento ?

Hi sometime we need to access the customer session in nexternal file and with the below code you can access


<?php
// Include Magento application
require_once ( "path_to_your_magento/app/Mage.php" );
umask(0);

// Initialize Magento
Mage::app();

// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));

$session = Mage::getSingleton("customer/session");

if($session->isLoggedIn())
{
    echo "Logged in";
}else{
    echo "Not logged in";
}

Monday 25 May 2015

Copy products from one category to another in Magento

Hi sometime we need to copy the products from one caegory to another in magento , but by default there is no such option like this in magento. So we can do this programatically with the below code




<?php
$category = Mage::getModel('catalog/category');
$category->load('2'); // Preset category id
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $product) {
$product->getId();// Now get category ids and add a specific category to them and save?
$categories = $product->getCategoryIds();
$categories[] = 3; // Category to add
$product->setCategoryIds($categories);
$product->save();

}

Thursday 21 May 2015

Programatically create order in Magebto ?

HI sometime we need to create a orders grammatically in magento and with the below code you can easily


Create a new file in magento root and add the below code in that
<?php
require_once('../app/Mage.php'); //Path to Magento
umask(0);
Mage::app();


$productids=array(1,2); // products you want to add in order
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$order=1;
 // Start New Sales Order Quote
 $quote = Mage::getModel('sales/quote')->setStoreId($store->getId());

 // Set Sales Order Quote Currency
 $quote->setCurrency($order->AdjustmentAmount->currencyID);
 $customer = Mage::getModel('customer/customer')
             ->setWebsiteId($websiteId)
             ->loadByEmail('demo@gmail.com');
 if($customer->getId()==""){
     $customer = Mage::getModel('customer/customer');
     $customer->setWebsiteId($websiteId)
             ->setStore($store)
             ->setFirstname('rohit')
             ->setLastname('goel')
             ->setEmail('any@gmail.com')
             ->setPassword("123456");
     $customer->save();
 }

 // Assign Customer To Sales Order Quote
 $quote->assignCustomer($customer);

     // Configure Notification
 $quote->setSendCconfirmation(1);
 foreach($productids as $id){
     $product=Mage::getModel('catalog/product')->load($id);
     $quote->addProduct($product,new Varien_Object(array('qty'   => 1)));
 }

 // Set Sales Order Billing Address
 $billingAddress = $quote->getBillingAddress()->addData(array(
     'customer_address_id' => '',
     'prefix' => '',
     'firstname' => 'john',
     'middlename' => '',
     'lastname' =>'Deo',
     'suffix' => '',
     'company' =>'',
     'street' => array(
             '0' => 'Patiala',
             '1' => 'Sector 64'
         ),
     'city' => 'Noida',
     'country_id' => 'IN',
     'region' => 'PB',
     'postcode' => '147001',
     'telephone' => '9464532670',
     'fax' => 'gghlhu',
     'vat_id' => '',
     'save_in_address_book' => 1
 ));
 $shipprice=10;
 // Set Sales Order Shipping Address
$shippingAddress = $quote->getShippingAddress()->addData(array(
     'customer_address_id' => '',
     'prefix' => '',
     'firstname' => 'Rohit',
     'middlename' => '',
     'lastname' =>'Goel',
     'suffix' => '',
     'company' =>'',
     'street' => array(
             '0' => 'Patiala',
             '1' => 'Sector 64'
         ),
     'city' => 'Patiala',
     'country_id' => 'IN',
     'region' => 'PB',
     'postcode' => '147001',
     'telephone' => '9464532670',
     'fax' => 'gghlhu',
     'vat_id' => '',
     'save_in_address_book' => 1
 ));
 if($shipprice==0){
     $shipmethod='freeshipping_freeshipping';
 }

 // Collect Rates and Set Shipping & Payment Method
 $shippingAddress->setCollectShippingRates(true)
                 ->collectShippingRates()
                 ->setShippingMethod('flatrate_flatrate')
                 ->setPaymentMethod('checkmo');

 // Set Sales Order Payment
 $quote->getPayment()->importData(array('method' => 'checkmo'));

 // Collect Totals & Save Quote
 $quote->collectTotals()->save();

 // Create Order From Quote
 $service = Mage::getModel('sales/service_quote', $quote);
 $service->submitAll();
 //$increment_id = $service->getOrder()->getRealOrderId();

 // Resource Clean-Up
 //$quote = $customer = $service = null;

 // Finished
 //return $increment_id;



?>

Wednesday 13 May 2015

Get all statuses of particular state in Magento

Hi by below code you can get the all statuses of a particular state 

$complete_statuses = Mage::getResourceModel( 'sales/order_status_collection' )->joinStates()->addFieldToFilter('state','complete');
foreach ($complete_statuses as $complete_statuses_array ){
    $stausarray[] =  $complete_statuses_array->getStatus();
}


in this i have get the statuses of a complete state. You can replace  state
in ->addFieldToFilter('state','complete');  according to your needs


run some function on ajax complete in prototype

HI sometime we need to detect the ajax complete event .In prototype you can do this by below code



Ajax.Responders.register({
  onComplete: function() {
       yourfunction(); // you can put your code here which you want to run on ajax complete event 
    }
});

Friday 8 May 2015

Get collection count in magento

HI you can get the colection count by below code


Mage::getModel('catalog/product')->getCollection()->getSize();  :)

Thursday 7 May 2015

How to redirect to referrer from Observer in Magento ?

we can redirect to the referrer from magento observer like below


Mage::getSingleton('core/session')->addError('your error message'); // to show your message after redirection
$url = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer()  : Mage::getUrl();
Mage::app()->getFrontController()->getResponse()->setRedirect($url);
Mage::app()->getResponse()->sendResponse();
exit ;