我已在该创建的字段中创建了自定义选项卡,但在尝试保存字段值时,因此未在该字段上保存值,并且在发布值中未显示我的自定义选项卡字段,并且我已使用此事件catalog_product_save_after任何人都知道如何解决这个问题吗?
**这是我的 ui_component ** `
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="pricematrix">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Price Matrix</item>
<item name="provider" xsi:type="string">product</item>
<item name="sortOrder" xsi:type="number">10</item>
<item name="collapsible" xsi:type="boolean">true</item>
</item>
</argument>
<container name="_custom_tab">
<htmlContent name="html_content">
<block class="Test\PriceMatrix\Block\Adminhtml\Product\Edit\CustomTab" name="price_custom_tab" template="Test_PriceMatrix::customtab.phtml"/>
</htmlContent>
</container>
</fieldset>
</form>
**这是我的模板文件 **
<div class="pricematrix-custom-tab-content">
<div class="column">
<div class="field">
<label class="label">Base Price</label>
</div>
<?php for ($i = 1; $i <= 5; $i++): ?>
<input type="hidden" name="base_price[]" id="base_price_<?= $i ?>" value="">
<div class="field">
<div class="control">
<input type="text" name="[display_base_price][]" id="display_base_price_<?= $i ?>" class="input-text">
</div>
</div>
<?php endfor; ?>
</div>
<div class="column">
<div class="field">
<label class="label">Quantity</label>
</div>
<?php for ($i = 1; $i <= 5; $i++): ?>
<input type="hidden" name="qty[]" id="qty_<?= $i ?>" value="">
<div class="field">
<div class="control">
<input type="text" name="[display_qty][]" id="display_qty_<?= $i ?>" class="input-text">
</div>
</div>
<?php endfor; ?>
</div>
</div>
<style>
.pricematrix-custom-tab-content {
display: flex;
}
.column {
padding: 0 15px;
}
.field {
margin-bottom: 15px;
}
.label {
display: block;
margin-bottom: 5px;
}
.control {
margin-bottom: 15px;
}
</style>
**这是我的观察者文件
<?php
namespace Test\PriceMatrix\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\Product;
class SavePriceMatrix implements ObserverInterface
{
protected $request;
public function __construct(
\Magento\Framework\App\RequestInterface $request
) {
$this->request = $request;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$product = $observer->getEvent()->getProduct();
$postData = $this->request->getPostValue();
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/test.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info("Post Data: " . json_encode($postData));
if (isset($postData['display_base_price']) && isset($postData['display_qty'])) {
$displayBasePrice = $postData['display_base_price'];
$displayQty = $postData['display_qty'];
$processedBasePrice = json_encode($displayBasePrice);
$processedQty = json_encode($displayQty);
$product->setData('display_base_price', $processedBasePrice);
$product->setData('display_qty', $processedQty);
}
}
}
我已经尝试过此代码并检查了日志,但没有在发布值日志中显示我的自定义选项卡字段。
请交叉验证自定义数据是否确实保存在数据库中。如果自定义数据保存在数据库中,则问题可能出在显示上。在这种情况下,您需要使用插件来渲染保存的数据。