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

Thursday, 5 May 2016

PHP redirect without header()

It’s very frustrating when you want to redirect from one page to another, and you get the error:

Warning: Cannot modify header information – headers already sent by (output started at /some/filename.php:12) in /some/filename.php on line 99

The problem is Headers are already sent, or in simple language the output is already sent to browser when your header() function is called. The best solution is not to pass headers (send html/output to browser) till the point header() function is fired.

Another way is to use output buffering. Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script. For using this method, you have to start your file with ob_start(); so that output gets buffered.

But what if you don’t care whether the output is sent or not, you anyway want the redirect to work? This simple function will do the trick for you. It will check if headers are not sent, then it will call the PHP’s header function to redirect. But if the headers are sent, it will use Javascript to redirect to the URL you want.

// OR Used to without function .....


function redirect($url)
{
    if (!headers_sent())
    {
        header('Location: '.$url);
        exit;
    }
    else
    {
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>';
        exit;
    }
}

Magento bug – Checkout cart 500 error – Redirect loops

Magento checkout cart gives 500 error and redirect loops when there is a shopping cart rule with Category condition.

I found a bug in Magento which redirects shopping cart indefinitely causing it 500 internal server error. This can be a serious bug as customer will not able to shop if this happens. I noticed this happens when there is a shopping cart rule which have Category in conditions of the rule.

If total quantity equals or greater than 1 for a subselection of items in cart matching ALL of these conditions:
Category is 125

So for example you have a shopping cart rule where you want to give some discount or free product if at least one product is chosen from specific Category, this triggers the error in frontend shopping cart. Main reason here is Category condition. If you remove category condition then the error should go away. But if you want to keep the category condition and still want Magento to handle the shopping cart promotion rule, check the code changes below:

To solve this I copied below file to my local
app/code/core/Mage/SalesRule/Model/Rule/Condition/Product/Combine.php

and edited the function validate:

**
     * Validate a condition with the checking of the child value
     * @param Varien_Object $object
     *
     * @return bool
     */
    public function validate(Varien_Object $object)
    {
        /** @var Mage_Catalog_Model_Product $product */
        $product = $object->getProduct();
        if (!($product instanceof Mage_Catalog_Model_Product)) {
            $product = Mage::getModel('catalog/product')->load($object->getProductId());
        }

        $valid = parent::validate($object);

        /* Prashant commented whole block, as it causes redirect loop and Segmentation fault in apache
        if (!$valid && $product->getTypeId() == Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE) {
            $children = $object->getChildren();
            //$valid = $children && $this->validate($children[0]); //Prashant commented, issue....
        }*/


        return $valid;
    }


    Thanks:-
    

magento cookie text replaces description in google

I was facing the exact same problem: Google was showing the cookie warning text as description in search results for my Magento store.

The problem turned out to be my Meta description being too short. Solution for me was making the meta description longer, atleast about 150 characters (including spaces).

What goes in your < description > tag is found in Magento's backoffice:

  system>configuration>general>design, under HTML head, Default Description.

After save, I cleared cache and checked the page source for showing the updated meta. To make things with Google go faster, I used their webmaster tools to submit the store url for crawling. After a little wait, Google was showing the store's description in the search results just like it's supposed to.

Hope this can still help you!

Cheers :)

Thursday, 15 October 2015

How do you change the number of product columns

The line you wish to change is generated by this code:

<?php $_columnCount = $this->getColumnCount(); ?>

<ul class="products-grid products-grid--max-<?php echo $_columnCount; ?>-col">
that you can find in this file:

// OPen list.phtml file

app\design\frontend\Your_Package\Your_Theme\template\catalog\product\list.phtml
One simple way to change the number of columns is to edit catalog.xml of your theme:

app/design/frontend/Your_Package/Your_Theme/layout/catalog.xml
Find and edit the line that says:

<action method="setColumnCount"><count>4</count></action>
Another simple way to change columns count with a custom layout section in the categories: In the Custom Design tab for a category, enter this code in the Custom Layout Update field:

<reference name="product_list">
    <action method="setColumnCount"><count>4</count></action>
</reference>
You can find further reference in this video: Change magento column count in products grid list

Another way would be to edit the list.phtml file and replace this line:

<?php $_columnCount = $this->getColumnCount(); ?>
with something like:

<?php $_columnCount = 6; ?>

Thursday, 28 August 2014

How to get High Quality Dofollow Backlink from Orkut

Do you remember when you last logged in to your Orkut account. I guess it may be more than a few months or years. But this is definitely not applicable to all of you as Orkut is still very popular in some regions of Brazil, India and US. Orkut is still holding the Alexa rank around 2000 and having Pagerank7. Here I am going to show how to get Dofollow backlink from.
Before going ahead, I would like to highlight some points.
On 3rd October 2013, Google announced a major search algorithm release called Hummingbird. There are many debates on Hummingbird’s consideration of backlinks and its impact on site’s Pagerank. So the question is if backlink from Orkut is really going to help you to improve your Pagerank? Or Pagerank itself is dead now?
1) Hummingbird looks at over 200 factors when determining search rank for a site — and PageRank appears to be one of them. Backlinks can and will continue to impact PageRank, meaning that credible backlinks (Backlinks from High Pagerank sites) are a major factor of Hummingbird.
2) Along with valuable content, social media shares and interaction will also impact your search rank.
Orkut is a Social Media site with high Pagerank. Considering above two points, getting dofollow backlink from Orkut is definitely going to help you. So here we go.

1) Go to http://www.orkut.com and login with your Google credentials
2) After login click on Profile button and select edit my profile info
2) In Orkut Settings, select Social tab and put your site name in webpage field. Then click Save button and that’s it.
3) To see the link again click on Profile button and expand profile by clicking on view full profile. This is a Dofollow link. 

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'...