diff --git a/assets/other-scripts/custom-product-options/index.js b/assets/other-scripts/custom-product-options/index.js new file mode 100644 index 0000000000..4394355b70 --- /dev/null +++ b/assets/other-scripts/custom-product-options/index.js @@ -0,0 +1,19 @@ +/* globals jQuery */ +( function ( $ ) { + if ( ! $ ) { + return; + } + $( '#variable_product_options' ).on( 'change', 'input.variable_is_virtual', function ( e ) { + $( e.currentTarget ) + .closest( '.woocommerce_variation' ) + .find( '.show_if_variation_virtual' ) + .hide(); + + if ( $( e.currentTarget ).is( ':checked' ) ) { + $( e.currentTarget ) + .closest( '.woocommerce_variation' ) + .find( '.show_if_variation_virtual' ) + .show(); + } + } ); +} )( jQuery ); diff --git a/includes/reader-revenue/woocommerce/class-woocommerce-products.php b/includes/reader-revenue/woocommerce/class-woocommerce-products.php index e119b9c9bb..776e52c899 100644 --- a/includes/reader-revenue/woocommerce/class-woocommerce-products.php +++ b/includes/reader-revenue/woocommerce/class-woocommerce-products.php @@ -21,6 +21,7 @@ class WooCommerce_Products { * @codeCoverageIgnore */ public static function init() { + \add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_enqueue_scripts' ] ); \add_filter( 'product_type_options', [ __CLASS__, 'show_custom_product_options' ] ); \add_action( 'woocommerce_variation_options', [ __CLASS__, 'show_custom_variation_options' ], 10, 3 ); \add_action( 'woocommerce_process_product_meta', [ __CLASS__, 'save_custom_product_options' ] ); @@ -28,6 +29,19 @@ public static function init() { \add_filter( 'woocommerce_order_item_needs_processing', [ __CLASS__, 'require_order_processing' ], 10, 2 ); } + /** + * Enqueue admin scripts. + */ + public static function admin_enqueue_scripts() { + wp_enqueue_script( + 'newspack-products-custom-options', + Newspack::plugin_url() . '/dist/other-scripts/custom-product-options.js', + [], + NEWSPACK_PLUGIN_VERSION, + true + ); + } + /** * Get custom product options. Product option values are always booleans but represented by string. * See: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/wc-formatting-functions.php#L37 @@ -38,7 +52,7 @@ public static function get_custom_options() { return [ 'newspack_autocomplete_orders' => [ 'id' => '_newspack_autocomplete_orders', - 'wrapper_class' => 'show_if_simple', + 'wrapper_class' => 'show_if_virtual', 'label' => __( 'Auto-complete orders', 'newspack-plugin' ), 'description' => __( 'Allow orders containing this product to automatically complete upon successful payment.', 'newspack-plugin' ), 'default' => 'yes', @@ -108,10 +122,10 @@ public static function show_custom_variation_options( $loop, $variation_data, $v foreach ( $custom_options as $option_key => $option_config ) { $meta_key = $option_config['id']; ?> -