Tag Archives: Customer

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

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

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;
}
}

Current Customer Group Magento 2

<?php
namespace PHPCodez\Disableaddtocart\Helper;

use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Customer\Model\Session;

class Data extends AbstractHelper {

protected $_customerSession;

public function __construct(Session $_customerSession) {
$this->_customerSession = $_customerSession;
}

public function getCustomerGroup(){
return $this->_customerSession->isLoggedIn()?$this->_customerSession->getCustomer()->getGroupId():0;
}


}

Magento 2 Disable Add To Cart Customer Group

I am creating an extension that will allow you to hide/disable add to cart button for certain customer groups.

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_Disableaddtocart" setup_version="0.0.1" />
</config>

2) Register a module. Continue reading Magento 2 Disable Add To Cart Customer Group

Magento 2 API Issue Offline Refund

The following code will issue offline refund and will return creditmemo id.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "apiuser", "password" => "apiuserpwd");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);

$requestUrl =$magentoURL.'index.php/rest/V1/order/16/refund/';

$refundData = [
 'items' => array([ "order_item_id" =>28,"qty"=> 2],["order_item_id" =>29,"qty"=> 1]),
 "notify"=>true,
 "arguments"=>["shipping_amount" => 0,"adjustment_positive" => 0,"adjustment_negative" =>0,"extension_attributes" =>[ "return_to_stock_items" =>array(28,29)]]
 ];

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($refundData));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
 ?>

Magento 2 API Create Shipment

The following code will create shipment for a given order id and return the shipment id.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "apiuser", "password" => "apiuserpwd");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);
 
 $requestUrl =$magentoURL.'index.php/rest/V1/order/16/ship/';
 
 $shipingData = [
 'items' => array([ "order_item_id" =>28,"qty"=> 2],["order_item_id" =>29,"qty"=> 2]),
 "notify"=>true,
 "appendComment"=>false
 ];
 
 $ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($shipingData));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
?>

Magento 2 API Invoice Details

The following code will display the invoice details based on invoice id.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "apiuser", "password" => "apiuserpwd");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);

$requestUrl =$magentoURL.'index.php/rest/V1/order/17/invoice';
 
 $invoiceData = [
 'capture' => true,
 'notify' => true
 ];

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($invoiceData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
?>

Magento 2 API Create Invoice

The following code will create invoice for a given order id.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "apiuser", "password" => "apiuserpwd");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);

$requestUrl =$magentoURL.'index.php/rest/V1/order/17/invoice';
 
 $invoiceData = [
 'capture' => true,
 'notify' => true
 ];

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($invoiceData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
?>