如何将属性添加到Magento 2.1.x产品页面



我希望这是一个非常简单的问题,只有我对PhP和Magento的新手身份才能创建。

问题:鉴于下面描述的详细信息,在catalog_product_view.xml第一个属性中。 这应该是什么来确保我获得gsc_payment自定义属性? 我可以使用 UPC 等内置属性进行所有工作,但不能使用我的自定义属性。

  1. 我创建了一个新属性,并使其在Magento的产品页面上可见。 属性代码:gsc_payment 默认标签:ARC 90 付款

  2. 我打开了catalog_product_view.xml/home/XXXXXXXX/public_html/app/design/frontend/Venustheme/gosmart/Magento_Catalog/layout/catalog_product_view.xml 并添加了如下所示的 referenceContainer。

    <referenceContainer name="product.info.main">
    <block  class="MagentoCatalogBlockProductViewDescription" 
    name="product.info.gsc" 
    template="Magento_Catalog::product/view/gsc.phtml" 
    after="product.info.upc">
    <arguments>
    <argument name="at_call" xsi:type="string">getGSC_Payment</argument>
    <argument name="at_code" xsi:type="string">gsc_payment</argument>
    <argument name="css_class" xsi:type="string">gsc_payment</argument>
    <argument name="at_label" xsi:type="string">Arc 90 Payment:</argument>
    <argument name="add_attribute" xsi:type="string">itemprop="gsc_payment"</argument>
    </arguments>
    </block>
    </referenceContainer>
    
    
    <referenceContainer name="product.info.main">
    
    <block class="VesThemesettingsBlockProductView" name="ves.product.info.main" template="Magento_Catalog::product/view/product_info_main.phtml">
    <move element="product.info.sku" as="product_info_sku" destination="ves.product.info.main"/>
    <move element="product.info.review" as="product_info_review" destination="ves.product.info.main"/>
    <move element="product.price.final" as="product_price_final" destination="ves.product.info.main"/>
    <move element="product.price.tier" as="product_price_tier" destination="ves.product.info.main"/>
    <move element="alert.urls" as="alert_urls" destination="ves.product.info.main"/>
    <move element="product.info" as="product_info" destination="ves.product.info.main"/>
    <move element="product.info.overview" as="product_info_overview" destination="ves.product.info.main"/>
    <move element="require-cookie" as="require_cookie" destination="ves.product.info.main"/>
    <move element="product.info.extrahint" as="product_info_extrahint" destination="ves.product.info.main"/>
    <move element="product.info.type" as="product_info_type" destination="ves.product.info.main"/>
    </block>
    
    </referenceContainer>
    <container name="product.info.extrahint" as="extrahint" label="Product View Extra Hint">
    <container name="product.info.social" label="Product social links container" after="product.info.overview">
    <block class="MagentoCatalogBlockProductView" name="product.info.categories" template="product/view/categories.phtml"/>
    </container>
    
  3. 我在这里创建了一个新文件/home/XXXXXXXXX/public_html/vendor/magento/module-catalog/view/frontend/templates/product/view/gsc.phtml,其中包含从互联网复制的以下块代码。

    <?php
    $_helper = $this->helper('MagentoCatalogHelperOutput');
    $_product = $block->getProduct();
    $_code = $block->getAtCode();
    $_className = $block->getCssClass();
    $_attributeLabel = $block->getAtLabel();
    $_attributeType = $block->getAtType();
    $_attributeAddAttribute = $block->getAddAttribute();
    if ($_attributeLabel && $_attributeLabel == 'default') {
    $_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
    }
    $_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
    ?>
    <?php if ($_attributeValue): ?>
    <div class="product attibute <?php echo $_className?>">
    <?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?></strong><?php endif; ?>
    <div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
    </div>
    <?php endif; ?>
    
  4. 然后我打开/home/XXXXXXXXXXXX/public_html/app/design/frontend/Venustheme/gosmart/Magento_Catalog/templates/product/view/product_info_main.phtml 并在现有的 .

    <div>
    <table style="width: 100%; border-color: #ac1a2f; border-style: solid; border-width: 2px;">
    <tbody>
    <tr>
    <td>
    <p style="color: red; font-size: 300%; text-align: center;">Buy Today!</p>
    <div style="color: green; font-size: 400%; text-align: center;">$
    <?php echo $this->getChildHtml('product_info_gsc_payment') ?>
    <?php echo "Hello"?>
    <?php $_product = $block->getProduct();
    echo $_product->getPrice();
    echo $_product->getAttributeText('color');
    ?>
    </div>
    <p style="font-size: 150%; text-align: center;">bi-weekly/12 months</p>
    <p style="color: red; font-size: 150%; text-align: center;">90 Days Same As Cash</p>
    <p style="text-align: center;">20-25% Down Payment Required</p>
    <p style="text-align: center;">Estimated Payment</p>
    </td>
    <td>
    <div style="text-align: center;">
    <p><img style="width: 25%; height: 25%;" src="https://1792armory.com/public/firearms/arc90_logo_big.png" border="0" /></p>
    <p><a href="https://mail.globalcheck.com/cgi-bin/sendprequal.cgi?custid=149209368937458751" target="_blank" rel="nofollow"> <img style="align: center;" src="https://mail.globalcheck.com/images/qualify1.jpg" border="0" /> </a></p>
    </div>
    </td>
    </tr>
    </tbody>
    </table>
    

您可以使用Setup/upgradeData 创建自定义属性.php

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace CustomProductSetup;
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoEavModelEntityAttributeScopedAttributeInterface;
class UpgradeData implements UpgradeDataInterface {
public function __construct(MagentoEavSetupEavSetupFactory $eavSetupFactory) {
$this->eavSetupFactory = $eavSetupFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

if (version_compare($context->getVersion(), '2.1.24') < 0) {
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY, 'product_height', [
'type' => 'varchar',
'label' => 'Product Height in cm',
'input' => 'text',
'required' => false,
'class' => '',
'backend' => '',
'source' => '',
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'filterable_in_search' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'group' => 'General',
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => true,
]
);

}
$setup->endSetup();
}
}
?>

最新更新