Carts.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Adminhtml\Edit\Tab;
  7. /**
  8. * Obtain all carts contents for specified client
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Carts extends \Magento\Backend\Block\Template
  14. {
  15. /**
  16. * @var \Magento\Customer\Model\Config\Share
  17. */
  18. protected $_shareConfig;
  19. /**
  20. * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
  21. */
  22. protected $customerDataFactory;
  23. /**
  24. * @var \Magento\Framework\Api\DataObjectHelper
  25. */
  26. protected $dataObjectHelper;
  27. /**
  28. * @param \Magento\Backend\Block\Template\Context $context
  29. * @param \Magento\Customer\Model\Config\Share $shareConfig
  30. * @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory
  31. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  32. * @param array $data
  33. */
  34. public function __construct(
  35. \Magento\Backend\Block\Template\Context $context,
  36. \Magento\Customer\Model\Config\Share $shareConfig,
  37. \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
  38. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  39. array $data = []
  40. ) {
  41. $this->_shareConfig = $shareConfig;
  42. $this->customerDataFactory = $customerDataFactory;
  43. $this->dataObjectHelper = $dataObjectHelper;
  44. parent::__construct($context, $data);
  45. }
  46. /**
  47. * Add shopping cart grid of each website
  48. *
  49. * @return $this
  50. */
  51. protected function _prepareLayout()
  52. {
  53. $sharedWebsiteIds = $this->_shareConfig->getSharedWebsiteIds($this->_getCustomer()->getWebsiteId());
  54. $isShared = count($sharedWebsiteIds) > 1;
  55. foreach ($sharedWebsiteIds as $websiteId) {
  56. $blockName = 'customer_cart_' . $websiteId;
  57. $block = $this->getLayout()->createBlock(
  58. \Magento\Customer\Block\Adminhtml\Edit\Tab\Cart::class,
  59. $blockName,
  60. ['data' => ['website_id' => $websiteId]]
  61. );
  62. if ($isShared) {
  63. $websiteName = $this->_storeManager->getWebsite($websiteId)->getName();
  64. $block->setCartHeader(__('Shopping Cart from %1', $websiteName));
  65. }
  66. $this->setChild($blockName, $block);
  67. }
  68. return parent::_prepareLayout();
  69. }
  70. /**
  71. * Just get child blocks html
  72. *
  73. * @return string
  74. */
  75. protected function _toHtml()
  76. {
  77. $this->_eventManager->dispatch('adminhtml_block_html_before', ['block' => $this]);
  78. return $this->getChildHtml();
  79. }
  80. /**
  81. * @return \Magento\Customer\Api\Data\CustomerInterface
  82. */
  83. protected function _getCustomer()
  84. {
  85. $customerDataObject = $this->customerDataFactory->create();
  86. $this->dataObjectHelper->populateWithArray(
  87. $customerDataObject,
  88. $this->_backendSession->getCustomerData()['account'],
  89. \Magento\Customer\Api\Data\CustomerInterface::class
  90. );
  91. return $customerDataObject;
  92. }
  93. }