2

I've been working on this problem for a while and I'm not much closer to a proper solution for this. I'm at a stage where I think I can probably hack something together, but I feel there should be a better way of doing it that I just haven't found yet. Hopefully someone might have some pointers.

What I'm trying to do is add a product add-to-cart form to another frontend page. The issue is that I need that form to have all the appropriate bells and whistles associated with the product.

  • If it's a bundle product, it needs to have bundle option fields
  • If it's configurable, it needs to have the configurable option fields
  • If it has custom options, it needs to have custom option fields

It also needs to be able to be customised from the backend so that an admin user can dictate what products are displayed in this fashion.

What I have is:

  • a working add to cart form for simple products
  • the ability to dictate which products to display from the backend

What I don't have is:

  • the right way to show the product option fields

I thought the best way to go ahead with this would be to try and incorporate the same blocks and layouts that the product pages use. That way there is unity of display and functionality. However, I have tried this two ways and neither of them worked.

First, I tried adding the blocks as children via the _prepareLayout function in my custom block.

protected function _prepareLayout() {
    $childBlock = $this->getLayout()->createBlock('Magento\Catalog\Block\Product\View\Options\Type\DefaultType', 'product.info.options.default');
    $childBlock->setTemplate('Magento_Catalog::product/view/options/type/default.phtml');
    $this->addChild('default', $childBlock);
    $childBlock = $this->getLayout()->createBlock('Magento\Catalog\Block\Product\View\Options\Type\Text', 'product.info.options.text');
    $childBlock->setTemplate('Magento_Catalog::product/view/options/type/text.phtml');
    $this->addChild('text', $childBlock);
    $childBlock = $this->getLayout()->createBlock('Magento\Catalog\Block\Product\View\Options\Type\File', 'product.info.options.file');
    $childBlock->setTemplate('Magento_Catalog::product/view/options/type/file.phtml');
    $this->addChild('file', $childBlock);
    $childBlock = $this->getLayout()->createBlock('Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Select', 'product.info.bundle.options.select');
    $childBlock->setTemplate('Magento_Bundle::catalog/product/view/options/type/bundle/option/select.phtml');
    $this->addChild('select', $childBlock);
    $childBlock = $this->getLayout()->createBlock('Magento\Catalog\Block\Product\View\Options\Type\Date', 'product.info.options.date');
    $childBlock->setTemplate('Magento_Catalog::product/view/options/type/date.phtml');
    $this->addChild('date', $childBlock);
    $childBlock = $this->getLayout()->createBlock('Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Multi', 'product.info.bundle.options.,ulti');
    $childBlock->setTemplate('Magento_Bundle::catalog/product/view/options/type/bundle/option/multi.phtml');
    $this->addChild('multi', $childBlock);
    $childBlock = $this->getLayout()->createBlock('Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Radio', 'product.info.bundle.options.radio');
    $childBlock->setTemplate('Magento_Bundle::catalog/product/view/options/type/bundle/option/radio.phtml');
    $this->addChild('radio', $childBlock);
    $childBlock = $this->getLayout()->createBlock('Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Checkbox', 'product.info.bundle.options.checkbox');
    $childBlock->setTemplate('Magento_Bundle::catalog/product/view/options/type/bundle/option/checkbox.phtml');
    $this->addChild('checkbox', $childBlock);
    return parent::_prepareLayout();
}

Second I tried adding the blocks as children via layout update XML in the backend.

<referenceContainer name="content">
    <container name="Example.personalisation.main" htmlTag="div" htmlClass="personalisation" after="-">
        <block class="Example\Personalisation\Block\Form" name="Example.personalisation.form" template="Example_Personalisation::form.phtml">
            <arguments>
                <argument name="products" xsi:type="string">bundle-test,bundle-test2</argument>
            </arguments>
            <container name="product.info.form.content" as="product_info_form_content">
                <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart" as="addtocart" template="Magento_Catalog::product/view/addtocart.phtml"/>
            </container>
            <block class="Magento\Framework\View\Element\Template" name="product.info.form.options" as="options_container">
                <block class="Magento\Catalog\Block\Product\View" name="product.info.options.wrapper" as="product_options_wrapper" template="Magento_Catalog::product/view/options/wrapper.phtml">
                    <block class="Magento\Catalog\Block\Product\View\Options" name="product.info.options" as="product_options" template="Magento_Catalog::product/view/options.phtml">
                        <block class="Magento\Catalog\Block\Product\View\Options\Type\DefaultType" name="product.info.options.default" as="default" template="Magento_Catalog::product/view/options/type/default.phtml"/>
                        <block class="Magento\Catalog\Block\Product\View\Options\Type\Text" name="product.info.options.text" as="text" template="Magento_Catalog::product/view/options/type/text.phtml"/>
                        <block class="Magento\Catalog\Block\Product\View\Options\Type\File" name="product.info.options.file" as="file" template="Magento_Catalog::product/view/options/type/file.phtml"/>
                        <block class="Magento\Catalog\Block\Product\View\Options\Type\Select" name="product.info.options.select" as="select" template="Magento_Catalog::product/view/options/type/select.phtml"/>
                        <block class="Magento\Catalog\Block\Product\View\Options\Type\Date" name="product.info.options.date" as="date" template="Magento_Catalog::product/view/options/type/date.phtml"/>
                    <block class="Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Select" name="product.info.bundle.options.select" as="select"/>
                    <block class="Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Multi" name="product.info.bundle.options.multi" as="multi"/>
                    <block class="Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Radio" name="product.info.bundle.options.radio" as="radio"/>
                    <block class="Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Checkbox" name="product.info.bundle.options.checkbox" as="checkbox"/>
                    </block>
                    <block class="Magento\Framework\View\Element\Html\Calendar" name="html_calendar" as="html_calendar" template="Magento_Theme::js/calendar.phtml"/>
                </block>
                <block class="Magento\Catalog\Block\Product\View" name="product.info.options.wrapper.bottom" as="product_options_wrapper_bottom" template="Magento_Catalog::product/view/options/wrapper/bottom.phtml">
                    <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart.additional" as="product.info.addtocart" template="Magento_Catalog::product/view/addtocart.phtml"/>
                </block>
            </block>
        </block>
    </container>
</referenceContainer>

Neither of these has worked - the child blocks have remained inaccessible to the custom block.


I feel like I'm missing something obvious, and I don't want to have to clone all of the existing templates and functionality in order to get the right result.

Does anyone have any insight on the matter, or advice on how I can proceed?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.