Php Magento 2 API使用客户ID获取客户购物车数据
我正在构建自定义magento 2 API。我想使用客户ID获取客户购物车数据。我这样做是为了将magento数据提供给android和ios客户设备 我尝试了以下代码,但它不工作Php Magento 2 API使用客户ID获取客户购物车数据,php,api,magento,magento2,Php,Api,Magento,Magento2,我正在构建自定义magento 2 API。我想使用客户ID获取客户购物车数据。我这样做是为了将magento数据提供给android和ios客户设备 我尝试了以下代码,但它不工作 $params = $this->request->getPostValue(); $customerId = $params["customer_id"]; $objectManager = \Magento\Framework\App\ObjectManager:
$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
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>
谢谢你,维克多。是的,出于安全考虑,我正在通过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
<?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>