Saya ingin mengatur opsi default ke nilai opsi kustom di tingkat produk.
Bagaimana cara melakukannya di Magento 2?
Tolong bantu saya untuk menyelesaikan ini.
Saya ingin mengatur opsi default ke nilai opsi kustom di tingkat produk.
Bagaimana cara melakukannya di Magento 2?
Tolong bantu saya untuk menyelesaikan ini.
Jawaban:
Saya tidak yakin Anda bisa melakukan ini melalui admin. Saya hanya melakukan pekerjaan di mana saya memastikan semua "opsi default" saya adalah opsi pertama dalam admin kemudian menambahkan di bawah ini ke js untuk toko saya.
<script>
require(['jquery', 'jquery/ui'], function($){
$('.product-add-form .field select').each(function(i, obj) {
$(obj).find('option:eq(1)').prop('selected', true);
});
});
</script>
Ini berfungsi untuk opsi khusus karena semuanya diberikan pada pemuatan halaman. Kode hanya loop melalui semua opsi khusus dan menetapkan opsi 2 sebagai yang pertama adalah "silakan pilih".
Namun saya mengalami sedikit kesulitan dengan produk-produk yang dapat dikonfigurasi karena semua opsi dimuat setelah memuat halaman tetapi untuk melakukannya Anda juga dapat melihat pertanyaan saya di sini: Magento 2: Bagaimana cara mengatur opsi default dalam opsi yang dapat dikonfigurasi?
Saya pikir apa yang ingin Anda capai adalah sesuatu seperti ini?
Saya menerapkannya untuk bidang dropdown, harus sama dengan radiobuttons.
catalog_product_option_type_value
.Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions
.Contoh:
vendor / module / etc / adminhtml / di.xml
<?xml version="1.0"?>
<config>
<type name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions">
<plugin name="CustomOptionsUiPlugin" type="Vendor\Module\Plugin\CustomOptionsUiPlugin" sortOrder="1"/>
</type>
</config>
Vendor \ Module \ Plugin \ CustomOptionsUiPlugin.php
namespace Vendor\Module\Plugin;
class CustomOptionsUiPlugin
{
...
public function afterModifyMeta(\Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions $subject,$meta)
{
$result = $meta;
$result['custom_options']['children']['options']['children']['record']['children']["container_option"]['children']['values']['children']['record']['children']['is_default'] = [
'arguments' => [
'data' => [
'config' => [
'label' => __('Default'),
'componentType' => 'field',
'formElement' => 'checkbox',
'dataScope' => 'is_default',
'dataType' => 'number',
'additionalClasses' => 'admin__field-small',
'sortOrder' => 55,
'value' => '0',
'valueMap' => [
'true' => '1',
'false' => '0'
]
]
]
]
];
return $result;
}
}
Dan terakhir Anda perlu menimpa file Magento\Catalog\Block\Product\View\Options\Type\Select.php
dengan sesuatu seperti ini
$defaultAttribute = array();
if($_value->getData('is_default') == true){
$defaultAttribute = ['selected' => 'selected','default' => 'default'];
}
$select->addOption(
$_value->getOptionTypeId(),
$_value->getTitle() . ' ' . strip_tags($priceStr) . '',
['price' => $this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false),$defaultAttribute]
);
Semoga itu bisa membantu!
$defaultAttribute
sebagai atribut tambahan untuk opsi akan bertentangan dengan nilai yang telah dikonfigurasikan sebelumnya (buy_request, ...). Misalnya, saat mengedit item keranjang, nilai yang dipilih oleh pelanggan dan nilai "is_default" keduanya akan ditandai seperti selected="selected"
dalam kode.
@TrytoFly Terima kasih atas solusi Anda. Saya telah mencoba dengan kode Anda dan saya dapat menampilkan opsi "Default" tetapi nilai-nilai khusus tidak ditampilkan. Tolong bantu saya untuk memperbaiki masalah ini. Tolong cari gambarnya
$result = $meta;
di awal Metode afterModifyMeta () Anda. Atau Anda hanya akan menimpa nilai kembali Anda alih-alih menambahkan opsi default untuk itu.
@TrytoFly Inilah yang bekerja untuk saya.
<?php
namespace Sigma\DefaultCustomOptions\Plugin;
class CustomOptionsUiPlugin
{
public function afterModifyMeta(
\Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions $subject,
$result
) {
$subject;
$result['custom_options']['children']['options']['children']['record']['children']["container_option"]
['children']['values']['children']['record']['children'] =array_replace_recursive(
$result['custom_options']['children']['options']['children']['record']['children']
["container_option"]['children']['values']['children']['record']['children'],
[
'is_default' => [
'arguments' => [
'data' => [
'config' => [
'label' => __('Default'),
'componentType' => 'field',
'formElement' => 'checkbox',
'dataScope' => 'is_default',
'dataType' => 'number',
'sortOrder' => 70,
'value' => '0',
'valueMap' => [
'true' => '1',
'false' => '0'
]
]
]
]
]
]
);
return $result;
}
}
Anda dapat mengganti file Select.php seperti fungsi kode folowing:
class AroundOptionValuesHtml extends \Magento\Catalog\Block\Product\View\Options\Type\Select
{
public function getValuesHtml()
{
$_option = $this->getOption();
$configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
$store = $this->getProduct()->getStore();
$this->setSkipJsReloadPrice(1);
// Remove inline prototype onclick and onchange events
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN ||
$_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE
) {
$require = $_option->getIsRequire() ? ' required' : '';
$extraParams = '';
$select = $this->getLayout()->createBlock(
\Magento\Framework\View\Element\Html\Select::class
)->setData(
[
'id' => 'select_' . $_option->getId(),
'class' => $require . ' product-custom-option admin__control-select'
]
);
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN) {
$select->setName('options[' . $_option->getId() . ']')->addOption('', __('-- Please Select --'));
} else {
$select->setName('options[' . $_option->getId() . '][]');
$select->setClass('multiselect admin__control-multiselect' . $require . ' product-custom-option');
}
foreach ($_option->getValues() as $_value) {
$priceStr = $this->_formatPrice(
[
'is_percent' => $_value->getPriceType() == 'percent',
'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
],
false
);
// custom code
$defaultAttribute = array();
if($_value->getData('is_default') == true){
$defaultAttribute = ['selected' => 'selected'];
}
// custom code
$select->addOption(
$_value->getOptionTypeId(),
$_value->getTitle() . ' ' . strip_tags($priceStr) . '',
['price' => $this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false),$defaultAttribute]
);
}
// custom code added
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE) {
$extraParams = ' multiple="multiple"';
}
if (!$this->getSkipJsReloadPrice()) {
$extraParams .= ' onchange="opConfig.reloadPrice()"';
}
$extraParams .= ' data-selector="' . $select->getName() . '"';
$select->setExtraParams($extraParams);
if ($configValue) {
$select->setValue($configValue);
}
return $select->getHtml();
}
}
}
Inilah cara terbersih yang saya temukan untuk menetapkan nilai default untuk opsi yang dapat disesuaikan :
(Berdasarkan jawaban @TrytoFly)
Catatan : Saya akan menganggap Anda bekerja pada modul yang sudah dibuat yang akan saya panggil Vendor_Module
.
is_default
kolom ke catalog_product_option_type_value
tabelapp / code / Vendor / Module / Setup / UpgradeSchema.php
<?php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
* @codeCoverageIgnore
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '2.0.1') < 0) {
$setup->getConnection()->addColumn(
$setup->getTable('catalog_product_option_type_value'),
'is_default',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
'length' => 1,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'Defines if Is Default'
]
);
}
}
}
Catatan : Jangan lupa untuk mengubah versi dibandingkan dengan modul Anda
app / code / Vendor / Module / etc / adminhtml / di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions">
<plugin name="vendor_module_custom_options_ui_plugin"
type="Vendor\Module\Plugin\CustomOptionsUiPlugin" />
</type>
</config>
app / code / Vendor / Module / Plugin / CustomOptionsUiPlugin.php
<?php
namespace Vendor\Module\Plugin;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions;
use Magento\Ui\Component\Form\Field;
use Magento\Ui\Component\Form\Element\Checkbox;
use Magento\Ui\Component\Form\Element\DataType\Number;
/**
* Data provider for "Customizable Options" panel
*/
class CustomOptionsUiPlugin
{
/**
* Field values
*/
const FIELD_IS_DEFAULT = 'is_default';
/**
* @param \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions $subject
* @param bool $meta
* @return bool
*/
public function afterModifyMeta(CustomOptions $subject, $meta) {
$result = $meta;
$result[CustomOptions::GROUP_CUSTOM_OPTIONS_NAME]['children']
[CustomOptions::GRID_OPTIONS_NAME]['children']['record']['children']
[CustomOptions::CONTAINER_OPTION]['children']
[CustomOptions::GRID_TYPE_SELECT_NAME]['children']['record']['children']
[static::FIELD_IS_DEFAULT] = $this->getIsDefaultFieldConfig(70);
return $result;
}
/**
* Get config for checkbox field used for default values
*
* @param int $sortOrder
* @return array
*/
protected function getIsDefaultFieldConfig($sortOrder)
{
return [
'arguments' => [
'data' => [
'config' =>[
'label' => __('Default'),
'componentType' => Field::NAME,
'formElement' => Checkbox::NAME,
'dataScope' => static::FIELD_IS_DEFAULT,
'dataType' => Number::NAME,
'additionalClasses' => 'admin__field-small',
'sortOrder' => $sortOrder,
'value' => '0',
'valueMap' => [
'true' => '1',
'false' => '0'
]
],
],
],
];
}
}
Catatan : Di sini kita menggunakan Magento\Ui\Component\Form\Element\Checkbox
bukan Magento\Ui\Component\Form\Element\Radio
komponen karena sepertinya Magento tidak pernah mendefinisikannya dalam Elemen Bentuknya.
Lihat vendor\magento\module-ui\view\base\ui_component\etc\definition.xml
baris 112+
Magento\Catalog\Block\Product\View\Options\Type\Select
untuk memeriksa elemen yang telah dipilih sebagai "Elemen default".app / code / Vendor / Module / etc / adminhtml / di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\View\Options\Type\Select"
type="Vendor\Module\Block\Rewrite\Catalog\Product\View\Options\Type\Select" />
</config>
app / kode / Vendor / Modul / Blok / Tulis Ulang / Katalog / Produk / Lihat / Opsi / Jenis / Select.php
<?php
namespace Vendor\Module\Block\Rewrite\Catalog\Product\View\Options\Type;
/**
* Product options text type block
*/
class Select extends \Magento\Catalog\Block\Product\View\Options\Type\Select
{
/**
* Return html for control element
*
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getValuesHtml()
{
$_option = $this->getOption();
$configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
$store = $this->getProduct()->getStore();
$this->setSkipJsReloadPrice(1);
// Remove inline prototype onclick and onchange events
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN ||
$_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE
) {
$require = $_option->getIsRequire() ? ' required' : '';
$extraParams = '';
$select = $this->getLayout()->createBlock(
\Magento\Framework\View\Element\Html\Select::class
)->setData(
[
'id' => 'select_' . $_option->getId(),
'class' => $require . ' product-custom-option admin__control-select'
]
);
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN) {
$select->setName('options[' . $_option->getId() . ']')->addOption('', __('-- Please Select --'));
} else {
$select->setName('options[' . $_option->getId() . '][]');
$select->setClass('multiselect admin__control-multiselect' . $require . ' product-custom-option');
}
foreach ($_option->getValues() as $_value) {
$priceStr = $this->_formatPrice(
[
'is_percent' => $_value->getPriceType() == 'percent',
'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
],
false
);
if($_value->getData('is_default') == true && !$configValue){
$configValue = $_value->getOptionTypeId();
}
$select->addOption(
$_value->getOptionTypeId(),
$_value->getTitle() . ' ' . strip_tags($priceStr) . '',
['price' => $this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false)]
);
}
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE) {
$extraParams = ' multiple="multiple"';
}
if (!$this->getSkipJsReloadPrice()) {
$extraParams .= ' onchange="opConfig.reloadPrice()"';
}
$extraParams .= ' data-selector="' . $select->getName() . '"';
$select->setExtraParams($extraParams);
if ($configValue) {
$select->setValue($configValue);
}
return $select->getHtml();
}
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO ||
$_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX
) {
$selectHtml = '<div class="options-list nested" id="options-' . $_option->getId() . '-list">';
$require = $_option->getIsRequire() ? ' required' : '';
$arraySign = '';
switch ($_option->getType()) {
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO:
$type = 'radio';
$class = 'radio admin__control-radio';
if (!$_option->getIsRequire()) {
$selectHtml .= '<div class="field choice admin__field admin__field-option">' .
'<input type="radio" id="options_' .
$_option->getId() .
'" class="' .
$class .
' product-custom-option" name="options[' .
$_option->getId() .
']"' .
' data-selector="options[' . $_option->getId() . ']"' .
($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
' value="" checked="checked" /><label class="label admin__field-label" for="options_' .
$_option->getId() .
'"><span>' .
__('None') . '</span></label></div>';
}
break;
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX:
$type = 'checkbox';
$class = 'checkbox admin__control-checkbox';
$arraySign = '[]';
break;
}
$count = 1;
foreach ($_option->getValues() as $_value) {
$count++;
$priceStr = $this->_formatPrice(
[
'is_percent' => $_value->getPriceType() == 'percent',
'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
]
);
$htmlValue = $_value->getOptionTypeId();
if ($arraySign) {
$checked = is_array($configValue) && in_array($htmlValue, $configValue) ? 'checked' : '';
} else {
$checked = $configValue == $htmlValue ? 'checked' : '';
}
$dataSelector = 'options[' . $_option->getId() . ']';
if ($arraySign) {
$dataSelector .= '[' . $htmlValue . ']';
}
$selectHtml .= '<div class="field choice admin__field admin__field-option' .
$require .
'">' .
'<input type="' .
$type .
'" class="' .
$class .
' ' .
$require .
' product-custom-option"' .
($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
' name="options[' .
$_option->getId() .
']' .
$arraySign .
'" id="options_' .
$_option->getId() .
'_' .
$count .
'" value="' .
$htmlValue .
'" ' .
$checked .
' data-selector="' . $dataSelector . '"' .
' price="' .
$this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false) .
'" />' .
'<label class="label admin__field-label" for="options_' .
$_option->getId() .
'_' .
$count .
'"><span>' .
$_value->getTitle() .
'</span> ' .
$priceStr .
'</label>';
$selectHtml .= '</div>';
}
$selectHtml .= '</div>';
return $selectHtml;
}
}
}
Meng-upgrade setup_version
diapp/code/Vendor/Module/etc/module.xml
Update version
di app/code/Vendor/Module/composer.json
Jalankan perintah berikut:
php bin/magento cache:clean
php bin/magento setup:upgrade