Magento 2:如何在另一个phtml文件中显示自定义选项


14

我们如何在自定义选项中覆盖此位置,以在另一个phtml文件中显示它。 在此处输入图片说明

添加了此代码,checkout_cart_index.xml其中仅form.phtml覆盖不覆盖default.phtml文件。

  <?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">
<!-- <update handle="checkout_cart_item_renderers"/> -->
<body>
    <referenceContainer name="content">
        <referenceBlock name="checkout.cart.form">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Bespoke_Bespoke::cart/form.phtml</argument>
            </action>
    </referenceBlock> 
     <referenceBlock name="checkout.cart.item.renderers.override">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">Bespoke_Bespoke::cart/item/default.phtml</argument>
    </action>
</referenceBlock>
    </referenceContainer>
</body>

我收到有关checkout.cart.item.renderers已经有一个别名为'simple'的孩子的错误,我已经修改了该文件checkout_cart_item_renderer.xml

 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<!-- <update handle="checkout_item_price_renderers"/> -->
<body>
  <referenceBlock name="checkout.cart.form">
<action method="setOverriddenTemplates">
    <argument xsi:type="array">
        <!-- list override templates -->
        <item name="default" xsi:type="string">Bespoke_Bespoke::cart/item/default.phtml</item>
        <item name="simple" xsi:type="string">Bespoke_Bespoke::cart/item/default.phtml</item>
        <item name="virtual" xsi:type="string">Bespoke_Bespoke::cart/item/default.phtml</item>
    </argument>
  </action>
  </referenceBlock>
</body>
</page>

覆盖我们使用的文件 etc/di.xml

 <config  xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <preference for="Magento\Checkout\Block\Cart\AbstractCart" type="AGS\Bespoke\Block\Checkout\Cart\AbstractCart"/><preference for="Magento\Catalog\Block\Product\View\Option" type="AGS\Bespoke\Block\Product\View\Option"/>
   <preference for=" Magento\Checkout\Block\Cart" type="AGS\Bespoke\Block\Checkout\Cart"/>
   <preference for="\Magento\Checkout\Block\Cart\Item\Renderer" type="AGS\Bespoke\Block\Checkout\Cart\Item\Renderer"/>
   <virtualType name="AGS\Bespoke\Model\ResourceModel\Bespoke\Bespoke\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">     
  <arguments><argument name="mainTable" xsi:type="string">ags_bespoke</argument><argument name="resourceModel" xsi:type="string">AGS\Bespoke\Model\ResourceModel\Bespoke</argument> 

  </arguments></virtualType><type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments><argument name="collections" xsi:type="array">
<item name="bespoke_record_bespoke_list_data_source" xsi:type="string">AGS\Bespoke\Model\ResourceModel\Bespoke\Bespoke\Collection</item>


删除产品详细信息页面中的图块。将其添加到其他页面中
Visakh B Sujathan

3
您想达到什么目的?因为如果我错了,请纠正我,但是首先您需要一些选择才能将产品添加到购物车中。您不能先将具有必需选项的产品添加到购物车,然后再选择选项。
Giel Berkers '17

Answers:


5

将代码放在产品集合中的任何其他phtml页面中

custome选项willl显示在下拉菜单,select,multiselect等上。

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_productCollection = $block->getLoadedProductCollection();
?>
<?php foreach ($_productCollection as $_product): ?>
<?php
    $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());
    $customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($_product); 
?>
  <?php 
    foreach($customOptions as $option){
      $values = $option->getValues();
      if (empty($values)) { 
          continue;
      } ?>
      <div class="custome-option">
          <?php foreach($values as $value) :  ?>
              <span><?php echo $value->getTitle(); ?></span>
          <?php endforeach; ?>
      </div>
  <?php }; ?>
<?php endforeach; ?>

,得到错误:1个异常:异常#0(异常):警告:为foreach()提供了无效的参数
akgola
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.