Popular Posts

Saturday, 4 March 2017

Magento: How to get last order id

There are many ways to get last order id:
 
1.  From checkout session:

    $lastOrderId = Mage::getSingleton('checkout/session')
                   ->getLastRealOrderId();

    $orderId = Mage::getModel('sales/order')
               ->loadByIncrementId($lastOrderId)
               ->getEntityId();


Above solution will not work, if you want to get last order id in admin.
For this you can try this solution:

2.  From model:
 

$orders = Mage::getModel('sales/order')->getCollection()
     ->setOrder('increment_id','DESC')
     ->setPageSize(1)
     ->setCurPage(1);


$orderId = $orders->getFirstItem()->getEntityId();
 

But this method will not work, if there are multi store in a single magento setup. So a better solution would be:

$orders = Mage::getModel('sales/order')->getCollection()
     ->setOrder('created_at','DESC')
     ->setPageSize(1)
     ->setCurPage(1);

$orderId = $orders->getFirstItem()->getEntityId();

Magento: Get all related products of current product

Many other situations can be there, when we may not have option, but to write custom code to get related products of currently loaded product.

For such situations, try this code:

$collection = Mage::getModel('catalog/product_link')
                    ->getCollection()
                    ->addFieldToFilter('product_id',$product_id)
                    ->addFieldToFilter('link_type_id','1');

$related_products = $collection->getData();


This will give you list of all related products of a product.

Note: Here $product_id is the id of currently loaded product.

Magento: Redirect to login page if user not login

Here is simple magento code to redirect to login page, if user is not logged in:

<?php
    $redirect_url = Mage::getUrl('customer/account/login/');
    $current_url = Mage::helper('core/url')->getCurrentUrl();
    if((!$this->helper('customer')->isLoggedIn()) && ($current_url != $redirect_url)){
        Mage::app()->getFrontController()->getResponse()->setRedirect($redirect_url);
    }
?>

Custom CMS field showing widget calling code in frontend in Magento

Scenario:
I added custom field for CMS pages and wanted to show its value in sidebar.

Field type: Textarea
Wysiwyg: Enabled
Field Name: content_custom

I wanted to call a widget in content of this custom field.

For reference, I added below code to call widget:

{{widget type="cms/widget_page_link" anchor_text="My Custom Link" title="My Custom Link" template="cms/widget/link/link_block.phtml" page_id="153"}}

When I tried to show field's value in frontend, it displayed content as it was inputted from cms page:

{{widget type="cms/widget_page_link" anchor_text="My Custom Link" title="My Custom Link" template="cms/widget/link/link_block.phtml" page_id="153"}}

instead of rendering its html.

Solution: To render html instead of code calling widget, use below code:

echo Mage::helper('cms')->getBlockTemplateProcessor()->filter(Mage::getBlockSingleton('cms/page')->getPage()->getContentCustom());

How to download Magento extension directly

Sometimes there are situations when we can't install extension via Magento Connect Manager in Magento. In such situations we may need to download the extension's files directly.

Below are some easy ways by which we can download Magento extension directly (without Magento Connect Manager):

Freegento.com (http://freegento.com/ddl-magento-extension.php)
http://mage.artem.im
The last one is ultimate 😋. If above both websites go down and you need a direct URL to download magento extension directly, try below URL trick:

http://connect20.magentocommerce.com/community/{PACKAGE NAME}/{VERSION}/{PACKAGE NAME}-{VERSION}.tgz

where {PACKAGE NAME} can be found from extension's key's last part and {VERSION} can be found under release notes tab on extension's page.

For example, you want to download the "Order Delivery Date" module as TGZ (.tar.gz) archive from Magento Connect.

A. Open Magento Connect page of the module: https://www.magentocommerce.com/magento-connect/order-delivery-date.html

B. Note down last release version number from the 'Release Notes' tab.

In our sample last version of the module is "0.1.9".

C. Check an Extension key for "Magento Connect 2.0".

For our module the Extension key is:
http://connect20.magentocommerce.com/community/order_delivery_date

4. Configure URL for downloading and open it in your browser: http://connect20.magentocommerce.com/community/order_delivery_date/0.1.9/order_delivery_date-0.1.9.tgz

Magento: How to get last order id

There are many ways to get last order id:   1. From checkout session: $lastOrderId = Mage::getSingleton('checkout/session'...