Setting up a LXC / LXD machine for development on Ubuntu/Debian

One of my favourite things I learned working at Smile was using LXC for development. It’s different from Docker in the sense that you don’t configure services and things like that: you just create a exact copy of the environment you’re going to have in production, with all its services included. You can do that with Docker, but it’s not the same. Above all when I’m already used to LXC and don’t see the need Read more…

[Magento 2] Delete All Categories Programmatically

There are a lot of articles on Internet proposing the deletion of all categories using a sql query. I’ve tested one of them and I’ve run into several problems due to this (url keys not deleted correctly, deletion of the Default category…). There is a safer way, using Magento tools. Things you need to know: You’ll need to inject the classes CategoryFactory and Registry Before deleting a category, it’s necessary to use the Registry to Read more…

[Magento 2] Adding product attributes to quote items getProduct function

When calling the function $block->getProduct() in a quote item in the cart page, Magento 2 will return the product and its basic attributes. The attributes that can be retrieved from the product when calling this function in a quote_item page can be found in the file vendor/magento/module-sales/etc/catalog_attributes.xml. From your own module, you can create the file Vendor/Module/etc/catalog_attributes.xml and add the attributes you need: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd"> <group name="quote_item"> <attribute name="your_attribute_code1"/> <attribute name="your_attribute_code2"/> </group> Read more…

[Magento 2] Update product attribute programmatically

It’s pretty easy when you have the eavSetup in your setup module. It’s only necessary to execute: $eavSetup->updateAttribute(Product::ENTITY, $attributeCode, $attributeField, $value); For example, if I want to change the ‘position’ field value of the ‘price’ product attribute: $eavSetup->updateAttribute(‘catalog_product’, ‘price’, ‘position’, 100);

[Magento 2] Create Multiselect Product Attribute Programmatically

It might be tricky if you want to create a multiselect product attribute in Magento 2. For that, it’s necessary to pass the correct backend model: if you don’t do it your attribute will be created but the values won’t be saved. Taking this Inchoo article as a basis, you just need to know the fields you should pass to your attribute. The 3 important values here are: Type and Input: these 2 values have Read more…

[Magento 2] Display category image in navigation menu

[Edited] I’ve updated the article to do this in a better way Recently I had to do something I thought it was simple to do: add the category images to the navigation menu. However, Magento doesn’t use a template to draw the whole menu but a function that will retrieve all categories and generate the ul/li tree. In Magento 2.1.X you can do this rewriting the _getHtml() function from the class Magento\Theme\Block\Html\Topmenu. This is a Read more…

[Magento 2] Get current product

Getting the “current product” in Magento 2 is trickier than it was in Magento 1. However, it is like that for a reason: Mage:: is a thing from the past, no more accessing to everything from everywhere (someone I know would say: “this is not logic at all”). That’s the reason why, in order to get the current product in a product page, you’ll need to use the class Magento\Framework\Registry in your block and add the Read more…

[Forms] Remove a field from a Form

Recently I wanted to remove a field from a form object. It was easy, we need to know 2 things: Fieldset name (‘main’ by default) Field name With this information we can just update the form object with: <?php $this->getForm()->removeField(‘password_fieldset’); ?>

Filter a collection when the attribute value is not set

Since the very first time I started working with collections in Magento I run into situations where I needed to filter a collection by an attribute whose value is not set yet (for example, when the attribute is of the type ‘yes/no’, values 0/1). If I wanted to filter by all the products with a value ‘1’, that was easy, but when I was on the case of filtering by ‘0’, then the collection filter Read more…