Tag Archives: eCommerce

Magento 2 API Create Customer

Here I am creating a customer using REST API

<?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);

$customerData = [
 'customer' => [
 "email" => 'Test.User'.time().'@phpcodez.com',
 "firstname" => "Brand",
 "lastname" => "Muscle",
 "storeId" => 1,
 "websiteId" => 1
 ],
 "password" => "PHPCodez1&"
 ];

$ch = curl_init($magentoURL."index.php/rest/V1/customers");
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

$result = curl_exec($ch);

$result = json_decode($result, 1);
 echo '<pre>';print_r($result);
?>
Share

Magento 2 REST API

Magento 2 API framework allows developers to create new services for communicating with Magento 2 stores.

Here I will use this Token based REST API to get all the customers from a Magento 2 store. So before each API call we should get the token from Magento and we should pass this token in the header of every request.

Also to achive this you need to create a new admin user with necessary access to the system.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "APIUSER", "password" => "APIUSERWD");
 $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);

//decoding generated token and saving it in a variable
 $token= json_decode($token);

//******************************************//

//Using above token into header
 $headers = array("Authorization: Bearer ".$token);

//API URL to get all Magento 2 modules
 $requestUrl =$magentoURL.'index.php/rest/V1/customers/search?searchCriteria[sortOrders][0][field]=email&searchCriteria[sortOrders][0][direction]=asc';

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $result = curl_exec($ch);

//decoding result
 $result= json_decode($result);

//printing result
 echo "<pre>";print_r($result);

?>
Share

Cannot find save handler ‘memcached’ Magento 2

I have faced the same issue when memcached was configured for Magento 2. The system was throwing the below error

Warning: ini_set(): Cannot find save handler ‘memcached’ in E:\xampp\htdocs\mage2\vendor\magento\framework\Session\SessionManager.php on line 570

In my case it was due to save_handler value was not udated in php.ini file.

So Make sure that save_handler is modified in php.ini file as well.

Please follow Configure Memcached Magento 2

Share

Configure Memcached Magento 2

Memcached is an open-source memory object caching system that web sites can use to help accelerate page load times. Memcached works by caching in RAM frequently accessed data, such as the results of API calls, database calls, and more.

Click here to undersatnd how to configure memcached in XAMPP – WIndows 10

To enable memcached for Magento 2, you need to change some settings in the env.php file. To do this, follow these steps:

1) Open the file app/etc/env.php

2) Locate the following section in the env.php file:
‘session’ =>
array (
‘save’ => ‘files’,
),
Modify this section as follows:

‘session’ =>
array (
‘save’ => ‘memcached’,
‘save_path’ => ‘127.0.0.1:11211’
),

3)Save your changes to the env.php file.

4) Edit the php ini.file

Seacch for the string in php.ini file ‘session.save_handler=files’ and update it to ‘session.save_handler=memcached’

Now memcached is enabled for you magento site

Share

Remove header and footer Magento 2

You can remove header and footer content from all the Magento pages by adding the following code in default layout xml file.

<?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="header.container" remove="true" />
 <referenceContainer name="footer" remove="true" /> 
 </body>
</page>

For example in case of the theme Luma, the file need to be edited is vendor\magento\theme-frontend-luma\Magento_Theme\layout\default.xml

Check out the extension to that removes the header and footer content.

Share