如何解决Magento 2 API 使用客户 ID 获取客户购物车数据
我正在构建自定义 magento 2 API。我想使用 CUSTOMER_ID 获取客户 CART DATA。我这样做是为了将 magento 数据提供给 android 和 ios 客户设备。
我尝试了以下代码,但它不起作用。
$params = $this->request->getPostValue();
$customerId = $params["customer_id"];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerData = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);
//Code to get customer cart data
$quote = $this->quoteFactory->create();
$customerQuote=$this->quoteModel->loadByCustomerId($quote,$customerId); // where `$customerId` is your `customer id`
return $items = $customerQuote->getAllItems();
出于安全考虑,我将传递自定义永久令牌和客户 ID。
我还尝试了许多其他示例代码,但它们不起作用任何人都可以在这里帮助我。 谢谢
解决方法
我假设您将客户 ID 传递给 POST 方法。 你可以试试关注吗?
添加以下要求
...
use Magento\Quote\Model\QuoteFactory;
...
protected $quoteFactory;
...
public function __construct(
...
\Magento\Quote\Model\QuoteFactory $quoteFactory,...
){
...
$this->quoteFactory = $quoteFactory;
...
}
试试下面的函数
public function getCart() {
$params = $this->request->getPostValue();
$customerId = $params["customer_id"];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);
$customerFirstName = $customerObj->getFirstname();
$customerFirstName = $customerObj->getLastname();
$quote = $this->quoteFactory->create()->loadByCustomer($customerObj);
$items = $quote->getAllItems();
$cart_data = array();
foreach($items as $item)
{
$cart_data[] = array(
'name'=> $item_data['name'],'product_id'=> $item_data['product_id'],'price'=>$item_data['price'],'qty'=> $item_data['qty'],);
}
return $cart_data;
}
,
我认为最好使用客户令牌而不是客户 ID,这就是 Magento 处理客户相关请求的方式。您可以在客户登录时检索令牌。
- 在
Vendor/Module/etc/webapi.xml
中声明您的 API 端点
<?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/example/customer/quote" method="GET">
<service class="Vendor\Module\Api\QuoteManagerInterface" method="getQuote"/>
<resources>
<resource ref="self"/>
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
</routes>
- 你的函数应该是这样的
public function getQuote(int $customerId)
{
// Token will be automatically replaced with the Customer ID,so $customerId is an integer here
}
- API 请求看起来像那样
GET: {{host}}example/customer/quote
HEADER: Authorization: Bearer {{customerToken}}
卷曲示例
curl -sS -H 'Content-type: application/json' -H 'X-HTTP-Method-Override: GET' \
-H 'Authorization: Bearer 99x8vbo999ox5qxjm7uhsso7ny3fzl5o' \
-X GET http://local.magento2.com/rest/default/V1/example/customer/quote
这是一种更安全的检索客户数据的方式。我不建议您通过 API 传递客户 ID。
或,
如果要使用管理员令牌,请在端点声明中指定客户 ID。它也可用作函数的第一个参数。
<?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/example/customer/quote/:customerId" method="GET">
<service class="Vendor\Module\Api\QuoteManagerInterface" method="getQuote"/>
<resources>
<resource ref="Vendor_Module::general"/>
</resources>
</route>
</routes>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。