magento2-docker/app/code/IpSupply/CartToQuote/Model/CartItemApi.php

185 lines
5.3 KiB
PHP
Executable File

<?php
namespace IpSupply\CartToQuote\Model;
use Magento\Framework\App\Helper\Context;
use IpSupply\CartToQuote\Api\CartItemInterface;
use Magento\Framework\ObjectManagerInterface;
class CartItemApi extends \Magento\Framework\App\Helper\AbstractHelper implements CartItemInterface
{
protected $cartItemFactory;
protected $_customerRepositoryInterface;
protected $request;
public function __construct(
Context $context,
\IpSupply\CartToQuote\Model\CartItemFactory $cartItemFactory,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
\Magento\Framework\Webapi\Rest\Request $request
) {
$this->cartItemFactory = $cartItemFactory;
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->request = $request;
parent::__construct($context);
}
private function getCartItems($customerId) {
$collection = $this->cartItemFactory->create()->getCollection();
$data = $collection->addFieldToFilter('customer_id', ['eq' => $customerId])
->setOrder('name','ASC')
->getData();
return $data;
}
private function isCartItem($item) {
$collection = $this->cartItemFactory->create()->getCollection();
$count = $collection->addFieldToSelect('id')
->addFieldToFilter('customer_id', ['eq' => $item["customer_id"]])
->addFieldToFilter('product_id', ['eq' => $item["product_id"]])
->count();
if ($count > 0) {
return true;
}
return false;
}
private function addCartItem($item) {
if (!$this->isCartItem($item)) {
$data = $this->cartItemFactory->create()->addData([
'email' => $item["email"],
'name' => $item["name"],
'sku' => $item["sku"],
'product_id' => $item["product_id"],
'qty' => $item["qty"],
'customer_id' => $item["customer_id"],
'price' => $item["price"],
'url' => $item["url"],
'image' => $item["image"]
])->save();
return $data;
}
return null;
}
private function removeCartItem($item) {
$collection = $this->cartItemFactory->create()->getCollection();
$count = $collection->addFieldToSelect('id')
->addFieldToFilter('customer_id', ['eq' => $item["customer_id"]])
->addFieldToFilter('product_id', ['eq' => $item["product_id"]])
->walk('delete');
}
private function clearCartItem($customerId) {
$collection = $this->cartItemFactory->create()->getCollection();
$count = $collection->addFieldToSelect('id')
->addFieldToFilter('customer_id', ['eq' => $customerId])
->walk('delete');
}
private function getCurrentCustomerId($data){
$customer = $this->_customerRepositoryInterface->getById($data['customer_id']);
if ($customer) {
if ($customer->getEmail() == $data['email']){
return $customer->getId();
}
}
$customerId = $this->sessionFactory->create()->getCustomer()->getId();
return 0;
}
/**
* POST
* @param string[] $data
* @return string
*/
public function addItem($data){
$currentCustomerId = $this->getCurrentCustomerId($data);
if ($currentCustomerId == 0) {
return [];
}
$data['customer_id'] = $currentCustomerId;
if (!$this->isCartItem($data)) {
$this->addCartItem($data);
}
return $this->getCartItems($currentCustomerId);
}
/**
* POST
* @param string[] $data
* @return string
*/
public function updateItem($data){
$currentCustomerId = $this->getCurrentCustomerId($data);
if ($currentCustomerId == 0) {
return [];
}
$data['customer_id'] = $currentCustomerId;
if ($this->isCartItem($data)) {
$this->removeCartItem($data);
$this->addCartItem($data);
}
return $this->getCartItems($currentCustomerId);
}
/**
* POST
* @param string[] $data
* @return string
*/
public function removeItem($data){
$currentCustomerId = $this->getCurrentCustomerId($data);
if ($currentCustomerId == 0) {
return [];
}
$data['customer_id'] = $currentCustomerId;
$this->removeCartItem($data);
return $this->getCartItems($currentCustomerId);
}
/**
* get
* @return string
*/
public function getData(){
$par = $this->_request->getParams();
if (!array_key_exists('customer_id', $par) || !array_key_exists('email', $par)) {
return [];
}
$currentCustomerId = $this->getCurrentCustomerId($par);
if ($currentCustomerId == 0) {
return [];
}
return $this->getCartItems($currentCustomerId);
}
/**
* POST
* @param string[] $data
* @return string
*/
public function clearItem($data){
$currentCustomerId = $this->getCurrentCustomerId($data);
if ($currentCustomerId == 0) {
return [];
}
$this->clearCartItem($currentCustomerId);
return $this->getCartItems($currentCustomerId);
}
}