CommercePeer Brand Logo


Programmatically: Magento 2 Product Entity

Programmatically: How to implement custom Magento SOAP API clients

Step 1: Create a new module

Create a new folder in the app/code/local directory, and inside it, create a new file called Client.php. This file will contain the code for our custom SOAP API client. Client.php


	namespace YourCompany\YourModule\Model;

		use Magento\Framework\Soap\SoapAdapter;
		use Magento\Framework\Soap\SoapClient;

		class Client extends SoapAdapter
		{
			private $client;

			public function __construct(SoapClient $client)
			{
				$this->client = $client;
			}

			public function getProducts()
			{
				$params = array(
					'store' => 'your_store_id',
					'searchCriteria' => array(
						'fulltext' => 'your_search_term'
					)
				);
				$result = $this->client->call('catalogProduct.list', $params);
				return $result;
			}
		}
		

Step 2: Create a di.xml file

Create a new file called di.xml in the app/code/local/YourCompany/YourModule/etc directory. This file will define the dependencies for our module. di.xml


	<?xml version="1.0"?>
	<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
		<preference for="YourCompany\YourModule\Model\Client" type="YourCompany\YourModule\Model\Client"/>
	</config>
	

Step 3: Create a SOAP client instance

To create a SOAP client instance, you need to configure the soap adapter. You can do this by creating a new file called soap.xml in the app/etc/modules directory. soap.xml


	<?xml version="1.0"?>
	<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:soap/etc/config.xsd">
		<soap>
			<adapter>
				<name>your_soap_adapter</name>
				<class>Magento\Framework\Soap\SoapAdapter</class>
				<args>
					<arg name="endpoint">http://your-magento-url.com/soap/?wsdl</arg>
					<arg name="wsdl">http://your-magento-url.com/soap/?wsdl</arg>
				</args>
			</adapter>
		</soap>
	</config>
	

Step 4: Use the client in your code

You can now use the client in your code by instantiating it and calling its methods. For example:


	$client = \Magento\Framework\App\ObjectManager::getInstance()->get('YourCompany\YourModule\Model\Client');
	$result = $client->getProducts();
	

LET’S WORK TOGETHER

We love working with Small Businesses, Retailers, Manufacturers. Our team is here to Help.

→ Schedule Free Consultant Now
Copyright © 2024 CommercePeer. All Rights Reserved.