December 14, 2018

cm-mini

Thinking Magento

Magento - Special Prices Date (More Control)

So this next blog is going to be about what is featured in the Enterprise edition of Magento and how you can attain this in your current version of Magento. One big flaw in the special prices is that they are controlled by the day, so you can't really do very exciting promotional sales on your website in the CE version of Magento, however in the Enterprise edition you can implement a special price countdown til sale ends, as well as control product special prices by the hour and minute. Today you'll be briefly guided through how you can achieve this in your CE version of Magento.

Magento - UK Postcode Validation

We came across a problem recently, which was related to exporting orders to parcel force and how the UK postcode format is required to have a space between that seperates the district code and the inward code.

Many people these days assume that there is little or no reason to have a space in their postcode and seem to take a more continental approach towards this. However, this creates problems when transporting your orders to distribution firms such as ParcelForce and they WILL NOT recognise the postcode without a space. To prevent this from happening you can add the following into your name.phtml file in the customer folder of your frontend templates.

<script type="text/javascript">
var shippingForm = new VarienForm('co-shipping-form');
Validation.addAllThese(
[
['validate-zip-international', 'Please enter a valid uk zip code.E.G. GB1 6LY', function(v) {
var country_id = 'GB'
var country = $('billing:country_id');
var element = $('billing:postcode');
if (element && ('' != element.value) && (country_id == country.value)) {
if (!element.value.match(/(^[A-Za-z]{1,2}[0-9]{1,2}[A-Z]?[\s][0-9][A-Za-z]{2}$)/ )) {
return false;
}
}
return true;
}]
]
);
</script>z
Once you'ved added this to name.phtml you can now locate the postcode inputs in billing.phtml and shipping.phtml and replace the input with the following
<input type="text" title="" name="billing[postcode]" id="billing:postcode" value="" class="validate-postcode required-entry input-text" />
This will now give you nice clean postcodes for all of your UK postcodes only and mean that there will be less complications for users checking out when it comes to payment processing and order exporting to distributors.

Magento - Low Stock Notifications

Stock notifications in Magento are always confusing and it seems to me like the issue needs to be cleared up for some. Stock notification on the frontend of the store is for the customer to know when a product is back in stock. They will be alerted by an email that is sent when triggered by the cronjob in the early hours. The other is the RSS feed that can be found on the Catalog > Manage Products grid. This particular RSS feed however is admin protected, which in theory would be great, but in reality can be a real pain. I'll explain in this blog how to utilise the RSS feed, as well as the Low Stock Grid and a few other tips on how to manage your stock levels.

First of all you are going to require making some core hacks, you could modulise this, however for all intensive purposes the following will be surfice.

Open up /app/code/core/Mage/Rss/controllers/CatalogController.php

and remove  Mage::helper('rss')->authAdmin('catalog/products'); from public function notifystockAction().

This will remove the login authentication from the Low Product Stock RSS feed.

You can then use website like Feed My Inbox to check the RSS feed daily and to email your account once stock has reached 0. This then removes any need for tedious RSS viewers or readers to be running in your browser or desktop and that each morning the is the possibility of a stock notifcation.

On top of this method, the Low Stock Grid in the Reports section of Magento is extremely valuable.

Again though some alterations are required in order to fix the problems.

First of all if your on a version of Magento that is lower than 1.4.0.1 and you do not want to upgrade, however you are unable to filter by Qty on your Low Stock Grid, then all you need to do is download a copy of Magento 1.4 and find file app/code/core/Mage/Reports/Model/Mysql4/Product/Lowstock/Collection.php. You can use this file to replace the one you are currently using on your installation. We've posted in the Magento forums exactly what the problem is for this fix.

Next up is to give your Low Stock Grid more functionality and you can do this by adding in another column called Low Stock Date. This will allow you to filter when products went out of stock, depending on what number your low stock notification is set to.

For this we need make a copy of 

app/code/core/Mage/Adminhtml/Block/Report/Product/Lowstock/Grid.php

and place it in the same set for directories within /code/local 

->joinInventoryItem('qty')
with
->joinInventoryItem('qty')
->joinInventoryItem('low_stock_date')
and then further down add to the columns
 
$this->addColumn('low_stock_date', array(
'header' =>Mage::helper('reports')->__('Low Stock Date'),
'sortable' =>false,
'index' =>'low_stock_date',
'type' =>'date'
 ));
This will now give you a new column of when Low Stock was reported in the Magento system.

IF selling products via the admin and going into minus levels of stock, the Low Stock Date will update to the day of the most recent order with the associated product.
 
* Make backups of all core files before making changes.

Magento - Site Speed & Performance

The big question that anyone ever asks us about Magento is site speed and performance. The community edition of Magento is and extremely hungry resource hog a lot of the time and performance can suffer if subject to shared hosting, infact most hosting providers will remove a magento site from their servers and ask that it is either placed on a dedicated or virtual private server (VPS).

Being limited by a shared server means you will not be able to make changes to php settings, so this will include memory levels, time out issues and space. The improvements in site speed and performance are therefore recommended for a dedicated or VPS. At our hosting provider, xcache is used on Magento servers to help to improve the compilation of php scripts and places it into the RAM for direct access, reducing page load times. More information can be found here. On top of this the compression of the JS and CSS files is the next line of defence from poor performance. The extension from Fooman called "Fooman Speedster" is easy to install and will compile JS and CSS files that are featured in XML files (not css or js manually inputted into the template) or XML page updates and works in conjunction with the built in Magento cache. More information can be found here.

The final solution in terms of speeding up your store will be to edit the .htaccess file to enable apache html caching of files served. This does sometimes cause problems with updating catalog.xml files for the layout, but it does update to the latest version after a short period of time. However, it's well worth it, as it speeds up store speed by around 75%

The following code can be inserted into the base of your .htaccess

AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript application/x-json application/x-httpd-php
AddOutputFilter DEFLATE html xml css js php
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
php_flag zlib.output_compression on

Magento - Flushing Tables

In our previous posts we've discussed the importance of the cronjob as well as eav attributes, but now down to something that could possibly be more important than anything else so far. The magical log tables in magento, which will slowly but surely slow down your site until it crawls. Unfortunately most Magento users suffer from this exact problem on a regular basis and it is an every growing black mass of data that can quickly put your database into a ludicrous size of gigabytes. So, what can you do to prevent it? At present log cleaning on Magento versions does not do what it says on the tin, so for this you will have to utilise the cronjob that you have setup already and a custom module, which will be set to clean out your log tables on a nightly basis.

If you're wondering about the importance of the log tables and how we can so quickly erase the data from them, then your in luck. These tables store information on vistor information, like url trends and a few other bits. Unfortunately on a busy site you can see why these would quickly fill up.

The custom module:

There are a few pieces of code to help you with this solution, but most are rather dirty hacks, that don't work with the cronjob setup of Magento and are not controllable from the admin. We have a solution, which is based around a solution created by devgento and can be found at the below link.

Module:

Download Clean Log Tables - (Requires cronjob to be up and running in Magento)