0

I have created wishlist rest web api

from swagger this is something that has been generated as url

 localhost/m233/rest/all/V1/wishlistapi/1/2

i want to know that , Why "all" parameter gets added in rest web api ?

Here is my webapi.xml

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
   <route url="/V1/wishlistapi/:customerId/:productId" method="POST">
       <service class="Host\WishlistApi\Api\CustomerGreetingInterface" method="addWishlistForCustomer"/>
       <resources>
          <resource ref="anonymous" />
       </resources>
   </route>
   <route url="/V1/wishlistapi/:customerId" method="GET">
        <service class="Host\WishlistApi\Api\CustomerGreetingInterface" method="getWishlistForCustomer"/>
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>
</routes>

Model file

<?php

namespace Host\WishlistApi\Model;

use Host\WishlistApi\Api\CustomerGreetingInterface;
use Magento\Customer\Model\CustomerRegistry;

class CustomerGreeting implements CustomerGreetingInterface
{

    /**
     * @var CollectionFactory
     */
    protected $_wishlistCollectionFactory;
    /**
     * @var CustomerRegistry
     */
    protected $_wishlistFactory;
    /**
     *@var \Magento\Catalog\Model\Product
     */
    protected $_productload;
    /**
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storemanagerinterface;
    protected $customerRegistry;

    public function __construct(
        CustomerRegistry $customerRegistry,
        \Magento\Catalog\Model\ProductFactory $_productloader,
        \Magento\Store\Model\StoreManagerInterface $storemanagerinterface,
        \Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory $_wishlistCollectionFactory,
        \Magento\Wishlist\Model\WishlistFactory $wishlistFactory

    ) {
        $this->_productloader             = $_productloader;
        $this->_wishlistFactory           = $wishlistFactory;
        $this->_wishlistCollectionFactory = $_wishlistCollectionFactory;
        $this->storemanagerinterface      = $storemanagerinterface;

        $this->customerRegistry = $customerRegistry;
    }

    /**
     * Get customer's name by Customer ID and return greeting message.
     *
     * @api
     * @param int $customerId
     * @return \Magento\Customer\Api\Data\CustomerInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException If customer with the specified ID does not exist.
     * @throws \Magento\Framework\Exception\LocalizedException
     */

    public function addWishlistForCustomer($customerId, $productId)
    {

        if ($productId == null) {
            throw new LocalizedException(__
                ('Invalid product, Please select a valid product'));
        }
        try {
            $product = $this->_productloader->create()->load($productId);
        } catch (NoSuchEntityException $e) {
            $product = null;
        }
        try {
            $wishlist = $this->_wishlistFactory->create();
            $wishlist->loadByCustomerId($customerId);
            $wishlist->addNewItem($productId);
            $wishlist->save();
            return 'ok';
        } catch (NoSuchEntityException $e) {

        }
        return true;

    }

    public function getWishlistForCustomer($customerId)
    {

        if (empty($customerId) || !isset($customerId) || $customerId == "") {
            throw new InputException(__('Id required'));
        } else {

            $collection =
            $this->_wishlistCollectionFactory->create()
                ->addCustomerIdFilter($customerId)->getData();

            $baseurl = $this->storemanagerinterface->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product';

            $wishlistData = [];
            foreach ($collection as $item) {
                $productInfo = $this->_productloader->create()->load($item['product_id']);

                $data = [
                    "wishlist_item_id" => $item['wishlist_item_id'],
                    "wishlist_id"      => $item['wishlist_id'],
                    "product_id"       => $item['product_id'],
                    "store_id"         => $item['store_id'],
                    "added_at"         => $item['added_at'],
                    "description"      => $item['description'],
                    "qty"              => round($item['qty']),
                ];

                $wishlistData[] = $data;
            }
            return $wishlistData;
        }
    }
}


Any thoughts on this ?
Thankyou
1

The syntax for Magento rest endpoint is

http://<magento_host>/rest/<store_code>/<api_url>

Possible values for <store_code> could be one of the following:

  1. Actual assigned store code
  2. default: The default value get assigned when store_code is not mentioned.
  3. all : This specifies that to access all the merchant's stores.

Hope it helps, Thanks

| improve this answer | |
  • How can set if only for default/global in web api ? – devmage Jun 20 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.