On the edit product page, product fields added by third-party plugins (like WP Job manager) that have the show_if_variable-subscription class were being hidden incorrectly.
The reason this occurs is because we had code (see below) which had the effect of hiding all show_if_variable-subscription and then only showing them, if the product had a variable-subscription type.
If a product field has two "show if" criteria (eg show_if_variable-subscription and show_if_simple), this code would cause it to be hidden for all product types except variable subscriptions. ie, not shown for simple products.
This PR fixes that by:
Removing that code I mentioned above.
Removing code that handled (standard) variable products. We should only handle our product types and as far as I could tell, this wasn't necessary because WC hides and shows these fields automatically and we didn't need it.
Hiding the show_if_variable-subscription fields on load and then rely on them being shown when the product type matches. This was necessary because it's currently not possible to hide these fields after the fact (ie when creating variations) because doing so would require us to do a $( '.show_if_variable-subscription' ).hide(); and that reintroduces the issue.
How to test this PR
Standard tests
Create products, change the product type, and create variations and make sure there aren't product fields shown or hidden incorrectly.
Edit an existing product, change the product type, and create variations and make sure there aren't product fields shown or hidden incorrectly.
Custom field (#190)
Add the following code to your site.
Create a new product, choose the simple product type.
On trunk you'll notice on the Inventory tab that there's no Dummy Checkbox field.
On this branch there should be - note the show_if_simple.
Change the product type to variable-subscription.
On trunk and this branch the field would be shown.
Change the product type to any product type and verify that on this branch it is only shown for variable subscriptions, simple and simple subscriptions (simple subscriptions inherit simple fields).
Fixes #190 Fixes https://github.com/woocommerce/woocommerce-subscriptions/issues/4313 Fixes https://github.com/woocommerce/woocommerce-subscriptions/issues/4352
Description
On the edit product page, product fields added by third-party plugins (like WP Job manager) that have the
show_if_variable-subscription
class were being hidden incorrectly.The reason this occurs is because we had code (see below) which had the effect of hiding all
show_if_variable-subscription
and then only showing them, if the product had a variable-subscription type.If a product field has two "show if" criteria (eg
show_if_variable-subscription
andshow_if_simple
), this code would cause it to be hidden for all product types except variable subscriptions. ie, not shown for simple products.This PR fixes that by:
show_if_variable-subscription
fields on load and then rely on them being shown when the product type matches. This was necessary because it's currently not possible to hide these fields after the fact (ie when creating variations) because doing so would require us to do a$( '.show_if_variable-subscription' ).hide();
and that reintroduces the issue.How to test this PR
Standard tests
Custom field (#190)
trunk
you'll notice on the Inventory tab that there's no Dummy Checkbox field.show_if_simple
.trunk
and this branch the field would be shown.Custom product type (https://github.com/woocommerce/woocommerce-subscriptions/pull/4215)
The original changes removed by this PR were introduced in https://github.com/woocommerce/woocommerce-subscriptions/pull/4215 and were designed to fix an issue with subscription fields being shown on custom product types.
code
```php /** * Add option to product type selector. */ function kia_new_variable_product_type( $product_types ){ $product_types[ 'new-variable' ] = 'New Variable'; return $product_types; } add_filter( 'product_type_selector', 'kia_new_variable_product_type' ); /** * Define new product class. */ function kia_create_new_product_class(){ class WC_Product_New_Variable extends WC_Product_Variable { public function __construct( $product ) { $this->product_type = 'new-variable'; parent::__construct( $product ); } public function get_type() { return 'new-variable'; // so you can use $product = wc_get_product(); $product->get_type() } } } add_action( 'woocommerce_loaded', 'kia_create_new_product_class' ); /** * Custom JS for metaboxes. */ function kia_producttype_custom_js() { wp_add_inline_script( 'wc-admin-product-meta-boxes', ' jQuery( function( $ ) { $( ".variations_tab" ).addClass( "show_if_new-variable" ); $( document.body ).on( "woocommerce_added_attribute", function() { $( ".enable_variation" ).addClass( "show_if_new-variable" ); if ( "new-variable" === $( "select#product-type" ).val() ) { $( ".enable_variation" ).show(); } }); } ); ', 'before' ); } add_action( 'admin_enqueue_scripts', 'kia_producttype_custom_js', 99 ); /** * Use existing data store. */ function kia_new_variable_data_store( $stores ) { $stores['product-new-variable'] = 'WC_Product_Variable_Data_Store_CPT'; return $stores; } add_filter( 'woocommerce_data_stores', 'kia_new_variable_data_store' ); ```
WP Job manager (https://github.com/woocommerce/woocommerce-subscriptions/issues/4313)
trunk
you'll notice there's on Limit subscription field.Product impact