我有一个名为ProductUpdateInterface的接口,代码如下
/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/应用程序/代码/管理员网络套件PQ/型号/产品更新.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">123</parameter>
<parameter name="price" force="false">12.5</parameter>
<parameter name="quantity" force="false">20</parameter>
</data>
</route>
</routes>
现在当我通过 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": 123,
"price": 12.5,
"quantity": 20
}
现在,当我执行这些(实际上这些是Magento 2模块中的代码)时,我得到以下错误
致命错误:未捕获的错误:无法示例化接口AmdNetsuitePQ\NetsuitePQ\API\ProductUpdateInterface
请帮助解决这个错误。
1条答案
按热度按时间6pp0gazn1#
你需要告诉magento一个给定接口的默认类是什么,以便能够使用你的webapi定义中的接口。
AdmNetsuitePQ/NetsuitePQ/etc/di.xml