Tag Archives: Magento 2

Magento 2 Custom Query

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

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

$sql = "Select * FROM customer_entity" ;
$result = $connection->fetchAll($sql); 
print_r($result);
?>
Share

Magento 2 External PHP Script

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

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

$sql = "Select * FROM customer_entity" ;
$result = $connection->fetchAll($sql); 
print_r($result);
?>
Share

Install Magento 2 Commandline Linux CentOS

Follow the below steps to install Magento 2 with sample-data.

Make sure that httpd / Nginx,PHP,MySQL and Composer are installed in the server by issuing the following commands

httpd -v / nginx -v
php -v
composer -v
mysql

Change the directory to documet root

cd /var/www/html/

Download Magento and issue the following commands

sudo git clone https://github.com/magento/magento2.git

cd magento2

sudo git checkout tags/2.2.1 -b 2.2.1

chown -R httpd:www /var/www/html/magento2/

sudo chmod -R 775 /var/www/html/magento2/

chmod 777 var/ generated/ app/etc/ pub/

composer install

composer update

composer config repositories.0 composer https://repo.magento.com

php bin/magento setup:install --base-url=http://127.0.0.1/magento2/ --backend-frontname=admin \
--db-host=127.0.0.1 --db-name=mage2 --db-user=root --db-password=Root@123 \
--admin-firstname=Magento --admin-lastname=User --admin-email=user@example.com \
--admin-user=admin --admin-password=admin17 --language=en_US \
--currency=USD --timezone=America/Chicago --use-rewrites=1

php bin/magento sampledata:deploy

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy

Now check the site http://127.0.0.1/magento2/ .

Share

Magento 2 Product Attribute Customer Groups

Creating module PHPCodez_Customattribute to add a new attribute to list all the customer groups including ‘NOT LOGGED IN’.

1) Declare the module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PHPCodez_Customattribute" setup_version="0.0.1" />
</config>

2) Register the module. Continue reading Magento 2 Product Attribute Customer Groups

Share

Custom Fields Multiselect Product Add Edit Pages Magento 2

This is a quite important demand when you want to use extra information on your pages which the default system does not include.

You can add custom fields with the help of UI Component.

Follow the below steps to create the module ‘PHPCodez_Customfield’

1) Declare a module.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PHPCodez_Customfield" setup_version="0.0.1" />
</config>

2) Register a module. Continue reading Custom Fields Multiselect Product Add Edit Pages Magento 2

Share

Custom Fields Product Add Edit Pages Magento 2

This is a quite important demand when you want to use extra information on your pages which the default system does not include.

You can add custom fields with the help of UI Component.

Follow the below steps to create the module ‘PHPCodez_Customfield’

1) Declare a module.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PHPCodez_Customfield" setup_version="0.0.1" />
</config>

2) Register a module. Continue reading Custom Fields Product Add Edit Pages Magento 2

Share

Magento 2 Allow NULL Value Multiselect System Configuration

It can be achieved bu adding the <can_be_empty>1</can_be_empty> tag in system.xml file

Ex:

<field id="hideprice" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Hide Price For</label>
<source_model>PHPCodez\Permission\Model\Config\Source\Group\Multiselect</source_model>
<can_be_empty>1</can_be_empty>
</field>
Share

Magento 2 Unable To Unselect All Multiselect Values

Add the tag <can_be_empty>1</can_be_empty> with the field in system.xml file and now you should be enable to Unselect the multi select values.

Ex:

<field id="hideprice" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Hide Price For</label>
<source_model>PHPCodez\Permission\Model\Config\Source\Group\Multiselect</source_model>
<can_be_empty>1</can_be_empty>
</field>
Share

Magento 2 Hide Price

Basically we have to override class ‘Magento\Catalog\Pricing\Render\FinalPriceBox ‘ to achieve this. Here I am creating an extension ‘PHPCodez_Hideprice’ that will allow you to hide price from certain user groups and diaplay a text inplace of price.

1) Declare a module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PHPCodez_Hideprice" setup_version="0.0.1" />
</config>

2) Register a module. Continue reading Magento 2 Hide Price

Share

Magento 2 Source Model All Customer Groups

You can use the source model <source_model>Magento\Customer\Model\Config\Source\Group\Multiselect</source_model> to get the customer groups.

But its does not return the group ‘NOT LOGGED IN’ .so I have created new model to have ‘NOT LOGGED IN’ in the list.

<source_model>PHPCodez\Hideprice\Model\Config\Source\Group\Multiselect</source_model>

<?php
namespace PHPCodez\Hideprice\Model\Config\Source\Group;
use \Magento\Customer\Model\ResourceModel\Group\Collection;

class Multiselect implements \Magento\Framework\Option\ArrayInterface {

protected $_customerGroup;

protected $_options;

public function __construct( Collection $customerGroup ) {
$this->_customerGroup = $customerGroup;
}

public function toOptionArray() {
if (!$this->_options) {
$this->_options = $this->_customerGroup->toOptionArray();
}
return $this->_options;
}
}
Share