【问题标题】:How To Add Attribute to Magento 2.1.x Product Page如何向 Magento 2.1.x 产品页面添加属性
【发布时间】:2017-08-13 18:17:13
【问题描述】:

我希望这是一个非常简单的问题,仅由我对 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="Magento\Catalog\Block\Product\View\Description" 
            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="Ves\Themesettings\Block\Product\View" 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="Magento\Catalog\Block\Product\View" 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('Magento\Catalog\Helper\Output');
       $_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/XXXXXXXXXXX/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>
    

【问题讨论】:

    标签: customization product custom-attributes magento2.1


    【解决方案1】:

    您可以使用 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 Custom\Product\Setup;
    
    use Magento\Framework\Setup\UpgradeDataInterface;
    use Magento\Eav\Setup\EavSetupFactory;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
    
    class UpgradeData implements UpgradeDataInterface {
    
        public function __construct(\Magento\Eav\Setup\EavSetupFactory $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(
                        \Magento\Catalog\Model\Product::ENTITY, 'product_height', [
                    'type' => 'varchar',
                    'label' => 'Product Height in cm',
                    'input' => 'text',
                    'required' => false,
                    'class' => '',
                    'backend' => '',
                    'source' => '',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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();
        }
    
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多