0

I have developed a module in my magento 2.2.6 for to create an API that can update the product price and quantity via product id . Please see my code below

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(

    \Magento\Framework\Component\ComponentRegistrar::MODULE,

    'AdmNetsuitePQ_NetsuitePQ',

    __DIR__

);

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/Model/ProductUpdate.php

<?php
namespace AdmNetsuitePQ\NetsuitePQ\Model;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ProductFactory;
use AdmNetsuitePQ\NetsuitePQ\Api\ProductUpdateInterface;

class ProductUpdate implements ProductUpdateInterface
{
    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function update($productId, $price = null, $quantity = null)
    {
        $productId = (int) $productId;
        $product = $this->productRepository->getById($productId);
        if (!$product->getId()) {
            throw new NoSuchEntityException(__('Product with ID %1 not found.', $productId));
        }
        if ($price !== null && $price < 0) {
            throw new InputException(__('Price must be greater than or equal to 0.'));
        }
        if ($quantity !== null && $quantity < 0) {
            throw new InputException(__('Quantity must be greater than or equal to 0.'));
        }
        if ($price !== null) {
            $product->setPrice($price);
        }
        if ($quantity !== null) {
            $product->setQty($quantity);
            $product->setIsQtyDecimal(false);
        }
        $this->productRepository->save($product);
        return true;
    }
}

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/Api/ProductUpdateInterface.php

<?php
namespace AdmNetsuitePQ\NetsuitePQ\Api;

interface ProductUpdateInterface
{
    /**
     * Update product quantity and price
     *
     * @param int $productId
     * @param float|null $price
     * @param float|null $quantity
     * @return bool
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Exception\StateException
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function update($productId, $price = null, $quantity = null);
}

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/etc/module.xml

<?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="AdmNetsuitePQ_NetsuitePQ" setup_version="1.0.0"> </module>

</config>

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/etc/webapi.xml

<?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/product/update" method="POST">
        <service class="AdmNetsuitePQ\NetsuitePQ\Api\ProductUpdateInterface" method="update"/>
        <resources>
            <resource ref="Magento_Catalog::products"/>
        </resources>
        <data>
            <parameter name="productId" force="true">%productId%</parameter>
            <parameter name="price" force="false">%price%</parameter>
            <parameter name="quantity" force="false">%quantity%</parameter>
        </data>
    </route>
</routes>

Now when I am sending data via postman

POST /rest/V1/product/update HTTP/1.1
Host: my_site.com
Content-Type: application/json
Authorization: OAuth oauth_consumer_key="j9q7uilt4trla8hsi5lo1gtbtuh46icr",oauth_token="df4lz1gasp0su25rbwnp225kxfwknehn",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1678733662",oauth_nonce="kfxspsm3a2n",oauth_version="1.0",oauth_signature="sdKx5cO%2BU7fePuKPWfgZy5ZCAks%3D"
Cookie: PHPSESSID=23vl60opom23c1gfqalnh914e9
Content-Length: 65

{
    "productId": 122,
    "price": 0,
    "quantity": 12
}

{ "message": "Invalid type for value: "%productId%". Expected Type: "int"." }

Please help to solve this error. Please check my module code is correct or not.

1 Answer 1

0

You should specify the data type of productId argument as int in your function, like so:

public function update(int $productId, $price = null, $quantity = null)
{
    // logic goes here
}

Also, I should mention that it is good practice to specify data type of function arguments and function return result whenever possible (safely for PHP 7 and up). That is, your function declaration should look like this:

public function update(int $productId, ?float $price = null, ?int $quantity = null): bool
{
    // logic goes here
}

Hopefully this will help, otherwise please let me know so I can provide further assistance.

Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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