1:1 relationship with woocommerce product types
Support › MB Relationships › 1:1 relationship with woocommerce product types
- This topic has 2 replies, 2 voices, and was last updated 5 months, 2 weeks ago by
Peter.
-
AuthorPosts
-
November 17, 2024 at 3:19 AM #46943
Imre László
ParticipantHello!
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') ] ] ] ]);
November 17, 2024 at 3:36 AM #46944Imre László
ParticipantI 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?
November 19, 2024 at 10:31 PM #46967Peter
ModeratorHello,
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.
-
AuthorPosts
- You must be logged in to reply to this topic.