Skip to content

Instantly share code, notes, and snippets.

@herveguetin
herveguetin / shipping_method_renderer_available.phtml
Last active August 8, 2023 08:38
Use renderers for shipping methods in Magento
<dl class="sp-methods">
<?php $shippingCodePrice = array(); ?>
<?php $_sole = count($_shippingRateGroups) == 1; foreach ($_shippingRateGroups as $code => $_rates): ?>
<?php echo Mage::helper('your_module/shipping')->getShippingRatesHtml($code, $_rates, $_sole); ?>
<?php endforeach; ?>
</dl>
@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 / .gitignore_magento
Last active October 24, 2019 09:06
Magento .gitignore example
######################
# Magento .gitignore #
######################
# Magento Core unnecessary files #
##################################
/errors/local.xml
/index.php
/install.php
/mage
@herveguetin
herveguetin / Observer.php
Last active April 1, 2019 11:06
Dynamically add items to admin menu in Magento
<?php
class My_Module_Model_Observer {
/**
* Update admin menu with dynamic items
*/
public function updateAdminMenu()
{
$menu = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu/MAIN_MENU_ITEM/children/MENU_ITEMS_CONTAINER/children');
@herveguetin
herveguetin / singleton_instances_magento2.php
Last active October 25, 2018 20:45
Injecting interfaces, singleton, factory, models in Magento 2
<?php
class SomeClass
{
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $configInterface;
/**
* @var \Magento\Framework\App\Config
@herveguetin
herveguetin / load_model_magento2.php
Created October 25, 2018 20:28
Load model from DB in Magento 2
<?php
class SomeClass
{
/**
* @var \Magento\Catalog\Model\ResourceModel\Product
*/
private $productResource;
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
@herveguetin
herveguetin / SomeClass.php
Created October 25, 2018 17:48
Directive interpolation in Magento 2
class SomeClass
{
public function __construct(
\Magento\Framework\Filter\Template $templateFilter
)
{
$this->templateFilter = $templateFilter;
}
public function getDirectiveContent()
@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 / breadcrumb_custom_helper.php
Last active May 18, 2018 01:41
Add custom or CMS-page-title-based translated breadcrumbs with simple layout update in Magento
<?php
/**
* Retrieve current cms page
*
* @return Mage_Core_Model_Abstract
*/
public function getCurrentCmsPage()
{
$pageId = $this->_getRequest()->getParam('page_id', $this->_getRequest()->getParam('id', false));
$page = Mage::getSingleton('cms/page')->load($pageId);
@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'),