Magento

Magento Backorders – Complete with Notifications to Customers

Tuesday, July 2nd, 2013

There’s many articles out there that cover how to enable backorders in Magento, and that is simple enough.

However – there are several pieces of this puzzle that are nowhere brought together for a “full” backorder solution.

This solution allows:

  1. Your products to be set to backordered
  2. You to set a custom notification for the product page
  3. Your customers to be notified in the cart page

Enable Backorders in the Dashboard

  1. Log into your Magento admin dashboard.
  2. Hover over the “System” menu.
  3. Click “Configuration”
  4. On the left-side menu, choose “Catalog”
  5. In the Catalog sub-menu, choose “Inventory”
  6. In the pane on the right, find the select box titled “Backorders”, and choose “Allow Qty Below 0 and Notify Customer”

Now that you’ve enabled backorders, a few things happen.  First, products with a quantity of zero are still displayed, and may still be added to the cart.  However, they do not show any indication of being backordered in the product page, which these next two steps will correct:

Modify Your Theme File to Display a Custom Backorder Message

Find the following theme file:

[your theme]/[your_theme]/template/catalog/product/view/type/default.phtml

Open it up, and you should see a line of code that says

$_product = $this->getProduct()

Add the following code immediately after that line:

 // Backorder message
	$backorder_message = '';
	$inventory =  Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

	if((int)$inventory->getQty()<=0 && $inventory->getBackorders()>=1) {
		$backorder_message = '

' . $_product->getData('backorder_message') . '

'; }

Then, immediately below / after the

endif;

line, add this:

echo $backorder_message;

Add the Custom Backorder Message to the Product
The final step in bringing this all together is adding a new attribute to the product that is going to be backordered.

This is simple to do via the Magento dashboard:

  1. Edit the product in question.
  2. On the left menu, choose “Description”
  3. Near the top of the page, click the orange button titled “Create New Attribute”
  4. Important: be sure to set the Attribute code to backorder_message (this is the exact code that the template changes are looking for, so it has to be correct).
  5. Set the rest of the attribute up as desired.
  6. Save the attribute.
  7. The attribute will now be displayed for you to fill in on the product. Enter your custom message.
  8. Save the product.

You may need to reindex in Magento, or clear cache, but when you do, your message will display if, and only if, you have zero quantity (or less) and the site is set to backorder.

Magento Staging Setup Accessing Live Database with SSL Certificate

Saturday, June 22nd, 2013

With a recent project, we needed to get the Magento staging environment on the same server and accessing the same database as the live environment.  There was lots of setup, product adjusting, etc. that needed to be done, and the client didn’t want to have to do it twice.

Setting up the staging environment was fairly straightforward, thanks to the excellent article over at Mag Life.  However, the article didn’t quite get the job done.

The live site, like all good live ecommerce sites should, was using an SSL certificate to provide a secure connection.  However, the SSL was for a fully qualified domain name (www.sitename.com) and the staging site was on a subdomain (dev.sitename.com).  Because the SSL settings are in the database, every time we attempted to hit a secure page, the dev site would no longer load, and instead would attempt to load the live site again.

Additionally, I felt lazy and didn’t want to set up server environment variables like the article at Mag Life, so I made a few other alterations to the code.

When attempting to set this up, follow the directions over at Mag Life, but utilize this code in your Store.php file:

class Alphachannel_Core_Model_Store extends Mage_Core_Model_Store
{

    /**
     *  This function is modified per the Mag Life article.
     *  With one modification to avoid setting server environment variables
     **/
    public function getBaseUrl($type=self::URL_TYPE_LINK, $secure=null)
    {
    	// This is the lazy bit.  Rather than setting 
        // environment variables, just set the url's here
    	$urls = array("www.sitename.com"=>"dev.sitename.com");
        $store_code = $this->getCode();
        $url = parent::getBaseUrl($type, $secure);
        // Several nested ifs are removed from Mag Life version 
        // that deal with the environment variables
        $host = parse_url($url, PHP_URL_HOST);
        if (isset($urls[$host]))
        {
            $url = str_replace('://'.$host.'/', '://'.$urls[$host].'/', $url);
            $url = str_replace("https:", "http:", $url);
                }
        return $url;
    }

    /**
     * This is the same function as the core files, with one 
     * minor alteration to prevent https redirects
     **/
    public function isCurrentlySecure()
    {
        $standardRule = !empty($_SERVER['HTTPS']) && 'off' != $_SERVER['HTTPS'];
        $offloaderHeader = trim(Mage::getStoreConfig('web/secure/offloader_header'));

        if ((!empty($offloaderHeader) && !empty($_SERVER[$offloaderHeader])) || $standardRule) {
            return true;
        }

        if (Mage::isInstalled()) {
            $secureBaseUrl = Mage::getStoreConfig('web/secure/base_route_url');
            if ( ! $secureBaseUrl) 
            {
                // Here's our hack.  Lie to tell it is secure!
            	return true;
                return false;
            }
            $uri = Zend_Uri::factory($secureBaseUrl);
            $isSecure = ($uri->getScheme() == 'https' )
                && isset($_SERVER['SERVER_PORT'])
                && ($uri->getPort() == $_SERVER['SERVER_PORT']);
            return $isSecure;
        } else {
            $isSecure = isset($_SERVER['SERVER_PORT']) && (443 == $_SERVER['SERVER_PORT']);
            return $isSecure;
        }
    }
}

And, for a little icing on this cake, check out the simple debugging script I whipped up that made it fast and easy to trace back which class / method is calling the current function: Get the calling function or class method

Customizing and Styling Magento Order Confirmation

Monday, July 23rd, 2012

Magento is powerful, but man does it make some things more laborious than they need to be.

Out of the box, Magento’s order confirmation is weak. The client wanted a more robust solution, and after scraping the web for resources and dumping class methods out, I was able to put together a very robust solution.
(more…)

Magento 1.6 Theming Admin Pages

Monday, May 10th, 2010

Creating an Admin Theme to match your frontend

As it turns out, this is simple. Here’s the steps:
1) Look in skin/adminhtml/default – inside the folder, you will see another folder titled default
2) Copy that folder, and rename it to match the name of your frontend themes (for example, “cooltheme”)
3) Modify the boxes.css and images to your liking
Read More →

Magento 1.6 Theming Error Pages

Saturday, May 1st, 2010

Over the past few years, I’ve had the mixed blessing of working with Magento.  It’s a spectacularly powerful open source ecommerce platform.  As a developer, I get to roll around in the dirty underbelly of this monster, and have learned more than a few things the hard way.

Recently, a client requested that the error page be themed to match the rest of the site.  (As an aside, this same client asked that the admin login be themed earlier.  At that time I told them no, but I’ve decided I’ll take that on soon.  I’m hoping it won’t be too bad!)

So, when I Googled Magento Error Page Theme, I didn’t find any current or relevant results.  They all pertained to older versions of Magento, and were not quite accurate.

To help anyone else out there who would like to theme their Magento Error page, I’ve put this together.  Magento has made it fairly easy, so here goes:

In your Magento installation, you will find a folder called errors.  Open that folder up.

Inside you will find a folder titled “default”. Copy it, and rename the copy to your theme name – for this example, lets use “customtheme”

In the errors folder, you will find several files.  The Magento team has been kind enough to give us a sample local.xml file titled local.xml.sample.  Change this file name to local.xml

Now open the local.xml file you just created, and edit the <skin>default</skin> setting to be the name of your custom folder you created above (in this example, “customtheme”)

Finally, open up the files inside of “customtheme” and edit them to your liking. The code that displays the image can be found in page.phtml, and the styles are in css/styles.css

That’s it. You can test them by browsing to them directly: http://www.example.com/errors/report.php, http://www.example.com/errors/404.php, etc.