วิธีเปลี่ยนแอตทริบิวต์ที่กำหนดเองเป็นแอตทริบิวต์คำอธิบายใน Magento 2
วิธีสร้างcustom attribute value
แทนที่เป็น description attribute
.
เช่น - แอตทริบิวต์ที่กำหนดเองของฉันค่าทั้งหมดแทนที่แท็บรายละเอียดผลิตภัณฑ์หมายถึงแอตทริบิวต์คำอธิบาย
local_description
แอตทริบิวต์ที่กำหนดเองของฉันคือ
นี่คือรหัสของฉัน: -
<?php
$_helper = $this->helper(Magento\Catalog\Helper\Output::class);
$_product = $block->getProduct();
if (!$_product instanceof \Magento\Catalog\Model\Product) { return; } $_call = $block->getAtCall(); $_code = $block->getAtCode(); $_className = $block->getCssClass(); $_attributeLabel = $block->getAtLabel(); $_attributeType = $block->getAtType(); $_attributeAddAttribute = $block->getAddAttribute(); $renderLabel = true;
// if defined as 'none' in layout, do not render
if ($_attributeLabel == 'none') { $renderLabel = false;
}
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getStoreLabel(); } if ($_attributeType && $_attributeType == 'text') { $_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code)) ? $_product->getAttributeText($_code) : ''; } else { if($_code == 'description')
{
$_code = 'local_description'; } $_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code); } ?> <?php if ($_attributeValue) :?>
<div class="product attribute <?= $block->escapeHtmlAttr($_className) ?>">
<?php if ($renderLabel) :?> <strong class="type"><?= $block->escapeHtml($_attributeLabel) ?></strong> <?php endif; ?> <div class="value" <?= /* @noEscape */ $_attributeAddAttribute ?>><?= /* @noEscape */ $_attributeValue ?></div>
</div>
<?php endif; ?>
THANKS.
คำตอบ
คุณสามารถลองทำสิ่งต่อไปนี้
สร้างcatalog_product_view.xml
ในโมดูลหรือธีมของคุณ
และเพิ่มรหัสต่อไปนี้
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.description">
<arguments>
<argument name="at_call" xsi:type="string">getLocalDescription</argument>
<argument name="at_code" xsi:type="string">local_description</argument>
</arguments>
</referenceBlock>
</body>
</page>
นี่เป็นเพียงตัวอย่างโค้ดเท่านั้นปรับเปลี่ยนตามความต้องการของคุณ
สวัสดีไม่มีความรู้เพียงพอตามความเข้าใจของฉันกำลังโพสต์สิ่งนี้และจะเพิ่มฟิลด์คำอธิบายการทดสอบใหม่พร้อมกับคำอธิบายของคุณโปรดแจ้งให้เราทราบว่านี่คือสิ่งที่คุณต้องการหรืออื่น ๆ โปรดอัปเดตคำถามของคุณพร้อมรายละเอียดเพิ่มเติม
ตั้งค่า / InstallData.php
<?php
namespace Vendor\Module\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory; /** * Init * * @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { /** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); /** * Add attributes to the eav_attribute */ $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'test_descp');
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'test_descp'); // $statusOptions = 'Rbj\ProductAttribute\Model\Config\Source\StatusOptions';
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'test_descp',
[
'group' => 'Content',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'New Test Description',
'input' => 'text',
'class' => '',
'source' => '',
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'is_used_in_grid' => true,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false
]
);
}
}
ปรับปรุง: -
วางโค้ดนี้ไว้ในdafault.xml ของคุณในโมดูลที่กำหนดเอง
เพื่อลบแท็บ "ข้อมูลเพิ่มเติม":
<referenceBlock name="product.attributes" remove="true" />
หากคุณต้องการลบแท็บ "รายละเอียด":
<referenceBlock name="product.info.details" remove="true" />