Get all active payment method list by store id Magento 2.

You can get all the active payment method list using Magento 2 by PaymentMethodListInterface interface.

You can fetch the list of all active payment method only with the specific store by Payment title/code and name.

You just need to pass current store id in the getActiveList() method of above interface. Continue reading “Get all active payment method list by store id Magento 2.”

Create customer address attribute Programmatically using Patch data in Magento 2.3

Create a customer address attribute programmatically in Magento 2 using the simple module by Best Coding Standard Approach using Setup patch data.

In our demo, I have created a simple text field attribute for a customer address called Nickname.

You can see a customer attribute in backend using Customer -> All Customer -> Click on Edit Customer Link.
Go to Addresses Tab and edit or add new Address. Continue reading “Create customer address attribute Programmatically using Patch data in Magento 2.3”

Use of fetchOne() method in sql query Magento 2.

You can run a direct SQL query for fetching only first value using fetchOne() method.

fetchOne() Fetches the first column of the first row of the SQL result as output.

You can use direct SQL query for the fetch the first column of the first row using below way, Get Product id by Product SKU,

<?php
class UpdateQuery {
   public function __construct(
       \Magento\Framework\App\ResourceConnection $resource
   ) {
       $this->resource = $resource;
   }

   /**
    * Update Sql Query
    */
   public function runUpdateQuery()
   {
      $connection  = $this->resource->getConnection();
      $tableName = $connection->getTableName("catalog_product_entity");
      
      $select = $connection->select()->from($tableName, 'entity_id')->where('sku = :sku');
      $sku = "24-MB01";
      $bind = [':sku' => (string)$sku];

      return (int)$connection->fetchOne($select, $bind);
  }
}

Output is 1. //entity_id for SKU 24-MB01

Check for other Direct SQL Query in Magento 2