Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try: Add product editor hooks for testing variation extensibility #562

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions assets/js/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1419,4 +1419,17 @@ jQuery( function ( $ ) {
$( '.woo_subscriptions_empty_state__button_container a' ).on( 'click', function ( e ) {
$( this ).addClass( 'is-busy' );
} );

if ( window.wp.hooks && wp.data ) {
window.wp.hooks.addFilter( 'woocommerce_save_product_data', 'woocommerce-subscriptions', ( data, productId ) => {
const product = wp.data.select( 'core' ).getEditedEntityRecord( 'postType', 'product', productId );
if ( data.type === 'variable' && product.is_subscribable ) {
return {
...data,
type: 'variable-subscription'
}
}
return data;
} );
}
} );
33 changes: 33 additions & 0 deletions includes/admin/class-wc-subscriptions-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public static function init() {
// Add subscription pricing fields on edit product page
add_action( 'woocommerce_product_options_general_product_data', __CLASS__ . '::subscription_pricing_fields' );

// Add subscription fields to product editor
add_action( 'woocommerce_block_template_area_product-form_after_add_block_product-pricing-section', __CLASS__ . '::add_product_editor_fields' );

// Add listener to clear our own transients when WooCommerce -> Clear Transients is
// triggered from the admin panel
add_action( 'woocommerce_page_wc-status', __CLASS__ . '::clear_subscriptions_transients' );
Expand Down Expand Up @@ -155,6 +158,35 @@ public static function init() {
add_filter( 'woocommerce_delete_variations_on_product_type_change', array( __CLASS__, 'maybe_keep_variations' ), 10, 4 );
}

public static function add_product_editor_fields( $pricing_section ) {
if ( ! $pricing_section->get_root_template()->get_id() === 'simple-product' ) {
return;
}

$pricing_section->add_block(
[
'id' => 'subscriptions-enabled',
'blockName' => 'woocommerce/product-checkbox-field',
'order' => 20,
'attributes' => [
'title' => __(
'Subscriptions',
'woocommerce-subscriptions'
),
'label' => __(
'Subscribable',
'woocommerce-subscriptions'
),
'property' => 'is_subscribable',
'tooltip' => __(
'When checked, this product will act as a subscription.',
'woocommerce-subscriptions'
),
],
]
);
}

/**
* Clear all transients data we have when the WooCommerce::Tools::Clear Transients action is
* triggered.
Expand Down Expand Up @@ -852,6 +884,7 @@ public static function enqueue_styles_scripts() {
'users',
'woocommerce_page_wc-settings',
'woocommerce_page_wc-orders',
'woocommerce_page_wc-admin',
wcs_get_page_screen_id( 'shop_subscription' ),
],
true
Expand Down
Loading