:::: MENU ::::
Monthly Archives: January 2018

Magento 2 Find value comma separated string column

Today we discuss Magento 2 find value comma separated string column. Sometime you need to search in comma separated values in database. So for that we use the Finset to search in comma separated values. Let’s take the example of product collection. Let’s spose the field name test_sku have the comma separated values. So here is example.

protected $_productCollectionFactory;
public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory
    ) {
        $this->_productCollectionFactory = $productFactory;
    }
    public function getProductCollection()
    {
       $sku=’test’;    
      return   $this->_productCollectionFactory->create()->addFieldToFilter('test_sku', array('finset' => $sku));
     
     }
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 deploy static content theme

Today we talk about how to use command line interface to do Magento 2 deploy static content theme.Doing the static content deployment is quite a headache. So today i will tell how to deploy content smartly. Magento 2 give option to deploy language example en_US, deploy area example frontend or adminhtml also the deploy the theme example blank or luma. Now we separate these with frontend and backend. This tutorial include the single theme deploy in magento 2, area base deployment example frontend or adminhtml and local base content deploy. Here is shortcut of adding the language area and theme.
-l = language
-a = area
-t = theme

Frontend theme deployment

By default you can use below command to deploy the static content. This is deploy static content for add the languages.

php bin/magento  setup:static-content:deploy

Now we add language filter so it deploy only my specific language example en_Us

php bin/magento  setup:static-content:deploy  en_US

After adding language filter we add the area specific filter so it deploy only my area language example frontend.

php bin/magento  setup:static-content:deploy  en_US -a frontend

After adding area filter we add the theme filter so it deploy only my theme example luma.

php bin/magento  setup:static-content:deploy  en_US -a frontend -t Magento/luma

Bakcend theme deployment

Using php bin/magento setup:static-content:deploy en_US after adding language filter we add the area specific filter so it deploy only my area language example backend. Now it will deploy the backend content only.

php bin/magento  setup:static-content:deploy  en_US -a adminhtml

After adding area filter we add the theme filter so it deploy only my theme example backend.

php bin/magento  setup:static-content:deploy  en_US -a adminhtml-t Magento/backend
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 store information

Today we talk about how to get Magento 2 store information. In Store information we get the current store id , store code , store website id ,store name and also the available currencies in current store.So let start with example.

<?php
namespace QaisarSatti\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        array $data = []
    )
    {            
        parent::__construct($context, $data);
    }
   public function getStoreInfromation()
   {
        return $this->_storeManager->getStore();
   }
}

In phtml file get the information

$storeInformation = $block->getStoreInfromation();

$storeInformation->getStoreId();  //get store id
$storeInformation->getCode();  // get store code
$storeInformation->getWebsiteId(); // get store website id
$storeInformation->getName();  //get store name

//To get available currencies in store you check

foreach($storeInformation->getAvailableCurrencyCodes() as $codes):
  echo $codes;
endforeach;
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 best Seller product collection

Let’s talk about using sales report collection so today we talk about how to get Magento 2 best seller product collection. Best seller collection in depend on number of time product quantity is sold. First we have to inject the Bestsellers\CollectionFactory in you block or helper any other file you want to use it. You can get the report of monthly,yearly and daily and also custom date. So let start with coding. In this example I am going to inject into my block.

Step 1: Create a block file

<?php
    /**
    * Simple Hello World Module
    *
    * @category    QaisarSatti
    * @package     QaisarSatti_HelloWorld
    * @author      Muhammad Qaisar Satti
    * @Email       [email protected]
    *
    */

namespace QaisarSatti\HelloWorld\Block;
class BestSeller extends \Magento\Framework\View\Element\Template
{
 
    protected $_collectionFactory;
   public function __construct(
       \Magento\Backend\Block\Template\Context $context,
      \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
               array $data = []
   ) {
 
       $this->_collectionFactory = $collectionFactory;
             parent::__construct($context, $data);
   }


public function getBestSellerData(){
     
$bestSellerProdcutCollection = $this->_collectionFactory->create()
                    ->setModel('Magento\Catalog\Model\Product')
                    ->setPeriod('month') //you can add period daily,yearly
                   ;
           
       return $bestSellerProdcutCollection;
   }
}

Step 2: Show Output

Getting collection in phtml file from block.

<!--/**
    * Simple Hello World Module
    *
    * @category    QaisarSatti
    * @package     QaisarSatti_HelloWorld
    * @author      Muhammad Qaisar Satti
    * @Email       [email protected]
    *
    */
-->

    <?php
        $bestSeller =  $block->getBestSellerData(); ?>
        <h1>Best Seller Collection.....</h1>
        <ul>
            <?php foreach ($bestSeller as $product) {
                ?>
                <li><?php  echo $product->getProductName();?>--<?php  echo $product->getQtyOrdered();?></li>
            <?php } ?>
</ul>
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 Create custom renderer in admin grid

Today we discussed How to create Magento 2 Create custom renderer in admin grid. Sometime we need get some other values than current values. For example get product name instead of product id. For that we need to create a custom renderer for gird that will show the product name instead of product id so here we start. First we need to add column class that we need to renderer.

Step 1:

Create a ui class of column.

<column name="vendor_id" class="QaisarSatti\HelloWorld\Ui\Component\Listing\Column\Testing">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Vendor</item>
                </item>
            </argument>
        </column>

Step 2:

Create a renderer file for that particular column.

PME\PoManager\Ui\Component\Listing\Column\Vendor

<?php
namespace QaisarSatti\HelloWorld\Ui\Component\Listing\Column;
 
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
 

class Testing extends Column
{
        protected $escaper;
 
      protected $systemStore;
      protected $productloader;
 
 
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        \Magento\Catalog\Model\ProductFactory $productloader,
        Escaper $escaper,
        array $components = [],
        array $data = []
    ) {
        $this->escaper = $escaper;
       $this->productloader = $productloader;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }
 
    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                               $product = $this->productloader>create()->load((int)$item[$this->getData('name')]);


                $item[$this->getData('name')] =$product->getName();
            }
        }
 
        return $dataSource;
    }
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Pages:12