Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Composite.php
Go to the documentation of this file.
1 <?php
7 
16 
27 {
33  protected $_coreRegistry = null;
34 
40  protected $_catalogProduct = null;
41 
45  protected $_storeManager;
46 
51 
55  protected $productRepository;
56 
65  public function __construct(
66  Context $context,
68  Product $catalogProduct,
69  Registry $coreRegistry,
72  ) {
73  $this->_storeManager = $storeManager;
74  $this->_coreRegistry = $coreRegistry;
75  $this->_catalogProduct = $catalogProduct;
76  $this->resultLayoutFactory = $resultLayoutFactory;
77  $this->productRepository = $productRepository;
78  parent::__construct($context);
79  }
80 
86  protected function _initUpdateResultLayout()
87  {
88  $resultLayout = $this->resultLayoutFactory->create();
89  $resultLayout->addHandle('CATALOG_PRODUCT_COMPOSITE_UPDATE_RESULT');
90  return $resultLayout;
91  }
92 
100  public function renderUpdateResult(\Magento\Framework\DataObject $updateResult)
101  {
102  $this->_coreRegistry->register('composite_update_result', $updateResult);
103  return $this->_initUpdateResultLayout();
104  }
105 
116  protected function _initConfigureResultLayout($isOk, $productType)
117  {
118  $resultLayout = $this->resultLayoutFactory->create();
119  if ($isOk) {
120  $resultLayout->addHandle('CATALOG_PRODUCT_COMPOSITE_CONFIGURE')
121  ->addHandle('catalog_product_view_type_' . $productType);
122  } else {
123  $resultLayout->addHandle('CATALOG_PRODUCT_COMPOSITE_CONFIGURE_ERROR');
124  }
125  return $resultLayout;
126  }
127 
138  public function renderConfigureResult(\Magento\Framework\DataObject $configureResult)
139  {
140  try {
141  if (!$configureResult->getOk()) {
142  throw new \Magento\Framework\Exception\LocalizedException(__($configureResult->getMessage()));
143  }
144 
145  $currentStoreId = (int)$configureResult->getCurrentStoreId();
146  if (!$currentStoreId) {
147  $currentStoreId = $this->_storeManager->getStore()->getId();
148  }
149 
150  $product = $this->productRepository->getById($configureResult->getProductId(), false, $currentStoreId);
151 
152  $this->_coreRegistry->register('current_product', $product);
153  $this->_coreRegistry->register('product', $product);
154 
155  // Register customer we're working with
156  $customerId = (int)$configureResult->getCurrentCustomerId();
157  $this->_coreRegistry->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
158 
159  // Prepare buy request values
160  $buyRequest = $configureResult->getBuyRequest();
161  if ($buyRequest) {
162  $this->_catalogProduct->prepareProductOptions($product, $buyRequest);
163  }
164 
165  $isOk = true;
166  $productType = $product->getTypeId();
167  } catch (\Exception $e) {
168  $isOk = false;
169  $productType = null;
170  $this->_coreRegistry->register('composite_configure_result_error_message', $e->getMessage());
171  }
172 
173  return $this->_initConfigureResultLayout($isOk, $productType);
174  }
175 }
$storeManager
__()
Definition: __.php:13
__construct(Context $context, StoreManagerInterface $storeManager, Product $catalogProduct, Registry $coreRegistry, LayoutFactory $resultLayoutFactory, ProductRepositoryInterface $productRepository)
Definition: Composite.php:65
renderConfigureResult(\Magento\Framework\DataObject $configureResult)
Definition: Composite.php:138
_initConfigureResultLayout($isOk, $productType)
Definition: Composite.php:116
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest
renderUpdateResult(\Magento\Framework\DataObject $updateResult)
Definition: Composite.php:100