最近有个需求,需要在customizable options里面添加一个自定义输入框,供业务填写属性注释等内容,安排。。。
实际使用中,将vendor和module 改成对应文件夹路径即可
实际效果图:
0622】.png
首先往 catalog_product_option表中添加字段,因为我要兼容中英文,所以加了两个字段,笨办法,但是简单好用
app/code/vendor/module/etc/module.xml 升级模块版本,根据实际情况修改 我这里版本1.0.5
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="vendor_module" setup_version="1.0.5" />
</config>
app\code\vendor\module\Setup\UpgradeSchema.php
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
// 更新表 [catalog_product_option] 字段 添加option title备注提示信息
if (version_compare($context->getVersion(), '1.0.5', '<'))
{
$this->addFieldToProductOption($setup);
}
}
private function addFieldToProductOption(SchemaSetupInterface $setup)
{
$optionsManageTable = $setup->getTable('catalog_product_option');
$optionsConnection = $setup->getConnection();
if ($optionsConnection->isTableExists($optionsManageTable) == true)
{
$optionsConnection->addColumn($optionsManageTable, 'option_msg', [
'type' => Table::TYPE_TEXT,
'nullable' => true,
'default' => null,
'comment' => 'option_msg 中文'
]);
$optionsConnection->addColumn($optionsManageTable, 'option_msg_en', [
'type' => Table::TYPE_TEXT,
'nullable' => true,
'default' => null,
'comment' => 'option_msg 英文'
]);
}
unset($optionsManageTable, $optionsConnection);
}
编辑di.xml
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_Plugin_Magento_Catalog_Ui_DataProvider_Product_Form_Modifier_CustomOptions" type="vendor\module\Plugin\Product\Options" sortOrder="10" disabled="false"/>
</type>
</config>
添加plugin文件
app/code/vendor/module/Plugin/Product/Options.php
<?php
declare(strict_types=1);
namespace vendor\module\Plugin\Product;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions;
use Magento\Ui\Component\Form\Element\Input;
use Magento\Ui\Component\Form\Element\DataType\Number;
use Magento\Ui\Component\Form\Field;
class Options
{
/**
* @var array
*/
protected $meta = [];
public function aroundModifyMeta(
CustomOptions $subject,
\Closure $proceed,
$meta
) {
\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('aroundModifyMeta Plugin2' );
$this->meta = $proceed($meta);
$this->addFields();
return $this->meta;
}
/**
* Adds fields to the meta-data
*/
protected function addFields()
{
$groupCustomOptionsName = CustomOptions::GROUP_CUSTOM_OPTIONS_NAME;
$optionContainerName = CustomOptions::CONTAINER_OPTION;
$commonOptionContainerName = CustomOptions::CONTAINER_COMMON_NAME;
// Add fields to the option
$optionFeaturesFields = $this->getOptionGtinFieldsConfig();
$this->meta[$groupCustomOptionsName]['children']['options']['children']['record']['children']
[$optionContainerName]['children'][$commonOptionContainerName]['children'] = array_replace_recursive(
$this->meta[$groupCustomOptionsName]['children']['options']['children']['record']['children']
[$optionContainerName]['children'][$commonOptionContainerName]['children'],
$optionFeaturesFields
);
}
/**
* The custom option fields config
*
* @return array
*/
protected function getOptionGtinFieldsConfig()
{
$fields['gtin'] = $this->getGtinFieldConfig();
return $fields;
}
/**
* Get gtin field config
*
* @return array
*/
protected function getGtinFieldConfig()
{
$storeId = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
// \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('当前storeID' .$storeId);
$dataScope = in_array($storeId, [0,1]) ? 'option_msg' : 'option_msg_en';//如果是默认视图,取中文,否则取英文字段
return [
'arguments' => [
'data' => [
'config' => [
'label' => __('Option Msg'),
'componentType' => Field::NAME,
'formElement' => Input::NAME,
'dataType' => Number::NAME,
'dataScope' => $dataScope,
'sortOrder' => 65
],
],
],
];
}
/**
* Check is current modifier for the product only
*
* @return bool
*/
public function isProductScopeOnly()
{
return false;
}
/**
* Get sort order of modifier to load modifiers in the right order
*
* @return int
*/
public function getSortOrder()
{
return 32;
}
}
(完)