Tag Archives: Product

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

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

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

Magento 2 API Add Product To Cart

The following code will add product to a cart/quote for a logged-in customer.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/customer/token";
 $data = array("username" => "Test.User1521623008@phpcodez.com", "password" => "phpcodezpwd");
 $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);

$quoteUrl =$magentoURL.'index.php/rest/V1/carts/mine';

$chQuote = curl_init($quoteUrl);
 curl_setopt($chQuote, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 curl_setopt($chQuote, CURLOPT_RETURNTRANSFER, true);
 $quote = json_decode(curl_exec($chQuote));
 
 $cartData = [
 'cartItem' => [
 "quote_id" => $quote->id,
 "sku" => "152110360696",
 "qty" => 2
 ]
 ];

$requestUrl =$magentoURL.'index.php/rest/V1/carts/mine/items';

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($cartData));
 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);

?>