Skip to content

Instantly share code, notes, and snippets.

@herveguetin
herveguetin / order_country_name.php
Last active Oct 16, 2018
The right way to get country name for an order address in Magento
View order_country_name.php
<?php
// For billing address
$order->getBillingAddress()->getCountryModel()->getName()
// For shipping address
$order->getShippingAddress()->getCountryModel()->getName()
?>
@herveguetin
herveguetin / create_order_status.php
Last active Mar 1, 2017
This is how to create an new order status in Magento
View create_order_status.php
<?php
// Create new order status
$data = array(
'status' => 'status_code',
'label' => 'Default Label'
);
$status = Mage::getModel('sales/order_status');
$status->setData($data)->save();
// Assign to some order state
@herveguetin
herveguetin / enterprise_category_url_key.php
Created Nov 24, 2014
When using Magento EE, and especially when flat category is active, requesting the "url_key" attribute for a category fails because it is not populated in catalog_category_flat_store_*. This method fixes that.
View enterprise_category_url_key.php
<?php
/**
* Get category URL key for Enterprise
*
* @param Mage_Catalog_Model_Category $category
* @return string
*/
public function getEnterpriseUrlKey(Mage_Catalog_Model_Category $category)
{
$resource = Mage::getSingleton('core/resource');
View Reset Magento permissions
find . -type f -exec chmod 644 {} \; && find . -type d -exec chmod 755 {} \; && chmod 550 mage
@herveguetin
herveguetin / varnish_bypass.js
Last active Aug 29, 2015
Create varnish_bypass cookie for Turpentine on Magento
View varnish_bypass.js
document.cookie="varnish_bypass=1"
@herveguetin
herveguetin / replace_html_tags.php
Last active Aug 29, 2015
Replace HTML tags using PHP DOM objects and Zend_Dom_Query
View replace_html_tags.php
<?php
$replacedTags = array(
'dt' => array( // <dt> tag is replaced by...
'tag' => 'div', // ... <div> tag...
'attributes' => array( // ... with these attributes
'class' => 'some-class',
'id' => 'some_id',
'data-something' => 'some-value'
)
),
@herveguetin
herveguetin / sanitize_zip_fr.js
Created Aug 7, 2014
Sanitize and validate ZIP codes in Magento using Prototype's Validation
View sanitize_zip_fr.js
/**
* A simple ZIP code validator and sanitizer
*
* @author Hervé Guétin <@herveguetin> <herve.guetin@agence-soon.fr>
*/
var zipSanitizer = {
// All sanitizers available
sanitizers: {},
@herveguetin
herveguetin / shipping_method_title.php
Created Jul 31, 2014
Oneliner to retrieve shipping method *title* in Magento
View shipping_method_title.php
Mage::getStoreConfig('carriers/' . $order->getShippingMethod(true)->getCarrierCode() . '/title')
@herveguetin
herveguetin / remove_definer_mysqldump
Created Jul 23, 2014
Removing DEFINER from mysqldump
View remove_definer_mysqldump
mysqldump … | sed -e 's/DEFINER=[^*]*\*/\*/' > dump.sql
@herveguetin
herveguetin / upgrade_add_column.php
Created Jul 22, 2014
The right way to add a column to an existing table in Magento.
View upgrade_add_column.php
<?php
$installer = $this->startSetup();
$installer->getConnection()
->addColumn(
$installer->getTable('module/table_alias'),
'sql_column_name',
array(
'type' => Varien_Db_Ddl_Table::TYPE_INTEGER, // Or any other type from Varien_Db_Ddl_Table
'nullable' => true, // Or false...