Magento – Magento 2: How to show the custom option in another phtml file

javascriptmagento2PHP

How can we override this position in custom option to show it in another phtml file.
enter image description here

added this code in checkout_cart_index.xml which override only the form.phtml not override the default.phtml file.

  <?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>

i am getting error regarding the checkout.cart.item.renderers already has a child with alias 'simple' i have modify that file of 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>

to override the file we useed 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>

Best Answer

Put the code in any other phtml page in product collection

custome option willl show on dropdown , select , multiselect , etc...

<?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; ?>