I need a new custom option type which extends from select/dropdown, so I can preload my options from a database dynamically. I did not find any tutorial or documentation how I would do something like that. Can anybody help me out?
EDIT:
My backend template doesn't get loaded properly. Although I can select my option type, the template paper.phtml
does not appear.
What I got so far:
MyVendor/MyModule/etc/product-options.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_options.xsd">
<option name="MyOptions" label="MyOptions" renderer="MyVendor\MyModule\Block\Adminhtml\Product\Edit\Tab\Options\Type\Paper">
<inputType name="Paper" label="Paper" />
</option>
</config>
MyVendor\MyModule\Block\Adminhtml\Product\Edit\Tab\Options\Type\Paper.php
<?php
namespace MyVendor\MyModule\Block\Adminhtml\Product\Edit\Tab\Options\Type;
class Paper extends \Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Options\Type\Select
{
protected $_template = 'MyVendor_MyModule::catalog/product/edit/options/type/paper.phtml';
/**
* Class constructor
*
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->setTemplate($_template);
}
}
MyVendor/MyModule/view/adminhtml/templates/catalog/product/edit/options/type/paper.phtml
which is a exact copy of magento/catalog/view/adminhtml/templates/catalog/products/edit/options/type/select.phtml
Best Answer
Our team just recently went through this. In fact, one of our Ace member solved this. So this one is tricky because it nests so close to Magento's core. The best way to go about this is to start with living in vendor/magento/module-catalog
A few sub directories that come to mind would be:
Now for the fun part!
In your modules etc, create a product_options.xml and add in something like this:
This is the building blocks and mayhap all you'll need moving forward.
With that said, in our build, we required more advanced functionality. This functionality being a custom key to prevent collisions with default key values. We also needed to save them onto a separate table to prevent any undesirables if someone ever removes the module.
If you are looking to add more customization to your implementation, you'll want to take advantage of plugins and you'll want to add something like we have below:
Here is a sample of the First plugin CustomOptionPlugin.php. In some cases you may not need to make any changes here, but if you are looking to build custom option inputs you may have to inject them. You'll want the inner file to be something like the following:
Depending on your approach and end goal, you may need to use the second and third plugin as well. Feel free to ask any other questions you have and I will do my best to answer them.
Edit w/ addition info for the afterModifyMeta method listed above. Here you'll see the result parameter, this has the values needed to render the code. If you take advantage of var_dump within afterModifyMeta on results parameter using a Magento default option type, you can see how they structure it as well. Here is an example below: