Cart.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Sales\Block\Adminhtml\Order\Create\Sidebar;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Catalog\Pricing\Price\FinalPrice;
  10. /**
  11. * Adminhtml sales order create sidebar cart block
  12. *
  13. * @api
  14. * @author Magento Core Team <core@magentocommerce.com>
  15. * @since 100.0.2
  16. */
  17. class Cart extends \Magento\Sales\Block\Adminhtml\Order\Create\Sidebar\AbstractSidebar
  18. {
  19. /**
  20. * Storage action on selected item
  21. *
  22. * @var string
  23. */
  24. protected $_sidebarStorageAction = 'add_cart_item';
  25. /**
  26. * Constructor
  27. *
  28. * @return void
  29. */
  30. protected function _construct()
  31. {
  32. parent::_construct();
  33. $this->setId('sales_order_create_sidebar_cart');
  34. $this->setDataId('cart');
  35. }
  36. /**
  37. * Get header text
  38. *
  39. * @return \Magento\Framework\Phrase
  40. */
  41. public function getHeaderText()
  42. {
  43. return __('Shopping Cart');
  44. }
  45. /**
  46. * Retrieve item collection
  47. *
  48. * @return mixed
  49. */
  50. public function getItemCollection()
  51. {
  52. $collection = $this->getData('item_collection');
  53. if ($collection === null) {
  54. $collection = $this->getCreateOrderModel()->getCustomerCart()->getAllVisibleItems();
  55. $this->setData('item_collection', $collection);
  56. }
  57. return $collection;
  58. }
  59. /**
  60. * @inheritdoc
  61. * @since 102.0.1
  62. */
  63. public function getItemPrice(Product $product)
  64. {
  65. $customPrice = $this->getCartItemCustomPrice($product);
  66. $price = $customPrice ?? $product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue();
  67. return $this->convertPrice($price);
  68. }
  69. /**
  70. * Retrieve display item qty availability
  71. *
  72. * @return true
  73. */
  74. public function canDisplayItemQty()
  75. {
  76. return true;
  77. }
  78. /**
  79. * Retrieve identifier of block item
  80. *
  81. * @param \Magento\Framework\DataObject $item
  82. * @return int
  83. */
  84. public function getIdentifierId($item)
  85. {
  86. return $item->getId();
  87. }
  88. /**
  89. * Retrieve product identifier linked with item
  90. *
  91. * @param \Magento\Quote\Model\Quote\Item $item
  92. * @return int
  93. */
  94. public function getProductId($item)
  95. {
  96. return $item->getProduct()->getId();
  97. }
  98. /**
  99. * Prepare layout
  100. *
  101. * Add button that clears customer's shopping cart
  102. *
  103. * @return $this
  104. */
  105. protected function _prepareLayout()
  106. {
  107. $deleteAllConfirmString = __('Are you sure you want to delete all items from shopping cart?');
  108. $this->addChild(
  109. 'empty_customer_cart_button',
  110. \Magento\Backend\Block\Widget\Button::class,
  111. [
  112. 'label' => __('Clear Shopping Cart'),
  113. 'onclick' => 'order.clearShoppingCart(\'' . $deleteAllConfirmString . '\')'
  114. ]
  115. );
  116. return parent::_prepareLayout();
  117. }
  118. /**
  119. * Returns cart item custom price.
  120. *
  121. * @param Product $product
  122. * @return float|null
  123. */
  124. private function getCartItemCustomPrice(Product $product): ?float
  125. {
  126. $items = $this->getItemCollection();
  127. foreach ($items as $item) {
  128. $productItemId = $this->getProduct($item)->getId();
  129. if ($productItemId === $product->getId() && $item->getCustomPrice()) {
  130. return (float)$item->getCustomPrice();
  131. }
  132. }
  133. return null;
  134. }
  135. }