Exemple #1
0
 /**
  * Returns JSON encoded config to be used in JS scripts
  *
  * @return string
  *
  */
 public function getJsonConfig()
 {
     /** @var Option[] $optionsArray */
     $optionsArray = $this->getOptions();
     $options = [];
     $currentProduct = $this->getProduct();
     $defaultValues = [];
     $preConfiguredFlag = $currentProduct->hasPreconfiguredValues();
     /** @var \Magento\Framework\Object|null $preConfiguredValues */
     $preConfiguredValues = $preConfiguredFlag ? $currentProduct->getPreconfiguredValues() : null;
     $position = 0;
     foreach ($optionsArray as $optionItem) {
         /* @var $optionItem Option */
         if (!$optionItem->getSelections()) {
             continue;
         }
         $optionId = $optionItem->getId();
         $options[$optionId] = $this->getOptionItemData($optionItem, $currentProduct, $position);
         // Add attribute default value (if set)
         if ($preConfiguredFlag) {
             $configValue = $preConfiguredValues->getData('bundle_option/' . $optionId);
             if ($configValue) {
                 $defaultValues[$optionId] = $configValue;
             }
         }
         $position++;
     }
     $config = $this->getConfigData($currentProduct, $options);
     $configObj = new \Magento\Framework\Object(['config' => $config]);
     //pass the return array encapsulated in an object for the other modules to be able to alter it eg: weee
     $this->_eventManager->dispatch('catalog_product_option_price_configuration_after', ['configObj' => $configObj]);
     $config = $configObj->getConfig();
     if ($preConfiguredFlag && !empty($defaultValues)) {
         $config['defaultValues'] = $defaultValues;
     }
     return $this->jsonEncoder->encode($config);
 }
Exemple #2
0
 /**
  * Get json representation of
  *
  * @return string
  */
 public function getJsonConfig()
 {
     $config = [];
     foreach ($this->getOptions() as $option) {
         /* @var $option \Magento\Catalog\Model\Product\Option */
         $priceValue = 0;
         if ($option->getGroupByType() == \Magento\Catalog\Model\Product\Option::OPTION_GROUP_SELECT) {
             $tmpPriceValues = [];
             foreach ($option->getValues() as $value) {
                 /* @var $value \Magento\Catalog\Model\Product\Option\Value */
                 $id = $value->getId();
                 $tmpPriceValues[$id] = $this->_getPriceConfiguration($value);
             }
             $priceValue = $tmpPriceValues;
         } else {
             $priceValue = $this->_getPriceConfiguration($option);
         }
         $config[$option->getId()] = $priceValue;
     }
     $configObj = new \Magento\Framework\Object(['config' => $config]);
     //pass the return array encapsulated in an object for the other modules to be able to alter it eg: weee
     $this->_eventManager->dispatch('catalog_product_option_price_configuration_after', ['configObj' => $configObj]);
     $config = $configObj->getConfig();
     return $this->_jsonEncoder->encode($config);
 }