Add Stats Script Code before Closing in Magento

Not surprisingly, most people believe the right way to do this is to find the *footer* template in the Magento theme and then include the analytics code at the end of the file. While this approach works, it’s not the best, because it’s NOT immediately before </body></html>.

What if you really need to add the code IMMEDIATELY before the page closing tags </body></html>?

Add code immediately before </body></html> in your Magento store

The best intended approach is to supply the code in your Magento System Configuration. Just go to:

System -> Configuration -> Design -> Footer.

Here you will find a textarea field that reads Miscellaneous HTML. Just put the analytics code there and it will appear immediately before the closing </body></html> on every page of your Magento store.

Similarly, add code immediately before closing </head>

Follow the configuration path:

System -> Configuration -> Design -> HTML Head

And you will find a textarea field named Miscellaneous Scripts. Just put any head meta or scripts code there and they will appear immediately before </head> on every page of your Magento store.

 

Set Default Weight, Set Status to be ‘Enabled’ by Default and Tax Class to be ‘None’

Just like the points made from this post: Auto-generated SKU for new product, it’s annoying to have to enter some of the fields every time you create a new product because apparently, we want it ‘Enabled’ in status; weight doesn’t matter so much; and most of the products belong to one tax class or simply ‘None’.

To reduce repeatedly wasted time and increase productivity when creating products, just edit /app/design/adminhtml/default/default/template/catalog/product/edit.phtml and add the following JavaScript to the end of the file:

<script type="text/javascript">
if(document.getElementById('status').value == ""){
    document.getElementById('status').options[1].selected = true;
}
if(document.getElementById('weight').value == ""){
    document.getElementById('weight').value = "1.0";
}
if(document.getElementById('tax_class_id').value == ""){
    document.getElementById('tax_class_id').options[1].selected = true;
}
</script>

Now the Weight field will have a default value of ’1.0′, the Status field is selected ‘Enable’ by default and the Tax Class is selected as ‘None’ by default.

Auto-generated Product SKU when Adding New Product

SKU isn’t needed in all stores and it’s sometimes annoying to have to enter it for every new product. Is there any way for Magento to automatically generate new SKU for new products?

There is.

Open /app/design/adminhtml/default/default/template/catalog/product/edit.phtml and add the following snippet to the bottom of the file:

<?php
    $dbread = Mage::getSingleton('core/resource')->getConnection('core_read');
    $sql = $dbread->query("SELECT * FROM catalog_product_entity ORDER BY created_at DESC LIMIT 1");
    $res = $sql->fetch();
?>
<script type="text/javascript">
if(document.getElementById('sku').value == ""){
    document.getElementById('sku').value = <?php echo (int)$res["sku"] + 1; ?>;
}
</script>

What this code does:

  1. Read the SKU (must be an integer) of the last added product.
  2. Increment the SKU by 1.
  3. Pre-populate the incremented new SKU to the SKU field by JavaScript.

Therefore, you don’t have to manually enter the SKU yourself when adding the product.

You can do more with this

By adding extra JavaScript code to edit.phtml in a similar way, you can set default values for text input fields and make options selected by default for drop-down selections in the product editor.