We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Magento 2.4.x: Remove Content Security Policies Errors in Console

    Create config.xml in your custom module and add below code
    Vendor/Module/etc/config.xml
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
        <default>
            <csp>
                <mode>
                    <storefront>
                        <report_only>0</report_only>
                    </storefront>
                    <admin>
                        <report_only>0</report_only>
                    </admin>
                </mode>
            </csp>
        </default>
    </config>
    Add a domain to the whitelist
    Create csp_whitelist.xml in your custom module and add below code
    Vendor/Module/etc/csp_whitelist.xml
    <?xml version="1.0"?>
    <csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp/etc/csp_whitelist.xsd">
        <policies>
            <policy id="script-src">
                <values>
                    <value id="cdn.jsdelivr.net" type="host">https://cdn.jsdelivr.net/</value>
                </values>
            </policy>
            <policy id="style-src">
                <values>
                    <value id="fontawesome" type="host">https://fontawesome.com</value>
                </values>
            </policy>
            <policy id="img-src">
                <values>
                    <value id="google.co.in" type="host">https://www.google.co.in/</value>
                </values>
            </policy>
            <policy id="connect-src">
                <values>
                    <value id="ct.pinterest.com" type="host">https://ct.pinterest.com</value>
                </values>
            </policy>
            <policy id="font-src">
                <values>
                    <value id="cloudflare" type="host">https://cloudflare.com</value>
                </values>
            </policy>
            <policy id="frame-src">
                <values>
                    <value id="pinterest" type="host">https://pinterest.com</value>
                </values>
            </policy>
            <policy id="form-action">
                <values>
                    <value id="pinterest.com" type="host">https://pinterest.com</value>
                </values>
            </policy>
        </policies>
    </csp_whitelist>

     

    Magento 2 : How to check use default value with all categories & all products?

    If you want all products to have Use Default Value as CHECKED, then run below SQL in the database:

    DELETE FROM `catalog_product_entity_text` where store_id = 1;
    DELETE FROM `catalog_product_entity_datetime` where store_id = 1;
    DELETE FROM `catalog_product_entity_decimal` where store_id = 1;
    DELETE FROM `catalog_product_entity_int` where store_id = 1;
    DELETE FROM `catalog_product_entity_varchar` where store_id = 1;

    If you want all categories to have Use Default Value as CHECKED, then run below SQL in the database:

    DELETE FROM `catalog_category_entity_text` where store_id = 1;
    DELETE FROM `catalog_category_entity_datetime` where store_id = 1;
    DELETE FROM `catalog_category_entity_decimal` where store_id = 1;
    DELETE FROM `catalog_category_entity_int` where store_id = 1;
    DELETE FROM `catalog_category_entity_varchar` where store_id = 1;

     

     

    Magento 2.4.x: How to install coding Standard and errors debug command?

    Follow below steps to install Using composer : 

    - composer require --dev magento/magento-coding-standard
    - php bin magento setup:upgrade
    - php bin/magento cache:flush

    You are done with installations.

    Now, You can check Magento coding standard errors using the below command for Custom module :

    vendor/bin/phpcs --standard=Magento2 app/code/Vendor/Module

    After running the above command, you can see coding errors and warnings. You need to resolve those errors & warnings by adopting Magento coding standards.

    That’s It.

    How to add load more button in any listing page in Magento 2?

    In one of the Magento 2 project, we have added a load more button on the listing page.

    So whenever the user clicks on the load more button, the next page of products will be shown.

    You can use below on the custom listing page.

    <script src ="https://infiniteajaxscroll.com/vendor/jquery-ias/dist/jquery-ias.min.js"></script>
    <link src = "https://raw.githubusercontent.com/ravbetsky/infinite-ajax-scroll/master/dist/css/jquery.ias.css"/>
    <script type="text/javascript">
    var ias = jQuery.ias({
    container: ".main",
    item: ".products-grid",
    pagination: ".toolbar .pages",
    next: ".next"
    });
    ias.extension(new IASSpinnerExtension()); // shows a spinner (a.k.a. loader)
    ias.extension(new IASTriggerExtension({offset: 1})); // shows a trigger after page 3
    ias.extension(new IASNoneLeftExtension({
    text: 'There are no more pages left to load.' // override text when no pages left
    }));
    </script>

    You will also need to change class for this as shown below.

    container: ".main",
    item: ".products-grid",
    pagination: ".toolbar .pages",
    next: ".next"

    That’s it.

    How to discontinue product backend settings?

    To achieve this, create a basic module and below code in InstallSchema.php file.

    Change the vendor namespace and module name as per your requirement.

    <?php
    namespace MagemonkeysDiscontinueSetup;
    
    use MagentoEavSetupEavSetup;
    use MagentoEavSetupEavSetupFactory;
    use MagentoFrameworkSetupInstallDataInterface;
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    
    class InstallData implements InstallDataInterface
    {
        private $eavSetupFactory;
    
        public function __construct(EavSetupFactory $eavSetupFactory)
        {
            $this->eavSetupFactory = $eavSetupFactory;
        }
    
        public function install(
            ModuleDataSetupInterface $setup,
            ModuleContextInterface $context
        )
        {
            /** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    
            /**
             * Add attributes to the eav/attribute
             */
               $eavSetup->addAttribute(
                MagentoCatalogModelProduct::ENTITY,
                'discontinue',
                [
                    'group' => 'Discontinue Product Information',
                    'type' => 'int',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Discontinue',
                    'input' => 'boolean',
                    'class' => '',
                    'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
                    'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => false,
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => true,
                    'unique' => false
                ]
            );
    
               /**
             * Add attributes to the eav/attribute
             */
               $eavSetup->addAttribute(
                MagentoCatalogModelProduct::ENTITY,
                'alternate_product_url',
                [
                     'group' => 'Discontinue Product Information',
                     'type' => 'text',
                     'backend' => '',
                     'frontend' => '',
                     'label' => 'Alternate Product URL',
                     'input' => 'text',
                     'class' => '',
                     'source' => '',
                     'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
                     'visible' => false,
                     'required' => false,
                     'user_defined' => false,
                     'default' => '',
                     'searchable' => false,
                     'filterable' => false,
                     'comparable' => false,
                     'visible_on_front' => true,
                     'used_in_product_listing' => true,
                     'unique' => false,
                     'apply_to' => ''
                ]
            );
               $eavSetup->addAttribute(
                MagentoCatalogModelProduct::ENTITY,
                'alternate_product_sku',
                [
                     'group' => 'Discontinue Product Information',
                     'type' => 'text',
                     'backend' => '',
                     'frontend' => '',
                     'label' => 'Alternate Product SKU',
                     'input' => 'text',
                     'class' => '',
                     'source' => '',
                     'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
                     'visible' => true,
                     'required' => false,
                     'user_defined' => false,
                     'default' => '',
                     'searchable' => false,
                     'filterable' => false,
                     'comparable' => false,
                     'visible_on_front' => true,
                     'used_in_product_listing' => true,
                     'unique' => false,
                     'apply_to' => ''
                ]
            );
        }
    }

    Create a Observer with catalog_product_save _before in adminhtml/events.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="catalog_product_save_before">
            <observer name="discontinue_product_save_after" instance="MagemonkeysDiscontinueObserverProductsavebefore" />
        </event>
    </config>

    Add Observer file and place the below code in it.

    <?php
    
    namespace MagemonkeysDiscontinueObserver;
    
    use MagentoFrameworkEventObserverInterface;
    use MagentoCatalogApiProductRepositoryInterface;
    use MagentoFrameworkUrl;
    use MagentoFrameworkExceptionNoSuchEntityException;
    
    class Productsavebefore implements ObserverInterface
    {    
    
        /**
         * @var ProductRepositoryInterface
         */
        protected $productRepository;
    
        protected $_url;
     
        /**
         * @param ProductRepositoryInterface $productRepository
         */
        public function __construct(
            ProductRepositoryInterface $productRepository,
            Url $url
        ) {
            $this->productRepository = $productRepository;
            $this->_url = $url;
        }
    
        public function execute(MagentoFrameworkEventObserver $observer)
        {
            $_product = $observer->getProduct();  // you will get product object
    
            $sku = $_product->getAlternateProductSku();
    
            if($sku != '')
            {
                try
                {
                    $alternateProduct = $this->productRepository->get($sku);
                }
                catch (NoSuchEntityException $noSuchEntityException) {
                    $productUrl = null;
                }
    
                if($alternateProduct)
                {
    
                    $product = $this->productRepository->getById($alternateProduct->getId(), false);
                    //$productUrl = $product->setStoreId(1)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
                    $productUrl = $product->getUrlKey();
                    $_product->setData('alternate_product_url', $productUrl);
                    
                }
            }
            else
            {
                $productUrl = null;
                $_product->setData('alternate_product_url', $productUrl);
            }
    
        }   
    }

    Create a plugin with aroundGetProduyctCollection to remove the discontinue product from whole site. Please add the below code in that plugin.

    <?php
    
    namespace MagemonkeysDiscontinuePluginMagentoCatalogModel;
    
    class Layer
    {
    
        public function aroundGetProductCollection(
            MagentoCatalogModelLayer $subject,
            Closure $proceed
        ) {
            $collection = $proceed();
            $collection->addAttributeToSelect('*')->addAttributeToFilter(array(
                array(
                    'attribute' => 'discontinue',
                    'null' => true),
                array(
                    'attribute' => 'discontinue',
                    'eq' => '0')
            ));
            return $collection;
        }
    
    
    }
    
    

     

    Bugs after upgrading Magento store? We got you covered

    To leverage Magento platform benefits, Magento store owners regularly upgrade their stores in latest version.

    But, it’s been observed that they faces some bugs in their eCommerce store after the upgradation process.

    Below are the reasons for same

    – Not installing Magento by following the standard practice
    – Not installing Magento version completely.
    – Server combability issues
    – Many more.

    Solving these bugs is necessary because it’s like having an expensive car with broken wheel.

    You should leverage the operational smoothness by solving those bugs.

    To solve this problem, Mage monkeys comes with a solution.

    We offer Magento upgrade specialists who helps in solving each and every bugs from your system.

    If you feel there is no bug in your system, then our developers will offer you a tech audit to find problems with your system who is effecting your digital sales silently. Hire an upgrade specialist to overcome such bugs today,

    Magento 2 : How to set custom product price while displaying at front end?

    If you want to change the main price with a custom price attribute. You can use the below method

    1. Create file di.xml on app/code/Magemonkeys/CustomPrice/etc/frontend

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="MagentoCatalogModelProduct">
            <plugin name="change_product" type="MagemonkeysCustomPricePluginModelProduct" sortOrder="1" />
        </type>
    </config>

    2. Create file Product.php on app/code/Magemonkeys/CustomPrice/Plugin/Model

    <?php
    namespace MagemonkeysCustomPricePluginModel;
     
    class Product
    {
        public function afterGetPrice(MagentoCatalogModelProduct $subject, $result)
        {        
            $result = $subject->getCustomPrice(); // you can set custom attribute price
            return $result;
        }
    }

     

    Magento 2: Create custom GraphQL data

    We can create custom api for GraphQL.

    As per the app/code/ standard, our module name is Magemonkeys/Customgraphql

    We can create schema.graphqls file in module etc folder.

    Below is the code for that file.

    type Query
    {
        CustomGraphql (
            username: String @doc(description: "Email of the customer")
            password: String @doc(description: "Password")
            fieldtype: String @doc(description: "Field Type")
        ): CustomGraphqlOutput @resolver(class: "Magemonkeys\Customgraphql\Model\Resolver\ResolverGraphql") @doc(description:"Customgraphql Datapassing")
    }
    type CustomGraphqlOutput
    {
        username: String
        password: String
        fieldtype: String
    }

    After that we need to create Model Resolver file.

    Create file ResolverGraphql.php in this location of module
    Magemonkeys/Customgraphql/Model/Resolver/

    Below is the code for that file.

    <?php
    namespace MagemonkeysCustomgraphqlModelResolver;
    
    use MagentoFrameworkGraphQlConfigElementField;
    use MagentoFrameworkGraphQlExceptionGraphQlInputException;
    use MagentoFrameworkGraphQlQueryResolverInterface;
    use MagentoFrameworkGraphQlSchemaTypeResolveInfo;
    use MagentoFrameworkExceptionNoSuchEntityException;
    use MagentoFrameworkGraphQlExceptionGraphQlNoSuchEntityException;
    
    class ResolverGraphql implements ResolverInterface
    {
        public function resolve(
            Field $field,
            $context,
            ResolveInfo $info,
            array $value = null,
            array $args = null)
        {
            if (!isset($args['username']) || !isset($args['password']) || !isset($args['fieldtype'])||
                empty($args['username']) || empty($args['password']) || empty($args['fieldtype']))
            {
                throw new GraphQlInputException(__('Invalid arguments list please check.'));
            }
            $output = [];
            $output['username'] = $args['username'];
            $output['password'] = $args['password'];
            $output['fieldtype'] = $args['fieldtype'];
          
            return $output ;
        }
    }

    After that you can check API in Postman API tool.

    That’s it.

     

    Magento 2: Change customer group before saving the customer using plugin

    Create di.xml file your module and add the below code
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="MagentoCustomerApiCustomerRepositoryInterface">
            <plugin name="save_customer_group" type="VendorModulePluginMagentoCustomerApiCustomerRepositoryPlugin" />
        </type>
    </config>
    Create plugin file under this directory Vendor/Module/Plugin/Magento/Customer/Api/CustomerRepositoryPlugin.php
    <?php
    namespace VendorModulePluginMagentoCustomerApi;
    
    use MagentoCustomerApiDataCustomerInterface;
    use MagentoCustomerApiCustomerRepositoryInterface;
    use MagentoFrameworkControllerResultFactory;
    use MagentoFrameworkUrlInterface;
    use MagentoFrameworkExceptionLocalizedException;
    
    class CustomerRepositoryPlugin
    {
        /**
         * @var Data
         */
        private $helper;
    
        /**
         * Constructor of class.
         *
         * @param Data $helper
         */
        public function __construct(
            MagentoFrameworkAppRequestInterface $request,
            MagentoCustomerModelResourceModelGroupCollectionFactory $groupCollection,
            MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
            MagentoCustomerApiGroupRepositoryInterface $groupRepository,
            MagentoFrameworkMessageManagerInterface $messageManager,
            ResultFactory $resultFactory,
            UrlInterface $url
        ) {
            $this->request = $request;
            $this->groupCollection = $groupCollection;
            $this->scopeConfig = $scopeConfig;
            $this->groupRepository = $groupRepository;
            $this->messageManager = $messageManager;
            $this->resultFactory = $resultFactory;
            $this->url = $url;
        }
    
        /**
         *
         * @param CustomerRepositoryInterface $subject
         * @param CustomerInterface $customer
         * @return array
         */
        public function beforeSave(CustomerRepositoryInterface $subject, CustomerInterface $customer, $passwordHash = null)
        {
            $groupid = 5; //change customer group id here
            $customer->setGroupId($groupId);
            return [$customer, $passwordHash];
        }
    }