cancel
Showing results for 
Search instead for 
Did you mean: 

M2 API Endpoint for 'Marketing -> Search Terms'

SOLVED

M2 API Endpoint for 'Marketing -> Search Terms'

Hi,

I have been trying to find the API endpoint for managing 'Search Term' entries. I would like to add/update entries for searches using item SKU codes, this would allow me to redirect the search straight to the product page instead of the search results.

 

https://i.imgsafe.org/3557cd9558.png - Image shows the Search Term module in the admin

http://i.imgsafe.org/447f96f78d.png - Image shows the api permissions for the 'Search Term' module i am reffering to


Many thanks in advance!
Dan

1 ACCEPTED SOLUTION

Accepted Solutions

Re: M2 API Endpoint for 'Marketing -> Search Terms'

Hi @ThisIsRuddy

 

The Magento\Search module which is responsible for Search Terms management in Magento Admin. The  only Web API available out of box is search /V1/search which will give you list of results.

 

It is possible to create new API for search terms to add and update them.

Create new webapi.xml file with new endpoint declaration:

 

 

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/search/:sku/:storeId/term" method="POST">
<service class="Magento\Search\Api\SearchTermsManagementInterface" method="addSkuSearchTermRedirect"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>
</routes>

Create new Vendor\Search\Api\SearchTermsManagementInterface interface

 

namespace Vendor\Search\Api;

interface SearchTermsManagementInterface
{
    /**
     * @param string $sku
     * @param int $storeId
     * @return int
     */
    public function addSkuSearchTermRedirect($sku, $storeId);
}

Create class which implements new interface:

 

 

namespace Vendor\Search\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Search\Model\QueryFactory;
use Magento\Search\Api\SearchTermsManagementInterface;

/**
* Search extended implementation for WebApi
*/
class SearchTermsManagement implements SearchTermsManagementInterface
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var QueryFactory
*/
private $queryFactory;

/**
* SearchTermsManagement constructor.
* @param ProductRepositoryInterface $productRepositoryFactory
* @param QueryFactory $queryFactory
*/
public function __construct(
ProductRepositoryInterface $productRepositoryFactory,
QueryFactory $queryFactory
) {
$this->productRepository = $productRepositoryFactory;
$this->queryFactory = $queryFactory;
}

/**
* @param string $sku
* @param int $storeId
* @return int
*/
public function addSkuSearchTermRedirect($sku, $storeId)
{
/** @var \Magento\Catalog\Model\Product|\Magento\Catalog\Api\Data\ProductInterface $product */
$product = $this->productRepository->get($sku, false, $storeId);

$productUrl = $product->getProductUrl();

/** @var \Magento\Search\Model\Query $query */
$query = $this->queryFactory->create();
$query->setStoreId($storeId);
$query->loadByQueryText($sku);

$query->setQueryText($sku);
$query->setRedirect($productUrl);

$query->save();

return $query->getId();
}
}

 

Make sure to use POST for sending SKU and Store Id. I tested locally and it works, redirects to a product page if I search by SKU.

 

Hope it helps.

 

 

View solution in original post

2 REPLIES 2

Re: M2 API Endpoint for 'Marketing -> Search Terms'

Hi @ThisIsRuddy

 

The Magento\Search module which is responsible for Search Terms management in Magento Admin. The  only Web API available out of box is search /V1/search which will give you list of results.

 

It is possible to create new API for search terms to add and update them.

Create new webapi.xml file with new endpoint declaration:

 

 

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/search/:sku/:storeId/term" method="POST">
<service class="Magento\Search\Api\SearchTermsManagementInterface" method="addSkuSearchTermRedirect"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>
</routes>

Create new Vendor\Search\Api\SearchTermsManagementInterface interface

 

namespace Vendor\Search\Api;

interface SearchTermsManagementInterface
{
    /**
     * @param string $sku
     * @param int $storeId
     * @return int
     */
    public function addSkuSearchTermRedirect($sku, $storeId);
}

Create class which implements new interface:

 

 

namespace Vendor\Search\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Search\Model\QueryFactory;
use Magento\Search\Api\SearchTermsManagementInterface;

/**
* Search extended implementation for WebApi
*/
class SearchTermsManagement implements SearchTermsManagementInterface
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var QueryFactory
*/
private $queryFactory;

/**
* SearchTermsManagement constructor.
* @param ProductRepositoryInterface $productRepositoryFactory
* @param QueryFactory $queryFactory
*/
public function __construct(
ProductRepositoryInterface $productRepositoryFactory,
QueryFactory $queryFactory
) {
$this->productRepository = $productRepositoryFactory;
$this->queryFactory = $queryFactory;
}

/**
* @param string $sku
* @param int $storeId
* @return int
*/
public function addSkuSearchTermRedirect($sku, $storeId)
{
/** @var \Magento\Catalog\Model\Product|\Magento\Catalog\Api\Data\ProductInterface $product */
$product = $this->productRepository->get($sku, false, $storeId);

$productUrl = $product->getProductUrl();

/** @var \Magento\Search\Model\Query $query */
$query = $this->queryFactory->create();
$query->setStoreId($storeId);
$query->loadByQueryText($sku);

$query->setQueryText($sku);
$query->setRedirect($productUrl);

$query->save();

return $query->getId();
}
}

 

Make sure to use POST for sending SKU and Store Id. I tested locally and it works, redirects to a product page if I search by SKU.

 

Hope it helps.

 

 

Re: M2 API Endpoint for 'Marketing -> Search Terms'

Max you are an absolute legend! I was not expecting such a comprehensive and complete solution especially over the weekend, so thank you very much 😊