Skip to main content

Posts

Showing posts from December, 2019

My Account display only after login

If Want to know how to display "My Account" only after customer LogIn. After LogOut it will again be hidden on the same place. The customer_logged_* layout handles used in M1 were removed in M2. Below is a small plugin using a method similar to what's used in core for adding/removing the "Register" top link in Magento\Customer\Block\Account\RegisterLink Pawan/Myaccount/Plugin/Link.php namespace Vendor\Module\Plugin; use Magento\Customer\Model\Context; class Link {     protected $httpContext;     public function __construct(         \Magento\Framework\App\Http\Context $context     ) {         $this->httpContext = $context;     }     public function afterToHtml(\Magento\Customer\Block\Account\Link $subject, $result)     {         if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {             return $result;         }         return '';     } } Pawan/Myaccount /etc/frontend/di.xml <?xml version="1.0"

Magento 2 UI Component Grid Explanation

1) layout file inside Company/Module/view/adminhtml/layout/routerid_controller_action.xml define grid as uiComponent with: 2) uiComponent is defined in Company/Module/view/adminhtml/ui_component/listing_name.xml file. File name must be the same as uiComponent name used in layout file. The structure of the file may seem pretty complex at first sight but as always these are some repeating nodes. To make it simple lets slice it. Main node of the component file is <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">. It is fixed and I believe it requires namespace location attribute. Next there are typically 4 nodes inside <listing /> node: <argument />, <dataSource />, <container /> and <columns />. This is however not a strict setup as <argument /> node might be duplicated to provide more configuration or <container /> as

EAV Entity type in Magento 2

Magento 2 divides entity into 8 types as below: customer – Entity Id = 1 customer_address – Entity Id = 2 catalog_category – Entity Id = 3 catalog – Entity Id = 4 order – Entity Id = 5 invoice – Entity Id = 6 creditmemo – Entity Id = 7 shipment – Entity Id = 8 (You can see in the eav_entity_type table )

Magento 2: Show price for out of stock configurable products

 If want to show price for out of stock configurable product price you need to check  vendor/magento/module-configurable-product/Pricing/Price/ConfigurablePriceResolver.php Find function on line number 61 public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product) Before Editing ::: public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product)     {        $price = null;        foreach ($this->lowestPriceOptionsProvider->getProducts($product) as $subProduct) {             $productPrice = $this->priceResolver->resolvePrice($subProduct);             $price = isset($price) ? min($price, $productPrice) : $productPrice;         }        return (float)$price; } After Editing ::: public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product)     {         $price = null;               $assPrice=array();         foreach ($this->configurable->getUsedProducts($pr

Magento 2 Enterprice B2B Category Permissions getting changed automatically

Symptoms: 1) Change only the description, but not the permissions, when the cron runs, it changes the description and resets permission to deny. 2) But if change description and permissions, when cron runs, both description and permissions are set OK. When ever 1 symptom takes place then consumer cron is over-writing permission data to deny by pulling data from shared catalog table where we have permissions as deny. Below is hot fix for permission issue which I am sharing link To apply the patch: If you don't have a directory named  m2-hotfixes  in the project  root , please create one. Patches we provide will be put here; Copy .patch file(s) to  m2-hotfixes  directory; git add m2-hotfixes; Commit changes; Push changes;                             Click here for  m2-hot-fix

Magento 2 coupon code restrict for only some associated simple products

If you want restrict coupon code for some associate simple products  Like for example  if you have one configurable product and it associated 5 simple products(associate products). I created coupon code to apply only 3 simple product if i add separate simple products it work but when if add configurable it won't work. if i add 5th product to cart it work .if i add configurable products it won't work. Configurable product :: testconfig simple products :: 1,2,3,4,5.If you  want use your coupon need to apply 4 and 5 product only. You need to change the below file. Magento/Quote/Model/ CouponManagement.php override this file into your custom module using di.xml and change the below function   public function set($cartId, $couponCode) Before adding customization look like below  public function set($cartId, $couponCode)     {         /** @var  \Magento\Quote\Model\Quote $quote */         $quote = $this->quoteRepository->getActive($cartId);         if

Magento 2 add associate Simple Products with custom options to Configurable Product not working

If want to add the simple products with custom options to configurable product.When you want show custom options and simple products for configurable product. when we save product that time if custom options having of that product while saving time in database  catalog_product_entity  table  required_options  will  update as 1 if not having it updating as 0. Based on this field configuration data filtering If take a look in to this file you will get idea  public_html\vendor\magento\module-configurable-product\Model\Product\Type\Configurable.php In this file found below function  private function getConfiguredUsedProductCollection code around line present code below function line number 1408 to 1411 private function getConfiguredUsedProductCollection(         \Magento\Catalog\Model\Product $product,         $skipStockFilter = true     )     $collection                 ->addAttributeToSelect($this->getAttributesForCollection($product))                  

Where to use object manager in Magento 2

You can depend on and use the  ObjectManager  class in the following scenarios: You can use the object manager in static magic methods like __wakeup(),  __sleep() , etc. An example can be found in the  __wakeup()  method in the  Magento/Eav/Model/Entity/Attribute/AbstractAttribute  class. You can use the  ObjectManager  to maintain backward compatibility for a constructor. In a global scope, like in fixtures of integration tests, you can use the object manager. The object manager can be a dependency in classes used for the creation of objects, e.g.  factories  or  proxies . Reference  Magento 2 Official Docs In nutshell al dependencies(except above cases) must be injected in the constructor.  Dependency Injection  will do the work. Then you will do specialised methods that return what you need. More specifically in the scenario of providing data to templates,  Magento  recommends using  ViewModels  which are nothing but service classes that provide needed information in