Trying to create custom block type in orocommerce but I am getting: Could not load a block type "product_variants".

599 views Asked by At

Trying to create custom block type in orocommerce but I am getting: Could not load a block type "product_variants".

I have created block_types yml file in Resources/config/block_types.yml

  services:
       xngage_product.layout.block_type.product_variants:
       parent: oro_layout.block_type.abstract_configurable_container
       calls:
        - [setOptionsConfig, [{variants: {required: true}}]]
        - [setName, ['product_variants']]
    tags:
         - { name: layout.block_type, alias: product_variants }


xngage_product.layout.block_type_extension.product_variants:
    class: Xngage\ProductBundle\Layout\Extension\ProductVariantsExtension
    tags:
        - { name: layout.block_type_extension, alias: product_variants }

and created layout for configure product in Resources/layouts/xngage_theme/oro_product_frontend_product_view/configure_product.yml

layout:
imports:
    - oro_product_view

actions:
    - '@setBlockTheme':
        themes: 'configurable_product.html.twig'

    - '@add':
        id: product_variants
        blockType: product_variants
        parentId: product_specification_container
        siblingId: product_specification
        options:
            variants: '=data["product_variants"].getVariants(data["product"])'

conditions: 'context["product_type"] == "simple"'

any missing steps please?

2

There are 2 answers

2
Andrey Yatsenko On

Make sure you registered Resources/config/block_types.yml at the container. example of registration can be found here https://github.com/orocommerce/orocommerce/blob/master/src/Oro/Bundle/CMSBundle/DependencyInjection/OroCMSExtension.php#L25

0
Ibnab On

So with Oro layout system you have the ability to create you custom widget based on block So the class parent responsible is Oro\Bundle\LayoutBundle\Layout\Block\Type\ConfigurableType or you can create custom widget based on container by parent oro_layout.block_type.abstract_configurable or

oro_layout.block_type.abstract_configurable_container:
    oro_layout.block_type.abstract_configurable:
        abstract: true
        class: Oro\Bundle\LayoutBundle\Layout\Block\Type\ConfigurableType
    oro_layout.block_type.abstract_configurable_container:
        abstract: true
        parent: oro_layout.block_type.abstract_configurable
        calls:
            - [setParent, ['container']]

create block_types.yml inside Ibnab/Bundle/CustomWidgetBundle/Resources/config/block_types.yml and fill with:

ibnab_custom_widget.layout.type.product:
    parent:  oro_layout.block_type.abstract_configurable
    calls:
        - [setOptionsConfig, [{product_id: {}}]]
        - [setName, ['ibnab_custom_widget_product']]

Note don't forget to load your block_types.yml inside DependencyInjection/CustomWidgetExtension for example:

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load('block_types.yml');

Here yo have added a custom widget based on block widget which able to accept and option product_id and you can add for example is required

- [setOptionsConfig, [{product_id: {required: true}}]]

inside the layout handler oro_product_frontend_product_view (is folder with the route name of product page view which well get dispatched when you’re on product details page) , So you can fill the layout.yml for example with:

layout:
    actions:
        - '@setBlockTheme':
            themes: 'CustomWidgetBundle:layouts:blank/oro_product_frontend_product_view/layout.html.twig' 
        - '@add':
            id:  ibnab_custom_widget_product_direct
            parentId: product_view_primary_container
            blockType:  ibnab_custom_widget_product
            prepend: true
            options:
                product_id: '=data["product"].getId()'

Now you can use inside in your layout.twig with this option product_id

The full course OroCommerce Custom widget type based on block or container