|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Vuefront\Vuefront\Model\Api\Model\Store; |
| 4 | + |
| 5 | +use \Vuefront\Vuefront\Model\Api\System\Engine\Model; |
| 6 | + |
| 7 | +class Cart extends Model |
| 8 | +{ |
| 9 | + |
| 10 | + private $_currencyHelper; |
| 11 | + |
| 12 | + /** |
| 13 | + * @var \Magento\Downloadable\Api\LinkRepositoryInterface |
| 14 | + */ |
| 15 | + private $_linkRepository; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var \Magento\Checkout\Model\Cart |
| 19 | + */ |
| 20 | + private $_cartModel; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \Magento\Quote\Model\Quote |
| 24 | + */ |
| 25 | + private $_quoteModel; |
| 26 | + |
| 27 | + private $_sessionFactory; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + \Magento\Framework\Pricing\Helper\Data $currencyHelper, |
| 31 | + \Magento\Checkout\Model\Cart $cartModel, |
| 32 | + \Magento\Downloadable\Api\LinkRepositoryInterface $linkRepository, |
| 33 | + \Magento\Customer\Model\Session $sessionFactory, |
| 34 | + \Magento\Quote\Model\Quote $quoteFactory |
| 35 | + ) { |
| 36 | + $this->_currencyHelper = $currencyHelper; |
| 37 | + $this->_cartModel = $cartModel; |
| 38 | + $this->_linkRepository = $linkRepository; |
| 39 | + $this->_sessionFactory = $sessionFactory; |
| 40 | + $this->_quoteModel = $quoteFactory; |
| 41 | + }//end __construct() |
| 42 | + |
| 43 | + public function prepareCart() |
| 44 | + { |
| 45 | + $cart = []; |
| 46 | + $this->_cartModel->getQuote()->collectTotals(); |
| 47 | + $cart = [ |
| 48 | + 'products' => [], |
| 49 | + 'total' => $this->currency->format($this->_cartModel->getQuote()->getGrandTotal()), |
| 50 | + ]; |
| 51 | + |
| 52 | + $results = $this->_cartModel->getItems(); |
| 53 | + |
| 54 | + foreach ($results as $value) { |
| 55 | + /* |
| 56 | + @var \Magento\Catalog\Model\Product $product |
| 57 | + */ |
| 58 | + $product = $value->getProduct(); |
| 59 | + if (!$value->isDeleted() && !$value->getParentItemId() && !$value->getParentItem()) { |
| 60 | + $price = ""; |
| 61 | + if ($product->getTypeId() != "simple") { |
| 62 | + $price = $this->_currencyHelper->currency($product->getFinalPrice(), true, false); |
| 63 | + } else { |
| 64 | + $price = $this->_currencyHelper->currency($product->getPrice(), true, false); |
| 65 | + } |
| 66 | + $cart['products'][] = [ |
| 67 | + 'key' => $value->getId(), |
| 68 | + 'product' => [ |
| 69 | + 'product_id' => $product->getId(), |
| 70 | + 'price' => $price |
| 71 | + ], |
| 72 | + 'quantity' => $value->getQty(), |
| 73 | + 'option' => $this->getCartOptions($product, $value), |
| 74 | + 'total' => $this->currency->format($value->getPrice() * $value->getQty()), |
| 75 | + ]; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return $cart; |
| 80 | + } |
| 81 | + |
| 82 | + public function getProduct($key) |
| 83 | + { |
| 84 | + $product = null; |
| 85 | + |
| 86 | + foreach ($this->cart->getProducts() as $value) { |
| 87 | + if ($value['cart_id'] == $key) { |
| 88 | + $product = $value; |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + if ($product === null) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + return $product; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param $product \Magento\Catalog\Model\Product |
| 101 | + * @param $value mixed |
| 102 | + * @return array |
| 103 | + */ |
| 104 | + public function getCartOptions($product, $value) |
| 105 | + { |
| 106 | + $options = []; |
| 107 | + $result_options = $product->getTypeInstance(true)->getOrderOptions($product); |
| 108 | + |
| 109 | + if (!empty($result_options['attributes_info'])) { |
| 110 | + foreach ($result_options['attributes_info'] as $option) { |
| 111 | + $options[] = [ |
| 112 | + 'option_id' => (int)$option['option_id'], |
| 113 | + 'option_value_id' => (int)$option['option_value'], |
| 114 | + ]; |
| 115 | + } |
| 116 | + } |
| 117 | + return $options; |
| 118 | + } |
| 119 | +} |
0 commit comments