Zip in chunks

 

Zip command for large file zip in chunks

zip -r -s 5m myfiles.zip example-dir/
Puneet Kumar Magento Developer

Mysql create user and privileges assign.

 Remove privileges

REVOKE ALL PRIVILEGES ON aws_oxtoolstaging.* FROM ‘aws_oxtoolstaging’@’localhost’;

Assign privileges

CREATE USER ‘username’@’localhost’ IDENTIFIED BY ‘S1@gingin’;

GRANT ALL PRIVILEGES ON databases . * TO ‘username’@’localhost’;

show privileges

show grants for ‘username’@’localhost’;

Puneet Kumar Magento Developer

magento 2 how to disable inventory modules.

please look below link it would help. to disable MSI module

 https://meetanshi.com/blog/disable-magento-msi/

Puneet Kumar Magento Developer

Product saleable qty not showing and update in magento 2.x

 I have same issue with resolved, take inventory_source_item table backup first.

Run sql query:

INSERT IGNORE INTO `inventory_source_item` (source_code, sku, quantity, status)
select 'default', sku, qty, stock_status from (`cataloginventory_stock_status` as `lg` join `catalog_product_entity` as `prd` on((`lg`.`product_id` = `prd`.`entity_id`)))

Apply command:

php bin/magento indexer:reindex

php bin/magento cache:flush

Puneet Kumar Magento Developer

How To Install PHP 7.4 / 7.3 / 7.2 on Ubuntu 18.04 / Ubuntu 16.04

 PHP is an open-source programming language widely used for web development. It is an HTML-embedded scripting language for building dynamic web sites.

By default, Ubuntu 18.04 and Ubuntu 16.04 ships PHP v7.2 (EOL) and PHP v7.0 (EOL), respectively.

In this post, we will see how to install PHP 7.4 / 7.3 / 7.2 on Ubuntu 18.04 / Ubuntu 16.04.

Add PHP Repository

Ondřej Surý, a third-party repository, hosted on the launchpad, which offers PHP 7.4 / 7.3 / 7.2 for Ubuntu operating system.

Update the repository cache.

sudo apt update

Install the below packages.

sudo apt install -y curl wget gnupg2 ca-certificates lsb-release apt-transport-https

Add the repository to your system.

sudo apt-add-repository ppa:ondrej/php

Update the repository index.

sudo apt update

Install PHP

Install PHP 7.4 on Ubuntu 18.04 / Ubuntu 16.04

Install PHP 7.4 with the below command.

sudo apt install -y php7.4 php7.4-cli php7.4-common

Install PHP 7.3 on Ubuntu 18.04 / Ubuntu 16.04

Install PHP 7.3 with the below command.

sudo apt install -y php7.3 php7.3-cli php7.3-common

Install PHP 7.2 on Ubuntu 18.04 / Ubuntu 16.04

Install PHP 7.2 with the below command.

sudo apt install -y php7.2 php7.2-cli php7.2-common
You can also have multiple PHP versions on your system. Each PHP version will have separate configuration files under /etc/php/7.X directory.

Verify PHP Version

Once you have installed the PHP package, check its version.

php -v

Output:

PHP 7.4.5 (cli) (built: Apr 19 2020 07:36:30) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies

PHP Support for Web Server

Both Apache and Nginx do not support the processing of PHP files by default when the browser requests the PHP page. So, we need to install the PHP package on the server to support PHP files.

PHP Support for Apache

You can install a below package with Apache webserver to support PHP for the LAMP stack. This package provides the PHP modules for Apache 2 web server.

ADVERTISEMENT

READ: How To Install LAMP Stack on Ubuntu 18.04 / Ubuntu 16.04

Change PHP version, if required

sudo apt install -y apache2 libapache2-mod-php7.4

PHP Support for Ngnix

Nginx does not have a PHP modules package to support PHP for the LEMP stack. To have PHP support for Nginx, we can use the PHP FastCGI Process Manager to handle PHP requests.

READ: How To Install LEMP Stack on Ubuntu 18.04 / Ubuntu 16.04

Change PHP version, if required

sudo apt install -y php7.4-fpm

PHP-FPM listens on the socket run/php/php7.4-fpm.sock by default.

To make PHP-FPM use TCP connection, edit the below file.

sudo nano /etc/php/7.4/fpm/pool.d/www.conf

Then, change the listen parameter.

FROM:

listen = /run/php/php7.4-fpm.sock

TO:

listen = 127.0.0.1:9000

Add socket details in Nginx virtual host.

server {

# other codes

  location ~* .php$ {
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  }
}

Install PHP Extensions

PHP extensions enable specific functions/support for your code. For example, installing PHP MySQL extension will let your PHP code to connect with the MySQL database.

PHP extensions package is normally named like php<version>-<extn_name>.

To install MySQL support for PHP v7.4, you can install a php7.4-mysql package.

sudo apt install -y php7.4-mysql

Once you have installed a required extension, use the below command to verify it.

php -m | grep -i mysql

Output:

mysqli
mysqlnd
pdo_mysql

PHP Extensions for CMS

PHP Extensions for WordPress

The following extensions are required to install and run WordPress on Ubuntu. WordPress recommends having PHP v7.3 for the installation.

sudo apt install -y php7.3-mysql php7.3-dom php7.3-simplexml php7.3-ssh2 php7.3-xml php7.3-xmlreader php7.3-curl  php7.3-exif  php7.3-ftp php7.3-gd  php7.3-iconv php7.3-imagick php7.3-json  php7.3-mbstring php7.3-posix php7.3-sockets php7.3-tokenizer

PHP Extensions for Joomla

The following extensions are required to install and run Joomla on Ubuntu. Joomla requires PHP v7.1 and above.

sudo apt install -y php7.3-mysql php7.3-xml php-pear php7.3-json

PHP Extensions for Drupal

The following extensions are required to install and run Drupal on Ubuntu. Drupal requires PHP v7.1 and above.

sudo apt install -y php7.3-mysql php7.3-dom php7.3-gd php7.3-json php7.3-pdo php7.3-simplexml php7.3-tokenizer php7.3-xml
Puneet Kumar Magento Developer

Magento 2 Java Script Bundling

JavaScript Bundling

Java-Script bundling is an optimization technique, you can use to reduce the number of files server requests for JavaScript requests. Bundling accomplishes this by merging files multiple JavaScript files together into one file to reduce the number of page requests.

Enable Java-Script bundling

JavaScript bundling does not work unless Magento is in production mode. Once in production mode, JavaScript bundling can only be enabled using the CLI. Follow these steps to setup JavaScript bundling from the CLI.

  1. From the Magento root directory, switch to production mode:

    bin/magento deploy:mode:set production
    

  2. Enable JavaScript bundling:

    bin/magento config:set dev/js/enable_js_bundling 1
    

  3. Optimize bundling by minifying JavaScript files:

    bin/magento config:set dev/js/minify_files 1
    

  4. Enable cache busting on static file URLs. This ensures users get the latest version of the assets anytime they update:

    bin/magento config:set dev/static/sign 1
    

  5. To configure JavaScript bundling, you must disable Javascript file merging. Bundling will not work as the merging of files excludes bundling:

    bin/magento config:set dev/js/merge_files 0
    

  6. Modifying the settings above when Magento is in production mode will require static view files deployment:

    bin/magento setup:static-content:deploy
    

  7. Finally, clear the cache:

    bin/magento cache:clean config
    

    For example, when Sign Static Files is disabled (which is the default: config:set dev/static/sign 0), the URL to a static file might look like this: /static/frontend/Magento/luma/en_US/mage/dataPost.js. But when you enable the setting (config:set dev/static/sign 1), the same URL might look something like this: static/version40s2f9ef/frontend/Magento/luma/en_US/mage/dataPost.js, with a version number added as shown. The next time this file is updated (with bin/magento setup:static-content:deploy), a new version will be generated, causing the browser to download a new file from the server, thus busting the browser’s cache.

How bundling works in Magento

When you enable bundling, Magento combines hundreds of JavaScript files into just a few JavaScript bundles and downloads those bundles for each page. Because the browser downloads the bundles synchronously, page rendering is blocked until all bundles finish downloading. But the time saved from reducing server requests from hundreds to just a few, usually offsets the cost of downloading the bundles synchronously.

Excluding files

The <exclude> node in the etc/view.xml file for a theme specifies the files to exclude from the Magento JavaScript bundling process. JavaScript files excluded from bundling are loaded asynchronously by RequireJS as needed.

As such, you should exclude the JavaScript files you use for testing or development so that they are not loaded on every page.

The following code snippet from Magento’s Luma theme shows the types of files you should exclude from the bundling process.

Show example

Setting bundle file size

The bundle_size variable controls the file size of the generated bundles. Specifying a large bundle_size reduces the number of bundles generated, but generates larger file sizes. Specifying a smaller bundle_size generates more bundles with a smaller file sizes.

Example:

1
2
3
<vars module="Js_Bundle">
    <var name="bundle_size">1MB</var>
</vars>

The goal is to balance the number of bundles to download with the size of each bundle. As a rule of thumb, each bundle should be at least 100 kB.

Fine tuning your theme

There are many ways to tune your theme using the etc/view.xml file.

For example, the Magento Luma theme is configured to work well for all pages, but you can maximize browser performance for home, catalog, or product pages by adding items to or removing items from the <exclude> node.

Follow these steps to help you identify which JavaScript files to bundle for your theme:

  1. Create a blank page with the layouts you would like to tune.
  2. Compare the JavaScript files loaded in the pages with the JavaScript files in Magento.
  3. Use the results of that comparison to build your exclude list.

 

Puneet Kumar Magento Developer

Magento 2 Error ERR_TOO_MANY_REDIRECTS

 ‘m new to Magento, after installation of Magento 2 community edition.I am not able to access the admin panel. I am getting below error.

This web page has a redirect loop

Error ERR_TOO_MANY_REDIRECTS

Please help me to resolve the issue. Advance thanks to all.

Solution

php bin/magento config:set web/unsecure/base_url http://example.local/ 
php bin/magento config:set web/secure/base_url https://example.local/ 
php bin/magento config:set web/secure/use_in_adminhtml 1
php bin/magento cache:flush
Puneet Kumar Magento Developer

What is Factory class in Magento 2

 

Click here for real example video

What is Factory
class in Magento 2

Overview

Factory classes are service class that instantiate
non-injectable classes, that is, models that represent a database entity.
They create a layer of abstraction between the 
ObjectManager and
business code.

Relationship to ObjectManager

The MagentoFrameworkObjectManager is the class responsible for instantiating objects in
the Magento application. Magento prohibits depending on and directly using
the 
ObjectManager in
your code.

Factories are an exception to this rule because
they require the 
ObjectManager to instantiate specific models.

The following example illustrates the relationship between a
simple factory and the 
ObjectManager:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

namespace MagentoFrameworkAppConfig;

 

class BaseFactory

{

  /**

   * @var
MagentoFrameworkObjectManagerInterface

   */

  protected $_objectManager;

 

  /**

   * @param
MagentoFrameworkObjectManagerInterface $objectManager

   */

  public function __construct(MagentoFrameworkObjectManagerInterface $objectManager)

  {

    $this->_objectManager = $objectManager;

  }

 

  /**

   * Create config model

   * @param
string|MagentoFrameworkSimplexmlElement $sourceData

   * @return
MagentoFrameworkAppConfigBase

   */

  public function create($sourceData = null)

  {

    return $this->_objectManager->create(MagentoFrameworkAppConfigBase::class, [‘sourceData’ => $sourceData]);

  }

}

Writing factories

Unless you require specific behavior for your factory
classes, you do not need to explicitly define them because they are an automatically generated class type. When you
reference a factory in a class constructor, Magento’s object manager generates the factory class
if it does not exist.

Factories follow the naming convention <class-type>Factory where <class-type> is
the name of the class the factory instantiates.

For example the automatically generated MagentoCmsModelBlockFactory class
is a factory that instantiates the class MagentoCmsModelBlock.

Using factories

You can get the singleton instance of a factory for a
specific model using dependency injection.

The following example shows a class getting the BlockFactory instance
through the constructor:

1

2

3

function __construct ( MagentoCmsModelBlockFactory $blockFactory)
{

    $this->blockFactory = $blockFactory;

}

Calling the create() method on a factory gives you
an instance of its specific class:

1

$block
= $this->blockFactory->create();

For classes that
require parameters, the automatically generated 
create() function accepts an array of
parameters that it passes on to the 
ObjectManager to create the target class.

The example below shows the construction of a MagentoFrameworkFlagFactory object
by passing in an array of parameters to a factory:

1

2

$flag
= $this->flagFactory->create([

 
‘data’ =>  [‘flag_code’ =>
‘something’]

The Flag class has a $data constructor parameter which
corresponds to the data key in the 
create array above.

Interfaces

Factories are smart enough to resolve dependencies and allow
you to get the correct instance of an interface as defined in your
module’s 
di.xml.

For example, in the CatalogInventory module,
the 
di.xml file
contains the following entry:

1

 <preference for=“MagentoCatalogInventoryApiDataStockItemInterface” type=“MagentoCatalogInventoryModelStockItem” />

It instructs Magento to
use the specific 
Item class wherever
the StockItemInterface is
used. When a class in that module includes the factory 
StockItemInterfaceFactory as
a dependency, Magento generates a factory that is capable of creating the
specific 
Item objects.

Puneet Kumar Magento Developer

MySQL: Cloning a MySQL database on the same MySql instance

 As the manual says in Copying Databases you can pipe the dump directly into the mysql client:

  mysqldump db_name | mysql new_db_name  

If you’re using MyISAM you could copy the files, but I wouldn’t recommend it. It’s a bit dodgy.

Integrated from various good other answers

Both mysqldump and mysql commands accept options for setting connection details (and much more), like:

 mysqldump -u <user name> --password=<pwd> <original db> | mysql -u <user name> -p <new db> 

Example : mysqldump -u root -pmypassword old_database | mysql -u root -pmypassowrd new_database 
Puneet Kumar Magento Developer