Skip to content

Instantly share code, notes, and snippets.

@herveguetin
herveguetin / order_country_name.php
Last active October 16, 2018 07:53
The right way to get country name for an order address in Magento
<?php
// For billing address
$order->getBillingAddress()->getCountryModel()->getName()
// For shipping address
$order->getShippingAddress()->getCountryModel()->getName()
?>
@herveguetin
herveguetin / create_order_status.php
Last active March 1, 2017 10:28
This is how to create an new order status in Magento
<?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 November 24, 2014 12:58
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.
<?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');
@herveguetin
herveguetin / Reset Magento permissions
Created November 20, 2014 18:18
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 August 29, 2015 14:08
Create varnish_bypass cookie for Turpentine on Magento
document.cookie="varnish_bypass=1"
@herveguetin
herveguetin / replace_html_tags.php
Last active February 15, 2023 05:55
Replace HTML tags using PHP DOM objects and Zend_Dom_Query
<?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 August 7, 2014 16:35
Sanitize and validate ZIP codes in Magento using Prototype's Validation
/**
* 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 July 31, 2014 13:02
Oneliner to retrieve shipping method *title* in Magento
Mage::getStoreConfig('carriers/' . $order->getShippingMethod(true)->getCarrierCode() . '/title')
@herveguetin
herveguetin / remove_definer_mysqldump
Created July 23, 2014 10:26
Removing DEFINER from mysqldump
mysqldump … | sed -e 's/DEFINER=[^*]*\*/\*/' > dump.sql
@herveguetin
herveguetin / upgrade_add_column.php
Created July 22, 2014 09:44
The right way to add a column to an existing table in Magento.
<?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...