1:1 relationship with woocommerce product types

Support MB Relationships 1:1 relationship with woocommerce product types

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #46943
    Imre LászlóImre László
    Participant

    Hello!

    There are some built-in product types in WooCommerce: simple, variable, external. I'd like to create a vica-versa relationship with one product to another, based on product type. The simple products should be paper-based books, while the external products should be books sold digitally (ebook).

    If I use this code, two META BOX apperas, and I can select the elements, but this is not OK, because I do not want to see tho boxes. When I set the 'reciprocal' setting to true, only one box appears, but the same on both product types. How can I solve it so that only the right one is displayed?

    \MB_Relationships_API::register([
      'id' => 'simple_product_to_external_product',
      'reciprocal' => false,
      'from' => [
        'object_type' => 'post',
        'post_type' => 'product',
        'meta_box' => [
          'title' => __('Paper based version', 'my-plugin-textdomain'),
          'context' => 'normal',
          'priority' => 'high'
        ],
        'field' => [
          'name' => __('Product', 'woocommerce'),
          'placeholder' => __('Select a paper based version', 'my-plugin-textdomain'),
          'query_args' => [
            'tax_query' => [['taxonomy' => 'product_type', 'field' => 'slug', 'terms' => 'external']]
          ],
          'max_clone' => 1,
          'js_options' => [
            'placeholder' => __('Select a product', 'my-plugin-textdomain')
          ]
        ]
      ],
      'to' => [
        'object_type' => 'post',
        'post_type' => 'product',
        'meta_box' => [
          'title' => __('Digital (eBook) version', 'my-plugin-textdomain'),
          'context' => 'normal',
          'priority' => 'high'
        ],
        'field' => [
          'name' => __('Product', 'woocommerce'),
          'placeholder' => __('Select a digital (eBook) version', 'my-plugin-textdomain'),
          'query_args' => [
            'tax_query' => [['taxonomy' => 'product_type', 'field' => 'slug', 'terms' => 'simple']]
          ],
          'max_clone' => 1,
          'js_options' => [
            'placeholder' => __('Select a product', 'my-plugin-textdomain')
          ]
        ]
      ]
    ]);
    #46944
    Imre LászlóImre László
    Participant

    I can make it work with the "MB Include Exclude" extension.

    Added this to the "from" side's "meta_box" array:

    'include' => ['product_type' => 'external']

    Added this to the "to" side's "meta_box" array:

    'include' => ['product_type' => 'simple']

    Is this the correct way to achieve this?

    #46967
    PeterPeter
    Moderator

    Hello,

    Yes, you can use the MB Include Exclude with MB Relationships to show the meta box based on a condition. However, it will include/exclude a meta box on loading so you will need to reload the editing page after updating the taxonomy for the product.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.