Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
81 stars 29 forks source link

Fix issues where edit product fields were incorrectly being hidden if they had the show_if_variable_subscription class #529

Closed james-allan closed 8 months ago

james-allan commented 8 months ago

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.

$( '.hide_if_variable-subscription' ).show();
$( '.show_if_variable-subscription' ).hide();

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:

  1. Removing that code I mentioned above.
  2. 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.
  3. 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

  1. Create products, change the product type, and create variations and make sure there aren't product fields shown or hidden incorrectly.
  2. 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)

  1. Add the following code to your site.
  2. Create a new product, choose the simple product type.
  3. On trunk you'll notice on the Inventory tab that there's no Dummy Checkbox field.
  4. On this branch there should be - note the show_if_simple.
  5. Change the product type to variable-subscription.
  6. On trunk and this branch the field would be shown.
  7. 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).
add_action( 'woocommerce_product_options_stock_status', 'sw_add_dummy_checkbox', 20 );
function sw_add_dummy_checkbox() {

    global $product_object;
    if ( ! is_a( $product_object, 'WC_Product' ) ) {
        return;
    }

    woocommerce_wp_checkbox(
        array(
            'id'            => '_wc_dummy_checkbox',
            'label'         => __( 'Dummy Checkbox', 'somewherewarm' ),
            'value'         => 'yes',
            'wrapper_class' => 'form-field show_if_simple  show_if_variable-subscription',
            'description'   => __( 'Dummy Checkbox Description', 'somewherewarm' ),
        )
    );
}

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.

  1. Add the code snippet provided below to your site.
  2. On this branch create a product.
  3. Choose "New Variable" - the product type added by the code snippet.
  4. Add at least 1 attribute.
  5. Create variations.
  6. Confirm that the variation product fields don't include any subscription product fields.
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)

  1. Install WP Job Manager
  2. Go to Products → Add new
  3. Select Job Package Subscription product type.
  4. Go to Advanced tab.
  5. On trunk you'll notice there's on Limit subscription field.
  6. On this branch there should be.

Product impact