Skip to content

Instantly share code, notes, and snippets.

@herveguetin
herveguetin / .gitignore_magento
Last active Oct 24, 2019
Magento .gitignore example
View .gitignore_magento
######################
# Magento .gitignore #
######################
# Magento Core unnecessary files #
##################################
/errors/local.xml
/index.php
/install.php
/mage
@herveguetin
herveguetin / Observer.php
Last active Apr 1, 2019
Dynamically add items to admin menu in Magento
View Observer.php
<?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 Oct 25, 2018
Injecting interfaces, singleton, factory, models in Magento 2
View singleton_instances_magento2.php
<?php
class SomeClass
{
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $configInterface;
/**
* @var \Magento\Framework\App\Config
@herveguetin
herveguetin / load_model_magento2.php
Created Oct 25, 2018
Load model from DB in Magento 2
View load_model_magento2.php
<?php
class SomeClass
{
/**
* @var \Magento\Catalog\Model\ResourceModel\Product
*/
private $productResource;
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
@herveguetin
herveguetin / SomeClass.php
Created Oct 25, 2018
Directive interpolation in Magento 2
View SomeClass.php
class SomeClass
{
public function __construct(
\Magento\Framework\Filter\Template $templateFilter
)
{
$this->templateFilter = $templateFilter;
}
public function getDirectiveContent()
@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 / breadcrumb_custom_helper.php
Last active May 18, 2018
Add custom or CMS-page-title-based translated breadcrumbs with simple layout update in Magento
View breadcrumb_custom_helper.php
<?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
Properly manage dates and datetimes in admin form (displaying and saving data in order to make sure to fit to locale in Magento)
View filter_dates_datetimes.php
<?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 / autoshipping.php
Last active Oct 17, 2017
Set shipping method and rate if they do not exist yet in Magento
View autoshipping.php
<?php
/**
* Has shipping been applied to quote?
*
* @var bool
*/
protected $_hasShipping = false;
/**
@herveguetin
herveguetin / m2_reindexall_programmatically.php
Last active Sep 20, 2017
Reindexing programmatically in Magento 2
View m2_reindexall_programmatically.php
<?php
/**
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Hervé Guétin <herve.guetin@gmail.com> <@herveguetin>
*/
class Reindex
{
/**
* @var \Magento\Indexer\Console\Command\IndexerReindexCommand