All posts by Pramod T P

Magento 2 Create Yes No Attribute Programmatically

Here i am creating upgradeData script that will add custom product attribute with yes and no options.

<?php
 namespace PHPCodez\Subscription\Setup;

use Magento\Eav\Setup\EavSetup;
 use Magento\Eav\Setup\EavSetupFactory;
 use Magento\Framework\Setup\UpgradeDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;

class upgradeData implements UpgradeDataInterface {
 private $eavSetupFactory;
 protected $logger;

public function __construct(EavSetupFactory $eavSetupFactory,\Psr\Log\LoggerInterface $logger) {
 $this->eavSetupFactory = $eavSetupFactory;
 $this->logger = $logger;
 }

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
 if (version_compare($context->getVersion(), '0.0.8', '<=')) {
 
  $this->logger->info("System - upgrading module - removing product attribute - subscripion_product");
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->addAttribute(
 \Magento\Catalog\Model\Product::ENTITY,
 'subscripion_product',[
 'type' => 'text',
 'backend' => '',
 'frontend' => '',
 'label' => 'Enable Subscription',
 'input' => 'select',
 'class' => '',
 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
 'visible' => true,
 'required' => true,
 'user_defined' => false,
 'default' => '',
 'searchable' => false,
 'filterable' => false,
 'comparable' => false,
 'visible_on_front' => false,
 'used_in_product_listing' => true,
 'unique' => false,
 'apply_to' => ''
 ]
 );
 
 
 }
 }
 }
Share

Add Navigation Menu Magento 2

You can add a new non catgory navigation menu as follows

1) Add the below code in layout XML

File : app\code\PHPcodez\Subscription\view\frontend\layout\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>
 <referenceContainer name="catalog.topnav">
 <block class="PHPCodez\Subscription\Block\Link" name="your.link">
 <arguments>
 <argument name="label" xsi:type="string">Subscription</argument>
 <argument name="path" xsi:type="string">subscription</argument>
 </arguments>
 </block>
 </referenceContainer>
 </body>
</page>

2) Define the block Link.php

File : app\code\PHPcodez\Subscription\Block\Link.php

<?php
 namespace PHPCodez\Subscription\Block;
 
 class Link extends \Magento\Framework\View\Element\Html\Link {

protected function _toHtml() {
 if (false != $this->getTemplate()) {
 return parent::_toHtml();
 }
 return '<li class="level0 "><a ' . $this->getLinkAttributes() . ' class="level-top" >' . $this->escapeHtml($this->getLabel()) . '</a></li>';
 }
 }
Share

Add Top Link Magento 2

You can add a new top link as follows

1) Add the below code in layout XML

File : app\code\PHPcodez\Subscription\view\frontend\layout\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="header.links">
 <block class="PHPCodez\Subscription\Block\Link" name="add-new-header-link">
 <arguments>
 <argument name="label" xsi:type="string" translate="true">New Link</argument>
 <argument name="path" xsi:type="string" translate="true">new-link</argument>
 </arguments>
 </block>
 </referenceBlock>
 </body>
</page>

2) Define the block Link.php

File : app\code\PHPcodez\Subscription\Block\Link.php

<?php
 namespace PHPCodez\Subscription\Block;
class Link extends \Magento\Framework\View\Element\Html\Link {

protected function _toHtml() {
 if (false != $this->getTemplate()) {
 return parent::_toHtml();
 }
 return '<li class="level0 "><a ' . $this->getLinkAttributes() . ' class="level-top" >' . $this->escapeHtml($this->getLabel()) . '</a></li>';
 }
 }
Share

Add Link Navigation Menu Magento 2

You can add a non-category link to the navigation menu by adding the below lines in layout xml file.

<referenceContainer name="navigation.sections">
 <block class="Magento\Framework\View\Element\Html\Links" name="mylink">
 <arguments>
 <argument name="label" xsi:type="string">Mylink</argument>
 <argument name="path" xsi:type="string">mypath</argument>
 <argument name="css_class" xsi:type="string">mycss</argument>
 </arguments>
 </block>
</referenceContainer>
Share

Magento 2 Setup Multiple Websites

Follow the below steps to configure multiple magento websites

1) Create websites, stores, and store views in the Magento Admin

2) Create Apache virtual hosts.

Edit the file E:\xampp\apache\conf\extra\httpd-vhosts.conf

<VirtualHost *:80>
 ServerName phpcode.com
 DocumentRoot E:\xampp\htdocs\phpcode\pub
 </VirtualHost>

<VirtualHost *:80>
 ServerName in.phpcode.com
 DocumentRoot E:\xampp\htdocs\phpcode\pub
 SetEnv MAGE_RUN_CODE "india"
 SetEnv MAGE_RUN_TYPE "website"
 </VirtualHost>

3) Restart Apache.

4) Edit your vhost file(In my system its C:\Windows\System32\drivers\etc\hosts)

127.0.0.1 phpcode.com
127.0.0.1 in.phpcode.com

5) Verify the sites accessing the following URLs

phpcode.com
in.phpcode.com

Share

Magento 2 cURL

Following code get content from an external site using cURL.

<?php
 namespace PHPCodez\Layout\Block;
 
 class Footer extends \Magento\Framework\View\Element\Template {
 
 protected $_httpClientFactory;
 
 public function __construct(\Magento\Framework\View\Element\Template\Context $context,
 \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory) {
 $this->_httpClientFactory = $httpClientFactory;
 parent::__construct($context);
 }

public function getFooterContent() {
 $url = "http://127.0.0.1:81/content/footer.php";
 $client = $this->_httpClientFactory->create();
 $client->setUri($url);
 return $response = $client->request(\Zend_Http_Client::POST)->getBody(); 
 }
 }

For more option you can check vendor\magento\framework\HTTP\Client\Curl.php

addHeader()
setCredentials()
addCookie()
get() // for get request
makeRequest() // for make request
setOption() // set curl options
Share

Magento 2 Remove Footer Links

You can remove Footer Links by adding the below line to layout 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="footer_links" remove="true"/>
<referenceBlock name="cms_footer_links_container" remove="true" />
 </body>
</page>
Share