Magento 2 Notes

Posted Over 4 years ago. Visible to the public.

1) Non Auto Increment Primary key

The $_isPkAutoIncrement should be assigned as false in resource model, when a primary key of a table is used as non auto increment.

protected $_isPkAutoIncrement = false;

2) Custom Cron Job

Following two files are mandatory to execute a process using cron

A class file and a crontab.xml file

class Notifier
{
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function execute()
    {
        $this->logger->info('Cron is executed');
    }
}
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <job name="low_stock_notification" instance="Dilmah\LowStockNotification\Cron\Notifier" method="execute">
            <schedule>*/5 * * * *</schedule>
        </job>
    </group>
</config>

3) How to get store-wise config data

A mothod should be written as follows in a Helper cleas that extends magento's AbastractHelper

public function getModuleConfig($path,$storeId = null)
    {
        return $this->scopeConfig->getValue(
            'lowstocknotification/' . $path,
            ScopeInterface::SCOPE_STORE,$storeId
        );
    }

4) How to get lowstock collection

Magento displays low stocks as a report and use a collection to get the low stock data.

private function getLowstockItems($storeId)
    {
        /** @var $collection \Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection  */
        $collection = $this->lowstockCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->setFlag('has_stock_status_filter', false);

        $collection->filterByIsQtyProductTypes()->joinInventoryItem(
            'qty'
        )->useManageStockFilter(
            $storeId
        )->useNotifyStockQtyFilter(
            $storeId
        )->setOrder(
            'qty',
            \Magento\Framework\Data\Collection::SORT_ORDER_ASC
        );

        if ($storeId) {
            $collection->addStoreFilter($storeId);
        }

        return $collection;

    }

5) Mini cart is not loaded the item added by custom action

Mini cart should be refreshed by adding the custom action in section.xml.

6) Dont use same route id for frontend and backend in route.xml

7) create admin user

php bin/magento admin:user:create --admin-user='newadmin' --admin-password='!ssvadmin123!' --admin-email='newadmin@domain.com' --admin-firstname='NewAdmin' --admin-lastname='NewAdmin'

8) Create url in block

public function getPrivacyPolicyLink()
    {
        return $this->_urlBuilder->getUrl('privacy-policy-cookie-restriction-mode');
    }

9) Getting request param in a block

public function getPaymentChoice()
    {
        return $this->getRequest()->getParam('choice');
    }

10) Adding custom block in admin order creation page

a) Select the admin order block to add the custom block
b) Add the custom block as child block in overridden admin order creation block
c) Admin order block can be overridden using preference in di.xml

11) How to fix Element 'block', attribute 'remove': exception

a)Search in layout xml that any block statement contains remove attribute

b)Change that as

<referenceBlock name="catalog.topnav" remove="true" />
  1. Mysqldump
mysqldump --no-tablespaces --column-statistics=0 -h20.0.0.71 -ubags -p bags > bags30112020.sql

  1. Adding Admin Configuration

If a module need system configuration, the system.xml should be added in your module.

a) Create a system.xml file in etc/adminhtml
b) Define the tab name of the system configuration

        <tab id="sendemailtab" translate="label" sortOrder="100">
            <label>Send Email Carbon Copy</label>
        </tab>

c) Define the section and fields to save in system configuration

<section id="sendemail" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Add Emails</label>
            <tab>sendemailtab</tab>
            <resource>Vasan_SendEmail::sendemail</resource>
            <group id="cities" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label> Add City with email </label>
                <field id="city_email" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>City and Email</label>
                    <frontend_model>Vasan\SendEmail\Block\Adminhtml\Form\Field\Cities</frontend_model>
                    <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
                </field>
            </group>
        </section>
  1. Granting for existing user
GRANT ALL PRIVILEGES ON accstaging.* TO 'medion'@'%';
  FLUSH PRIVILEGES;
vasan
Last edit
Almost 2 years ago
vasan
Posted by vasan to vasan's deck (2020-01-18 09:27)