2017-08-13 161 views
0

我希望这是一个非常简单的问题,只能通过我的newP状态创建到PhP和Magento。如何将属性添加到Magento 2.1.x产品页面

问题:鉴于下面描述的细节,在catalog_product_view.xml的第一个属性中。这应该是什么,以确保我得到我的自定义属性gsc_payment?我可以使用像UPC这样的内置属性来处理所有事情,但不能使用我的自定义属性。

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

  2. 我从 /home/XXXXXXXX/public_html/app/design/frontend/Venustheme/gosmart/Magento_Catalog/layout/catalog_product_view.xml打开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> 
    

回答

0

您可以使用安装/ 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(); 
    } 

} 

?> 
创建自定义属性
相关问题