Tag Archives: Magento tutorials online

Get list all bundle products in Magento


Get list all bundle products in Magento : In magento there can be many types of products such as simple, configurable and bundled etc. You can user magento model catalog product model and filters to get the bundle products. You can get other types of products in the same way. Here we are going to explain how to get bundle products in magento.


Get list all bundle products in Magento

Here is syntax for getting bundle products-

Get list all bundled products in Magento Syntax

$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToSelect('type')
    ->addAttributeToFilter('type_id', 'bundle');

Which will give you the bundle products.

Get bundle product items from Bundle Product in Magento

You can get the bundle product items simple using the following syntax –

Get bundle product items from Bundle Product in Magento Syntax

//first load bundle product
$bundleProduct = Mage::getModel('catalog/product')->load($bundleProductId);
$collection = $product->getTypeInstance(true)
    ->getSelectionsCollection(
        $product->getTypeInstance(true)
                ->getOptionsIds($bundleProduct), $bundleProduct);

foreach($collection as $item) {
    $itemId = $item->product_id;
}

The above example will give you items of the bundle product.

Magento format price currency


Magento format price currency: In magento we need to format integer values to price in their respective currency. You can format price with currency symbol or without current symbol. By Default currency symbol($22.50) is added in magento here we are going to cover both situation with and without current syn


Magento format price currency

Here are both conditions to format currency in magento –

Magento format price with currency

You can format price with currency as below –

Magento format price with currency

$priceWitCurrency = Mage::helper('core')->currency($finalPrice, true, false);

The example will format price with currency symbol.

Magento format price without currency

You can format price without currency as below –

Magento format price without currency

$priceWitoutCurrency = Mage::helper('core')->currency($finalPrice, array('display' => Zend_Currency::NO_SYMBOL));


The example will format price without currency symbol.

Magento create invoice programmatically from Order


Magento create invoice programmatically from Order : You can create invoice programmatically from an order. It is very important let us create and understand with example. For this you need order object for which you want to create invoice.


Magento create invoice programmatically from Order

First load order object using the sales order model Mage::getModel(“sales/order”)->load($orderId) and then check if invoice already created else create invoice. –

Magento create invoice programmatically from Order Example –

$order = Mage::getModel("sales/order")->load($orderId);
try {
if($order->canInvoice())
{ 
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
 
if ($invoice->getTotalQty()) { 

$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transaction = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder()); 
$transaction->save();
Mage::getSingleton('core/session')->addSuccess('Invoice Created Successfully.'); 
}else{
 Mage::getSingleton('core/session')->addError('Can not create invoice without Product Quantities'); 
 }
 
}else{
Mage::getSingleton('core/session')->addError('Can not create invoice'); 
}
}
catch (Mage_Core_Exception $e) {
 echo $e->getMessage();
}

Magento get admin user role


Magento get admin user role – You can get admin user role by using admin user model ie. Mage::getModel(‘admin/user’). Here we have explained how to get role of admin user in magento.


Magento get admin user role

Here is simple syntax to get admin user role in magento-

Magento get admin user role

$adminSession = Mage::getSingleton('admin/session');
$adminuserId = $adminSession->getUser()->getUserId();
$roleData = Mage::getModel('admin/user')->load($adminuserId)->getRole()->getData();
$roleName = $roleData['role_name'];

Magento get admin user id


Magento get admin user id: You can get admin user id in magento from admin user session ie. Mage::getSingleton(‘admin/session’); You can get admin user name, username, email, first name and last name also from the admin user session. Here we are going to demonstrate this with syntax and example.


Magento get admin user id

Here is syntax for getting admin user details-

:


$userSession = Mage::getSingleton('admin/session'); 
$userId = $userSession->getUser()->getUserId();
$userEmail = $userSession->getUser()->getEmail();
$userFirstname = $userSession->getUser()->getFirstname();
$userLastname = $userSession->getUser()->getLastname();
$userUsername = $userSession->getUser()->getUsername();

The above example will give you current admin user id, name, email, username etc.

Magento get PayPal Transaction Id


Magento get PayPal Transaction Id : You can get the paypal transaction id generated from the order payment object or if you can get it from the invoice generated directly. Here we are going to explain the method to get the paypal transaction id.


Magento get PayPal Transaction Id

You can get Paypal Transaction Id as Below :

Magento get PayPal Transaction Id from Order Payment Object

//suppose you have order object as $order.
$order = Mage::getModel("sales/order")->load($id);
$transactionId = $order->getPayment()->getLastTransId();

Magento get PayPal Transaction Id From Invoice

You can get detailed Paypal Transaction Id as Below :

Magento get PayPal Transaction from Invoice

$invoice = Mage::getModel("sales/order_invoice")->load($invoiceId);
$transaction_id  = $invoice->getTransactionId();

Magento get invoice id from order increment id


Magento get invoice id from order increment id : For getting the invoice id from order you first need to get order object using the order id. As we know multiple invoice can be created against a single order so we are going to get all invoices generated against a single order. Here we are going to explain how to load order invoices in magento.


Magento get invoice id from order increment id

Here is syntax to get invoice details from order

Magento get invoice id from order increment id

$orderid = '100000009'; // order increment id
$order = Mage::getModel('sales/order')->loadByIncrementId($orderid);
if ($order->hasInvoices()) {
    foreach ($order->getInvoiceCollection() as $invoice) {
        $invoiceIncrementID = $invoice->getIncrementId();
    }
}

If you want to load invoices on the basis of the order id instead of increment id use the following syntax –

Magento get invoice id from order id

Here is syntax to get invoice details from order id

Magento get invoice id from order id

$orderid = '445'; // order id
$order = Mage::getModel('sales/order')->load($orderid);
if ($order->hasInvoices()) {
    foreach ($order->getInvoiceCollection() as $invoice) {
        $invoiceIncrementID = $invoice->getIncrementId();
    }
}

Magento get url parameters on phtml


Magento get url parameters on phtml- If you are working with magento you often need to get the url parameters in phtml, observers or model controllers. You can use Mage::app()->getRequest()->getParams() to get url parameters anywhere you want.


Magento get url parameters on phtml

Here is syntax for getting the url parameters.

Magento get url parameters on phtml

$urlParams = Mage::app()->getRequest()->getParams();//will return array of parameters.

The above example will return the parameters of url on phtml, observers or model controllers.

Magento Get Customer Details


Magento Get Customer Details : If you are working with magento you often need to work with customer related module and you need to get/set customer data. You can get customer information using the model customer/customer. You can get or set the customer data using this model. Here we are going to explain how to get customer data in magento using customer id or customer session.


Magento Get Customer Details

Here is the detail which you can access using the customer model. Lets go in detail step by step –

Magento Get Customer Id From Session

Here is syntax for getting customer id from customer session-

Magento Get Customer Id From Session


    $customer = Mage::getSingleton('customer/session')->getCustomer();
    $customerId = $customer->getId();
  

Which will give you the customer id.

Magento Get Customer data from customer Id

Here is syntax for getting customer data from customer id –

Magento load customer data from customer Id

   $customerId = 10;
   $customer = Mage::getModel('customer/customer')->load($customerId);
   echo $customer->getFirstname();

Which will load the customer data for the customer id 10.

Now Let’s go and get data loaded from the magento.

Magento Get Customer Name

You can get customer first and last name as below –

Magento load customer data from customer Id

   $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
   $customer = Mage::getModel('customer/customer')->load($customerId);
   echo $customer->getFirstname()."
"; echo $customer->getLastname()."
";

Which will give you customer’s name.

Magento Get Customer Group Id

You can get customer group id as below –

Magento get customer group id

   $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
   $customer = Mage::getModel('customer/customer')->load($customerId);
   echo $customer->getGroupId();

Which will give you customer’s group id.

Magento Get Customer Email Id

You can get customer email id as below –

Magento get customer email id

   $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
   $customer = Mage::getModel('customer/customer')->load($customerId);
   echo $customer->getEmail();

Which will give you customer’s email.

Magento Get Customer Address Detail

You can get customer Address Detail such as below –

  • First Name
  • Last Name
  • Company
  • Post Code
  • City
  • Street
  • Telephone
  • Fax
  • Country

Magento get customer Address Detail


   $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
   $customer = Mage::getModel('customer/customer')->load($customerId);
  // get billing address id 
   $customerAddressId = $customer->getDefaultBilling();

   // now get addredd detail 

   $address  = Mage::getModel('customer/address')->load($customerAddressId);
   echo $address->getFirstname().'
'; echo $address->getLastname().'
'; echo $address->getCompany().'
'; echo $address->getPostcode().'
'; echo $address->getCity().'
'; echo $address->getStreert()[0].'
'; echo $address->getTelephone().'
'; echo $address->getFax().'
'; echo $address->getCountry().'
';

Which will give you customer’s email.