PHP SOLUTIONS

This is the blog for getting Idea regarding PHP, Magento, jQuery and JavaScript for Customization.

Showing posts with label Magento2. Show all posts
Showing posts with label Magento2. Show all posts

Thursday, 9 November 2017

Magento 2 : Apply Custom Discount to cart

Hi All,

     Offering some special discount to customers is a good way to attract more customers & indirectly increase sales of site !

    In Magento 2,  there are so many ways available for apply some special discounts after adding product to cart, but here some special things which allowed you for add dynamic discount product wise !

  For that need to follow some steps for make it work,

  A) Create "spe_dis" field  with "Decimal" data type in "quote_item" tabel.

  B) Override Add.php from core directory form vendor\magento\module-checkout\Controller\Cart\Add.php

                                                                         OR

  B) For Testing update same file in vendor\magento\module-checkout\Controller\Cart\Add.php

      After, this line $this->cart->save();

      Add below code,

     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
     $connection = $resource->getConnection();

     $data=$connection->fetchAll('SELECT item_id FROM quote_item WHERE quote_id='.$this->cart->getQuote()->getId());
     if(isset($_REQUEST['spe_dis']) and $_REQUEST['spe_dis'] > 1){
            $fianlsf = 0;
            foreach($data as $dats){$fianlsf = $dats['item_id'];}
            $sql = "Update quote_item  Set spe_dis='".$_REQUEST['spe_dis']."' where item_id = ".$fianlsf."";
            $connection->query($sql);
      }


     Note :- Above code save special discount value to DB.

   C) Create a hidden text field inside product page form

      <input type="hidden" id="spe_dis" value="0" />

      set special discount to this field which want to apply product wise.

  D) Finally, Unzip files in app/code 

       Note :- Download Zip

     Result would be like ->

This is working for me in Magento 2.1.8 and 2.2.0 also !

Monday, 8 May 2017

Magento2 : Create a custom backend theme

Hello Friends,

     Here is the code for create a custom theme for the backend in magento 2 !
   
     Magento2 provide Backend as a admin theme !

     Magento 2.X is a quite different process as compare of Magento 1.X, there are few easy steps for it !

Step 1 : Create a theme directory at below location,

app\design\adminhtml\Magento\custom

Step 2 : Now, Create 3 files inside custom directory,

A) composer (This is JSON file)

B) registration (This is PHP file)

C) theme (This is XML file)

Now, Copy/Paste below code in side related files,

Step A : composer (This is JSON file)
             
                {
                    "name": "magento/theme-adminhtml-custom",
                    "description": "N/A",
                    "require": {
                        "php": "~5.6.0|7.0.2|~7.0.6",
                        "magento/theme-adminhtml-backend": "~100.0",
                        "magento/framework": "~100.0"
                    },
                    "type": "magento2-theme",
                    "version": "1.0.0",
                    "license": [
                        "OSL-3.0",
                        "AFL-3.0"
                    ],
                    "autoload": {
                        "files": [
                            "registration.php"
                        ]
                    }
                }

   Step B : registration (This is PHP file)

<?php
                /**
                 * Copyright © 2016 Magento. All rights reserved.
                 * See COPYING.txt for license details.
                 */
             
                \Magento\Framework\Component\ComponentRegistrar::register(
                    \Magento\Framework\Component\ComponentRegistrar::THEME,
                    'adminhtml/Magento/custom',
                    __DIR__
                );

   Step C : theme (This is XML file)

                <theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
                    <title>Magento 2 new backend</title>
                    <parent>Magento/backend</parent>
                </theme>


Step 6 : Now, just run upgrade & deploy commands

       A) php bin/magento setup:upgrade

       B) php bin/magento setup:static-content:deploy
 
       you can see you custom theme name as Magento/custom while deploying !

Magento 2 : Create a custom frontend theme

Hello Friends,

     Here is the code for how to create a custom theme for the front end in magento 2 !

     Magento 2.X is a quite different process as compare of Magento 1.X, there are few easy steps for it !

Step 1 : Create a theme directory at below location,

app\design\frontend\Magento\custom

Step 2 : Now, Create 3 files inside custom directory,

A) composer (This is JSON file)

B) registration (This is PHP file)

C) theme (This is XML file)

Now, Copy/Paste below code in side related files,

Step A : composer (This is JSON file)

{
                    "name": "magento/custom-module-theme",
                    "description": "N/A",
                    "require": {
                        "php": "~5.6.0|7.0.2|~7.0.6",
                        "magento/theme-frontend-luma": "~100.0",
                        "magento/framework": "~100.0"
                    },
                    "type": "magento2-theme",
                    "version": "1.0.0",
                    "license": [
                        "OSL-3.0",
                        "AFL-3.0"
                    ],
                    "autoload": {
                        "files": [ "registration.php" ]
                    }
       }

   Step B : registration (This is PHP file)

<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
           
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::THEME,
'frontend/Magento/custom',
                __DIR__
);

   Step C : theme (This is XML file)

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
<title>Magento Custom Theme</title>
<parent>Magento/luma</parent>
<media>
<preview_image>media/preview.jpg</preview_image>
</media>
</theme>


Step 3 : Create directory at below mentioned location,

app\design\frontend\Magento\custom\Magento_Theme\layout

Step 4 : Inside \Magento_Theme\layout create file called default.xml

/* COPY & PASTE this code inside default.xml */

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="logo">
<arguments>
<argument name="logo_file" xsi:type="string">images/logo.png</argument>
</arguments>
</referenceBlock>
<remove name="report.bugs"/>
</body>
</page>

Step 5 : Create a media directory inside Magento\custom & upload preview.jpg file inside it !

 

Step 6 : Now, just run upgrade & deploy commands

       A) php bin/magento setup:upgrade

       B) php bin/magento setup:static-content:deploy

Wednesday, 5 October 2016

Magento 2 : Method to use SQL query

Hello Friends,

         Magento 2.X is use Resource Connection Method for fetch data from the Database, Which is little bit different from the Magento 1.X

        I have used code like below, and I hope it will helps to you also,

        Here, I show the code for SELECT query for magento2

 1)  $this->_resources= \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\ResourceConnection');
 2)  $connection = $this->_resources->getConnection();
 3)  $Table_Name = $this->_resources->getTableName('TABLE NAME');
4) $data = $connection->fetchAll('SELECT * FROM '.$Table_Name.' WHERE
Field_Name=Value');

       you just copy & paste this in related *.phtml or Controller files !

       If you want to use Insert or Update then just replace Select query, and it will work !

       For, Insert replace line number (4)

4) $data = $connection->fetchAll('INSERT into '.$Table_Name.'
VALUES ('value1','value2');

       For, Update replace line number (4)

4) $data = $connection->fetchAll('UPDATE '.$Table_Name.'
SET column1=value1,column2=value2 WHERE id=1;

This is working fine for me in Magento 2.0 !
       

Thursday, 7 April 2016

Magento 2 : CSS and Javascript no loading after installation

    This is an initial issue with magento 2, After Installation Front-End and Back-End CSS / JS do not work.

    Using following steps, it is getting work easily for magento 2 !

    This is creating frontend / adminhtml directory to the "pub/static/" directory.

Using Command Prompt(CMD) set CSS & JS for Magento2

Step 1 : Run CMD

Step 2 : Goto Magento installation directory using

             CD wamp/www/[Magento dir.]

Step 3 : Run

             php bin/magento setup:static-content:deploy

             If you are getting problem of

            "php is not recognized as an internal or external command" [Click here...]

Step 4 : Run

             php bin/magento indexer:reindex

Step 5 : Delete Cache from

             var/cache/[delete all dir.]

It's getting work for Front-end & Back-end !