Magento 2 API 使用客户 ID 获取客户购物车数据
Magento 2 API Get Customer Cart Data using customer 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>
我正在构建自定义 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>