Skip to content

Instantly share code, notes, and snippets.

@herveguetin
herveguetin / freeshipping_remainder.php
Created July 16, 2014 16:51
A powerful and easy freeshipping remainder block for Magento based on shopping cart promo rules
<?php
/*
Just create as many shopping cart price rules that are elligible for free shipping
and this block class will use the most appropriate one to define:
- minimum amount to get freeshipping
- remaining amount to purchase to get freeshipping.
If no rule is elligible, the block falls back to a CMS block.
@herveguetin
herveguetin / remove_SID_in_url.php
Created June 25, 2014 15:19
Avoid disgraceful SID params in Magento URLs
<?php
Mage::getSingleton('core/session')->setSkipSessionIdFlag(true);
// line below may also be needed
Mage::app()->setUseSessionInUrl(false);
?>
@herveguetin
herveguetin / custom_xml_block_attributes_block.php
Last active November 13, 2015 19:30
Using custom XML attributes for <block> nodes in Magento layout files
<?php
// This code must in a block class, child of Mage_Core_Block_Abstract
// Retrieve value of 'alt-template' attribute from layout for current block
$blockElement = $this->getLayout()->getXpath("*/block[@name='" . $this->getNameInLayout() . "']");
if($blockElement) {
/* @var $blockElement Mage_Core_Model_Layout_Element */
$blockElement = end($blockElement);
$altTemplate = $blockElement->getAttribute('alt-template');
if(!is_null($altTemplate)) {
$this->setTemplate($altTemplate); // Or your logic...
@herveguetin
herveguetin / sales_order_states.xml
Created June 4, 2014 15:05
Make Magento orders of a *state* (thus all attached statuses) visible / hidden on front
<!-- Update config node /config/global/sales/order/states like this: -->
<config>
<global>
<sales>
<order>
<states>
<[state_code]>
<!--
Available state_code are:
. new
@herveguetin
herveguetin / filter_dates_datetimes.php
Created May 28, 2014 17:41
Properly manage dates and datetimes in admin form (displaying and saving data in order to make sure to fit to locale in Magento)
<?php
/**
* 1) Make sure to display a correct date or datetime input field in admin forms
*
* Code below is in a _prepareForm() method of an admin form
*/
$dateOutputFormat = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$fieldset->addField('my_date_field', 'date', array(
'name' => 'my_date_field',
'label' => $this->__('My Date Field'),
@herveguetin
herveguetin / Mage_Core_Model_Email_Template.php
Created May 26, 2014 23:53
Bugfix in Mage_Core_Model_Email_Template
<php
/**
* Parse variables string into array of variables
*
* @param string $variablesString
* @return array
*/
protected function _parseVariablesString($variablesString)
{
$variables = array();
@herveguetin
herveguetin / form_own_model_data.php
Last active August 29, 2015 14:01
Enrich Magento product edit admin form with own model data
<?php
/**
* In some Mage_Adminhtml_Block_Catalog_Form class...
*/
protected function _prepareForm()
{
$formDataObject = new Varien_Object() // Your logic to get original data for your form
$form = new Varien_Data_Form();
@herveguetin
herveguetin / canonical_url.php
Created May 26, 2014 22:54
Add canonical URL to Magento <head>
<?php
$canonicalUrl = Mage::getBaseUrl(); // your logic...
$this->getLayout()
->getBlock('head')
->addLinkRel('canonical', $canonicalUrl);
@herveguetin
herveguetin / magento_collection_expression.php
Created May 26, 2014 22:49
Use mySQL expression in Magento collection select
<?php
Mage::getModel('my_model')
->getCollection()
->addAttributeToFilter('my_column', array('gt' => Zend_Db_Expr(MY_EXPRESSION)))
;
@herveguetin
herveguetin / translate_specific_string_module.md
Created May 26, 2014 22:34
Translating strings from specific modules in Magento