Skip to content

Commit

Permalink
MAGETWO-90516: Wrong custom option behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Leshchenko committed Jul 31, 2018
1 parent cfc7fb9 commit 4fae2ef
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 153 deletions.
57 changes: 40 additions & 17 deletions app/code/Magento/Catalog/Block/Product/View/Options/Type/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
namespace Magento\Catalog\Block\Product\View\Options\Type;

use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Block\Product\View\Options\View\Checkable;
use Magento\Catalog\Block\Product\View\Options\View\Multiple;
use Magento\Catalog\Block\Product\View\Options\View\CheckableFactory;
use Magento\Catalog\Block\Product\View\Options\View\MultipleFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Pricing\Helper\Data;
use Magento\Catalog\Helper\Data as CatalogHelper;

/**
* Product options text type block
Expand All @@ -19,38 +22,58 @@
*/
class Select extends \Magento\Catalog\Block\Product\View\Options\AbstractOptions
{
/**
* @var CheckableFactory
*/
protected $checkableFactory;

/**
* @var MultipleFactory
*/
protected $multipleFactory;

/**
* Select constructor.
* @param Context $context
* @param Data $pricingHelper
* @param CatalogHelper $catalogData
* @param array $data
* @param CheckableFactory|null $checkableFactory
* @param MultipleFactory|null $multipleFactory
*/
public function __construct(
Context $context,
Data $pricingHelper,
CatalogHelper $catalogData,
array $data = [],
CheckableFactory $checkableFactory = null,
MultipleFactory $multipleFactory = null
) {
parent::__construct($context, $pricingHelper, $catalogData, $data);
$this->checkableFactory = $checkableFactory ?: ObjectManager::getInstance()->get(CheckableFactory::class);
$this->multipleFactory = $multipleFactory ?: ObjectManager::getInstance()->get(MultipleFactory::class);
}

/**
* Return html for control element
*
* @return string
*/
public function getValuesHtml()
public function getValuesHtml(): string
{
$option = $this->getOption();
$optionType = $option->getType();
$objectManager = ObjectManager::getInstance();
// Remove inline prototype onclick and onchange events

if ($optionType === ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN ||
$optionType === ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE
) {
$optionBlock = $objectManager->create(
Multiple::class,
[
'option' => $option
]
);
$optionBlock = $this->multipleFactory->create();
}

if ($optionType === ProductCustomOptionInterface::OPTION_TYPE_RADIO ||
$optionType === ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX
) {
$optionBlock = $objectManager->create(
Checkable::class,
[
'option' => $option
]
);
$optionBlock = $this->checkableFactory->create();
}

return $optionBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,21 @@
*/
class Checkable extends AbstractOptions
{
protected $_template = 'Magento_Catalog::catalog/product/composite/fieldset/options/view/checkable.phtml';

/**
* Checkable constructor.
* @param Context $context
* @param Data $pricingHelper
* @param CatalogHelper $catalogData
* @param array $data
*/
public function __construct(
Context $context,
Data $pricingHelper,
CatalogHelper $catalogData,
array $data = []
) {
parent::__construct($context, $pricingHelper, $catalogData, $data);
}
protected $_template = 'Magento_Catalog::product/composite/fieldset/options/view/checkable.phtml';

/**
* @param $value
* @return string
*/
public function formatPrice(array $value) : string
public function formatPrice(ProductCustomOptionValuesInterface $value) : string
{
return parent::_formatPrice($value);

return parent::_formatPrice(
[
'is_percent' => $value->getPriceType() === 'percent',
'pricing_value' => $value->getPrice($value->getPriceType() === 'percent')
]
);
}

/**
Expand All @@ -57,4 +47,9 @@ public function getCurrencyByStore(ProductCustomOptionValuesInterface $value) :
false
);
}

public function getPreconfiguredValue($option) : string
{
$configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Multiple extends AbstractOptions
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _toHtml() : string
protected function _toHtml()
{
$option = $this->getOption();
$optionType = $option->getType();
Expand All @@ -38,7 +38,7 @@ protected function _toHtml() : string
]
);

$select = $this->setSelectOption($select, $option);
$select = $this->insertSelectOption($select, $option);
$select = $this->processSelectOption($select, $option);

if ($optionType === ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE) {
Expand All @@ -64,7 +64,7 @@ protected function _toHtml() : string
* @param Option $option
* @return Select
*/
private function setSelectOption(Select $select, Option $option) : Select
private function insertSelectOption(Select $select, Option $option) : Select
{
$require = $option->getIsRequire() ? ' required' : '';

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,39 @@
* See COPYING.txt for license details.
*/

use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
/**
* @var \Magento\Catalog\Block\Product\View\Options\View\Checkable $block
*/
$option = $block->getOption();

if ($option) : ?>
<?php
$configValue = $block->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId());
$configValue = $block->getPreconfiguredValue($option);
$optionType = $option->getType();
$arraySign = $optionType === 'checkbox' ? '[]' : '';
$arraySign = $optionType === ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX ? '[]' : '';
$count = 1;
?>

<div class="options-list nested" id="options-' <?= /* @noEscape */ $option->getId() ?>'-list">
<div class="options-list nested" id="options-<?= /* @noEscape */ $option->getId() ?>-list">

<?php if ($optionType === 'radio') : ?>
<div class="field choice field field-option">
<?php if ($optionType === ProductCustomOptionInterface::OPTION_TYPE_RADIO) : ?>
<div class="field choice admin__field admin__field-option">
<input type="radio"
id="options_<?= /* @noEscape */ $option->getId() ?>"
class="radio control-radio product-custom-option"
class="radio admin__control-radio product-custom-option"
name="options[<?= /* @noEscape */ $option->getId() ?>]"
data-selector="options[<?= /* @noEscape */ $option->getId() ?>]"
onclick="<?= $block->getSkipJsReloadPrice() ? '' : 'opConfig.reloadPrice()' ?>"
value=""
checked="checked"
/>
<label class="label field-label" for="options_<?= /* @noEscape */ $option->getId() ?>">
<label class="label admin__field-label" for="options_<?= /* @noEscape */ $option->getId() ?>">
<span>
<?= /* @noEscape */ __('None') ?>
</span>
</label>
<?php endif; ?>
<?php endif; ?>

<?php foreach ($option->getValues() as $value) : ?>
<?php
Expand All @@ -48,25 +49,19 @@ $count = 1;
$checked = $configValue == $value->getOptionTypeId() ? 'checked' : '';
}

$priceStr = $block->formatPrice(
[
'is_percent' => $value->getPriceType() === 'percent',
'pricing_value' => $value->getPrice($value->getPriceType() === 'percent')
]
);

$dataSelector = 'options[' . $option->getId() . ']';
if ($arraySign) {
$dataSelector .= '[' . $value->getOptionTypeId() . ']';
}
?>

<div class="field choice field field-option'<?= /* @noEscape */ $option->getIsRequire() ?>'">
<input type="<?= /* @noEscape */ $optionType ?>" class="<?= /* @noEscape */
$optionType === 'radio' ?
'radio control-radio' :
'checkbox control-checkbox' ?>
<?= /* @noEscape */ $option->getIsRequire() ?>
<div class="field choice admin__field admin__field-option'<?= /* @noEscape */ $option->getIsRequire() ?>'">
<input type="<?= /* @noEscape */ $optionType ?>"
class="<?= /* @noEscape */
/** @noinspection DisconnectedForeachInstructionInspection */
$optionType === ProductCustomOptionInterface::OPTION_TYPE_RADIO ?
'radio admin__control-radio' :
'checkbox admin__control-checkbox' ?> <?= /* @noEscape */ $option->getIsRequire() ?>
product-custom-option
<?= $block->getSkipJsReloadPrice() ? '' : 'opConfig.reloadPrice()' ?>"
name="options[<?= $option->getId() ?>]<?= /* @noEscape */ $arraySign ?>"
Expand All @@ -76,15 +71,15 @@ $count = 1;
data-selector="<?= /* @noEscape */ $dataSelector ?>"
price="<?= /* @noEscape */ $block->getCurrencyByStore($value) ?>"
/>
<label class="label field-label"
<label class="label admin__field-label"
for="options_<?= /* @noEscape */ $option->getId() . '_' . $count ?>">
<span>
<?= $block->escapeHtml($value->getTitle()) ?>
</span>
<?= /* @noEscape */ $priceStr ?>
<?= /* @noEscape */ $block->formatPrice($value) ?>
</label>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endif; ?>

0 comments on commit 4fae2ef

Please sign in to comment.