Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Hot answers tagged

19

The class you are trying to extend has this constructor: public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageHelper, array $data = [] ) { $this->_fileStorageHelper = $fileStorageHelper; parent::__construct($context, $data); } so you ...


16

The question is in fact, how do you change the number of product columns. The line you wish to change is generated by this code: <?php $_columnCount = $this->getColumnCount(); ?> <ul class="products-grid products-grid--max-<?php echo $_columnCount; ?>-col"> that you can find in this file: app\design\frontend\Your_Package\Your_Theme\...


16

I'm not completely sure if it has changed between Magento 1 and Magento 2, probably not so I'm gonna go with what I know from Magento 1. The _construct will be called after the __construct The PHP native __construct method should not be overwritten or used in your code. If you want to execute code in a safe way on the start of a class use _construct. ...


15

Yes, getSingleton returns the same instance, so you do not want to use it always, but only if you can live with having the same instance. Example of when NOT to use a singleton: $product = Mage::getModel('catalog/product'); $product->setSku("asdfasdf"); $product->setName("First Test Product"); $product->save(); $product = Mage::getModel('catalog/...


15

The _construct method was a "Varien invention" used to wrap some initialization logic in models, helpers and blocks. So is unusual to change or re-declare the native __construct() method in M1 Models/Blocks or Helpers since we always use the Magento factories. However isn't any problem/bad-practice about using it (if you care of compatibility). In M2 the ...


11

Page Layout XML As far as I'm aware this isn't possible out of the box via page layout XML, the existing implementation is done via PHP (checkout the addDefaultBodyClasses() function here); /** * Add default body classes for current page layout * * @return $this */ protected function addDefaultBodyClasses() { $this->pageConfig->addBodyClass($...


11

You can create plugin after Attribute merge method. Then you can modify checkout fields and add class to wrapper of country select box. In your module create MODULE/NAME/etc/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/...


10

Explanation To pass parameters to contructors you may use the direct injection mechanism in Magento2 (look for di.xml). Defining an argument in a class constructor in Magento 2 is very similar to Magento 1 Mage::getSingleton method. You will get a singleton class of the type you required in the constructor. Magento will just pass you a singleton instance ...


10

echo Company_Module_Model_Somemodel::SOME_CONST; This one is way to go because Magento uses an autoloader and your class will be autoloaded by name For further detail please check the reference link https://stackoverflow.com/questions/16792370/proper-way-to-handle-model-constants-in-magento I hope this will help


9

When you rewrite a helper class, it will only take effect when you use Mage::helper('...'). However, most helper classes extend from Mage_Core_Helper_Data directly and then, the original class is used :( For example: class My_Custom_Helper_Class extends Mage_Core_Helper_Data { } If you want to rewrite such a class in all cases, you can use the ugly ...


9

The sort answer is that it is not defined anywhere. If you trace back how Mage::helper works you get to the function Mage_Core_Model_Config::getGroupedClassName This will try to load any rewrites firstly, then the definition directly from the config and then finally just assume it starts with mage_ if (empty($className)) { if (!empty($config)) { ...


9

There are different reasons to generate different entities: Proxy - it's used for lazy-loading. But each type requires separate proxy class so that would be annoying for developers to write them manually so Magento generates them. Yo will only find references to Proxies in di.xml Factory - is a class that is used to create non-injectables (entities). Same ...


6

You can put your classes in the lib folder but just make sure they follow the naming convention. Class_Name_Here maps to the file Class/Name/Here.php relative to the lib folder


6

So it appears you are trying to rewrite a controller when you could simply rewrite the block Mage_Adminhtml_Block_Catalog_Product_Attribute_Set_Main. <config> <global> <blocks> <netgroup_attributegrouping> <class>Netgroup_Attributegrouping_Block</class> </...


6

As Meenakshi mentioned, there is a class Mage. Mage::log() and Mage::getModel() are static functions Nowadays you should avoid static function at all costs. There are a few little exceptions. Just google for static bad and/or when to use static and you get lots of blog posts and stackoverflow answers :-)


6

You can create before plugin on setTemplate method and overwrite template argument. Create plugin class ProductSwatchPlugin { public function beforeSetTemplate( \Magento\Swatches\Block\Product\Renderer\Configurable $subject, $template ) { return ['You_Module::template.phtml']; } } and declare it in DI.xml <type ...


6

Just upload your code same as below, Keep $this->_coreRegistry = $context->getRegistry(); Full code, public function __construct( \Magento\Catalog\Block\Product\Context $context, \Magento\Framework\Data\Helper\PostHelper $postDataHelper, \Magento\Catalog\Model\Layer\Resolver $layerResolver, CategoryRepositoryInterface $...


5

Mage::getResourceModel('sales/order_item_collection') = Mage_Sales_Model_Resource_Order_Item_Collection this is the resource for a collection of items in orders, normally the database table that this is attached to is sales_flat_order_item Mage::getResourceModel('sales/order') = Mage_Sales_Model_Resource_Order is the resource for a single sales orders and ...


5

The original definition for this class lives in the ./lib/Varien/ folder. If you must override any autoloaded classes from the ./lib folder, you can only do this via inclusion in total in one of the codepools (local, core, community in order of precedence). There is no way to overwrite only one method.


5

Unfortunatly there isn't a very clean solution, but the best one I've come across is the following: call_user_func(array(get_parent_class(get_parent_class($this)), '_afterSave'));


5

If you're comfortable writing code and creating a new Module, follow these instructions to overwrite Mage_Adminhtml_Block_Page class. In your new custom class, add the following method: /** * Remove CSS class from page body tag * * @param string $className * @return Mage_Adminhtml_Block_Page */ public function removeBodyClass($className) { $...


5

Mage class is present in {magento root}/app/Mage.php you can get helper function and log function definition


5

An alternative is to override it using dependency injection. Modify di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/ObjectManager/etc/config.xsd"> <preference for="Magento\Swatches\Block\Product\Renderer\Configurable" type="...


5

It says that \Magento\Catalog\Block\Product\Context already has \Magento\Framework\Registry in it. So instead of injecting both of them, you should inject only Context and get registry from it via \Magento\Catalog\Block\Product\Context::getRegistry()


5

First of all, we need to add Constructor injection method to our custom select class: (Edit: we don't need to add Constructor Injection, we need to add the sequence module) <?php namespace Mike\Newselect\Block\Product\View\Options\Type; class Select extends \Magento\Catalog\Block\Product\View\Options\Type\Select { public function ...


4

I've never seen anything official on this over the years — I vacillate between calling it Magento's pseudo-constructor or Magento's internal-constructor. Looking at the method definition #File: lib/Varien/Object.php /** * Internal constructor not depended on params. Can be used for object initialization */ protected function _construct() { } it looks ...


4

It initiates the resource model this model uses to commit data to the database. So in this case that would be the file app/code/[local|community]/Magentotutorials/Weblog/Model/Resource/Blogpost.php [EDIT] to answer @easymoden00b question, the _init method can be found in Mage_Core_Model_Abstract which directly calls _setResourceModel in the same class /** ...


4

You could put it in a helper class. Here's an example {module_directory}/Helper/StringHelper.php : <?php namespace Your\Module\Helper; use Magento\Framework\App\Helper\AbstractHelper; class StringHelper extends AbstractHelper { function stringFunc($string) { return $string . ' helped'; } } In your template file : <?php echo ...


4

Try to change this line: "input" => "string", to the: "input" => "text", because a component with name 'string' not exist. Then reinstall your module. Note: if on uninstall process your module does not delete the attribute salesAssociate programmatically you should do-it-yourself. Useful link - Magento 2 How to uninstall attributes added by ...


4

No, it's not a good practice to use the object manager. Use a factory instead. To get an instance of \Foo\Bar\My\Class you will need to use an instance of \Foo\Bar\My\ClassFactory that will be automatically generated. I assume that the code you listed in the question is part of a class. you should have in your class this: protected $fooBarFactory; public ...


Only top voted, non community-wiki answers of a minimum length are eligible