Skip to content

Commit de58008

Browse files
committed
Added custom render select and checkbox
1 parent 7ba4a09 commit de58008

File tree

11 files changed

+497
-0
lines changed

11 files changed

+497
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DK\CustomOptionDefaultValue\Block\Product\View\Options\Type\Select;
6+
7+
use Magento\Catalog\Api\Data\ProductCustomOptionValuesInterface;
8+
use Magento\Catalog\Block\Product\View\Options\AbstractOptions;
9+
use Magento\Catalog\Model\Product\Option;
10+
11+
class Checkable extends AbstractOptions
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $_template = 'DK_CustomOptionDefaultValue::product/composite/fieldset/options/view/checkable.phtml';
17+
18+
/**
19+
* Returns formatted price
20+
*/
21+
public function formatPrice(ProductCustomOptionValuesInterface $value): string
22+
{
23+
// @noinspection PhpMethodParametersCountMismatchInspection
24+
return parent::_formatPrice(
25+
[
26+
'is_percent' => $value->getPriceType() === 'percent',
27+
'pricing_value' => $value->getPrice($value->getPriceType() === 'percent'),
28+
]
29+
);
30+
}
31+
32+
/**
33+
* Returns current currency for store
34+
*
35+
* @return float|string
36+
*/
37+
public function getCurrencyByStore(ProductCustomOptionValuesInterface $value)
38+
{
39+
// @noinspection PhpMethodParametersCountMismatchInspection
40+
return $this->pricingHelper->currencyByStore(
41+
$value->getPrice(true),
42+
$this->getProduct()->getStore(),
43+
false
44+
);
45+
}
46+
47+
/**
48+
* Returns preconfigured value for given option
49+
*
50+
* @return null|array|string
51+
*/
52+
public function getPreconfiguredValue(Option $option)
53+
{
54+
return $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId());
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DK\CustomOptionDefaultValue\Block\Product\View\Options\Type\Select;
6+
7+
use Magento\Catalog\Block\Product\View\Options\AbstractOptions;
8+
use Magento\Catalog\Model\Product\Option;
9+
use Magento\Framework\View\Element\Html\Select;
10+
11+
class Multiple extends AbstractOptions
12+
{
13+
/**
14+
* @var bool
15+
*/
16+
private $existConfigValue = false;
17+
18+
/**
19+
* {@inheritdoc}
20+
*
21+
* @throws \Magento\Framework\Exception\LocalizedException
22+
*
23+
* @return string
24+
*/
25+
protected function _toHtml()
26+
{
27+
$option = $this->getOption();
28+
$optionType = $option->getType();
29+
$configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId());
30+
$require = $option->getIsRequire() ? ' required' : '';
31+
$extraParams = '';
32+
33+
if ($configValue) {
34+
$this->existConfigValue = true;
35+
}
36+
37+
/** @var Select $select */
38+
$select = $this->getLayout()->createBlock(
39+
Select::class
40+
)->setData(
41+
[
42+
'id' => 'select_' . $option->getId(),
43+
'class' => $require . ' product-custom-option admin__control-select',
44+
]
45+
);
46+
$select = $this->insertSelectOption($select, $option);
47+
$select = $this->processSelectOption($select, $option);
48+
49+
if ($optionType === Option::OPTION_TYPE_MULTIPLE) {
50+
$extraParams = ' multiple="multiple"';
51+
}
52+
if (!$this->getSkipJsReloadPrice()) {
53+
$extraParams .= ' onchange="opConfig.reloadPrice()"';
54+
}
55+
56+
$extraParams .= ' data-selector="' . $select->getName() . '"';
57+
$select->setExtraParams($extraParams);
58+
59+
if ($this->existConfigValue) {
60+
$select->setValue($configValue);
61+
}
62+
63+
return $select->getHtml();
64+
}
65+
66+
/**
67+
* Returns select with inserted option give as a parameter
68+
*/
69+
private function insertSelectOption(Select $select, Option $option): Select
70+
{
71+
$require = $option->getIsRequire() ? ' required' : '';
72+
if ($option->getType() === Option::OPTION_TYPE_DROP_DOWN) {
73+
$select->setName('options[' . $option->getId() . ']')->addOption('', __('-- Please Select --'));
74+
} else {
75+
$select->setName('options[' . $option->getId() . '][]');
76+
$select->setClass('multiselect admin__control-multiselect' . $require . ' product-custom-option');
77+
}
78+
79+
return $select;
80+
}
81+
82+
/**
83+
* Returns select with formatted option prices
84+
*/
85+
private function processSelectOption(Select $select, Option $option): Select
86+
{
87+
$store = $this->getProduct()->getStore();
88+
foreach ($option->getValues() as $_value) {
89+
$isPercentPriceType = $_value->getPriceType() === 'percent';
90+
$priceStr = $this->_formatPrice(
91+
[
92+
'is_percent' => $isPercentPriceType,
93+
'pricing_value' => $_value->getPrice($isPercentPriceType),
94+
],
95+
false
96+
);
97+
98+
$selectOption = [];
99+
if (!$this->existConfigValue && $_value->getData('is_default')) {
100+
$selectOption = ['selected' => 'selected'];
101+
}
102+
103+
$select->addOption(
104+
$_value->getOptionTypeId(),
105+
$_value->getTitle() . ' ' . strip_tags($priceStr) . '',
106+
[
107+
'price' => $this->pricingHelper->currencyByStore(
108+
$_value->getPrice(true),
109+
$store,
110+
false
111+
),
112+
$selectOption,
113+
]
114+
);
115+
}
116+
117+
return $select;
118+
}
119+
}

Model/Config.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DK\CustomOptionDefaultValue\Model;
6+
7+
use Magento\Framework\App\Config\ScopeConfigInterface;
8+
use Magento\Store\Model\ScopeInterface;
9+
10+
class Config
11+
{
12+
private const XML_PATH_GENERAL_ACTIVE = 'custom_option_default_value/general/active';
13+
14+
/**
15+
* @var ScopeConfigInterface
16+
*/
17+
private $scopeConfig;
18+
19+
public function __construct(ScopeConfigInterface $scopeConfig)
20+
{
21+
$this->scopeConfig = $scopeConfig;
22+
}
23+
24+
public function isActiveModule($store = null): bool
25+
{
26+
return (bool) $this->scopeConfig->isSetFlag(
27+
self::XML_PATH_GENERAL_ACTIVE,
28+
ScopeInterface::SCOPE_STORE,
29+
$store
30+
);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DK\CustomOptionDefaultValue\Plugin\Catalog\Block\Product\View\Options\Type;
6+
7+
use DK\CustomOptionDefaultValue\Block\Product\View\Options\Type\Select\CheckableFactory;
8+
use DK\CustomOptionDefaultValue\Block\Product\View\Options\Type\Select\MultipleFactory;
9+
use DK\CustomOptionDefaultValue\Model\Config;
10+
use Magento\Catalog\Block\Product\View\Options\Type\Select as TypeSelect;
11+
use Magento\Catalog\Model\Product\Option;
12+
13+
final class Select
14+
{
15+
/**
16+
* @var MultipleFactory
17+
*/
18+
private $multipleFactory;
19+
20+
/**
21+
* @var CheckableFactory
22+
*/
23+
private $checkableFactory;
24+
25+
/**
26+
* @var Config
27+
*/
28+
private $config;
29+
30+
public function __construct(
31+
MultipleFactory $multipleFactory,
32+
CheckableFactory $checkableFactory,
33+
Config $config
34+
) {
35+
$this->multipleFactory = $multipleFactory;
36+
$this->checkableFactory = $checkableFactory;
37+
$this->config = $config;
38+
}
39+
40+
public function aroundGetValuesHtml(TypeSelect $subject, \Closure $proceed)
41+
{
42+
if (!$this->config->isActiveModule()) {
43+
return $proceed();
44+
}
45+
46+
$option = $subject->getOption();
47+
$optionType = $option->getType();
48+
49+
$optionBlock = null;
50+
if ($optionType === Option::OPTION_TYPE_DROP_DOWN ||
51+
$optionType === Option::OPTION_TYPE_MULTIPLE
52+
) {
53+
$optionBlock = $this->multipleFactory->create();
54+
}
55+
56+
if ($optionType === Option::OPTION_TYPE_RADIO ||
57+
$optionType === Option::OPTION_TYPE_CHECKBOX
58+
) {
59+
$optionBlock = $this->checkableFactory->create();
60+
}
61+
62+
if (null === $optionBlock) {
63+
return $proceed();
64+
}
65+
66+
return $optionBlock
67+
->setOption($option)
68+
->setProduct($subject->getProduct())
69+
->setSkipJsReloadPrice(1)
70+
->toHtml();
71+
}
72+
}

Plugin/Catalog/UI/Form/Modifier/IsDefaultCustomOption.php

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DK\CustomOptionDefaultValue\Plugin\Catalog\UI\Form\Modifier;
66

7+
use DK\CustomOptionDefaultValue\Model\Config;
78
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions;
89
use Magento\Ui\Component\Form\Element\Checkbox;
910
use Magento\Ui\Component\Form\Element\DataType\Text;
@@ -14,8 +15,22 @@ class IsDefaultCustomOption
1415
protected const FIELD_IS_DEFAULT = 'is_default';
1516
private const DEFAULT_SORT_ORDER = 70;
1617

18+
/**
19+
* @var Config
20+
*/
21+
private $config;
22+
23+
public function __construct(Config $config)
24+
{
25+
$this->config = $config;
26+
}
27+
1728
public function afterModifyMeta(CustomOptions $subject, array $meta): array
1829
{
30+
if (!$this->config->isActiveModule()) {
31+
return $meta;
32+
}
33+
1934
return array_replace_recursive($meta, [
2035
CustomOptions::GROUP_CUSTOM_OPTIONS_NAME => [
2136
'children' => [

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Magento 2 Custom Option Default Value
2+
Magento 2 Custom Option Default Value extension use to set custom option default value for products.
3+
4+
# Installation
5+
#### Step-by-step to install the Magento 2 extension through Composer:
6+
1. Locate your Magento 2 project root.
7+
8+
2. Install the Magento 2 extension using [Composer](https://getcomposer.org/)
9+
```bash
10+
composer require dmitrykazak/magento2-custom-option-default-value
11+
```
12+
13+
3. After installation is completed the extension:
14+
```bash
15+
# Enable the extension and clear static view files
16+
$ bin/magento module:enable DK_CustomOptionDefaultValue --clear-static-content
17+
18+
# Update the database schema and data
19+
$ bin/magento setup:upgrade
20+
21+
# Recompile your Magento project
22+
$ bin/magento setup:di:compile
23+
24+
# Clean the cache
25+
$ bin/magento cache:flush
26+
```
27+
#### Manually (not recommended)
28+
* Download the extension of the required version
29+
* Unzip the file
30+
* Create a folder ````{root}/app/code/DK/CustomOptionDefaultValue````
31+
* Copy the files this folder
32+
33+
#### Overview
34+
*The supported custom options are:*
35+
* Dropdown
36+
* Multiselect
37+
* Radio Box
38+
* Checkbox
39+
40+
#### Support
41+
If you encounter any problems or bugs, please open an [issue](https://github.com/dmitrykazak/magento2-custom-option-default-value/issues) on GitHub.
42+
43+
#### Links
44+
* [Contact with me](https://developer-vub3295.slack.com/messages/CLG5P5A0N)

etc/acl.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
3+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
4+
<acl>
5+
<resources>
6+
<resource id="Magento_Backend::admin">
7+
<resource id="Magento_Backend::stores">
8+
<resource id="Magento_Backend::stores_settings">
9+
<resource id="Magento_Config::config">
10+
<resource id="DK_CustomOptionDefaultValue::custom_option_default_value" title="Custom Option Default Value" translate="title" sortOrder="130" />
11+
</resource>
12+
</resource>
13+
</resource>
14+
</resource>
15+
</resources>
16+
</acl>
17+
</config>
18+

etc/adminhtml/system.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
3+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
4+
<system>
5+
<section id="custom_option_default_value" translate="label" type="text" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
6+
<label>Custom Option Default Value</label>
7+
<tab>catalog</tab>
8+
<resource>DK_CustomOptionDefaultValue::custom_option_default_value</resource>
9+
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
10+
<label>General Configuration</label>
11+
<field id="active" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
12+
<label>Enable</label>
13+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
14+
<comment>Enable Custom Option Default Value extension</comment>
15+
</field>
16+
</group>
17+
</section>
18+
</system>
19+
</config>

0 commit comments

Comments
 (0)