jQuery( function ( $ ) {
$.extend( {
getParameterByName: function ( name ) {
name = name.replace( /[\[]/, '\\[' ).replace( /[\]]/, '\\]' );
var regexS = '[\\?&]' + name + '=([^]*)';
var regex = new RegExp( regexS );
var results = regex.exec( window.location.search );
if ( results == null ) {
return '';
} else {
return decodeURIComponent( results[ 1 ].replace( /\+/g, ' ' ) );
}
},
daysInMonth: function ( month ) {
// Intentionally choose a non-leap year because we want february to have only 28 days.
return new Date( Date.UTC( 2001, month, 0 ) ).getUTCDate();
},
showHideSubscriptionMeta: function () {
if (
$( 'select#product-type' ).val() == WCSubscriptions.productType
) {
$( '.show_if_simple' ).show();
$( '.grouping_options' ).hide();
$( '.options_group.pricing ._regular_price_field' ).hide();
$( '#sale-price-period' ).show();
$( '.hide_if_subscription' ).hide();
$( 'input#_manage_stock' ).trigger( 'change' );
if ( 'day' == $( '#_subscription_period' ).val() ) {
$( '.subscription_sync' ).hide();
}
} else {
$( '.options_group.pricing ._regular_price_field' ).show();
$( '#sale-price-period' ).hide();
}
},
showHideVariableSubscriptionMeta: function () {
// In order for WooCommerce not to show the stock_status_field on variable subscriptions, make sure it has the hide if variable subscription class.
$( 'p.stock_status_field' ).addClass(
'hide_if_variable-subscription'
);
var product_type = $( 'select#product-type' ).val();
if ( 'variable-subscription' === product_type ) {
// Hide and show subscription fields when variable subscription is selected
$( 'input#_downloadable' ).prop( 'checked', false );
$( 'input#_virtual' ).prop( 'checked', false );
// Variable subscriptions inherit fields from variable products.
$( '.show_if_variable' ).show();
$( '.hide_if_variable' ).hide();
$( '.show_if_variable-subscription' ).show();
$( '.hide_if_variable-subscription' ).hide();
$.showOrHideStockFields();
// Make the sale price row full width
$( '.sale_price_dates_fields' )
.prev( '.form-row' )
.addClass( 'form-row-full' )
.removeClass( 'form-row-last' );
} else {
if ( 'subscription' === product_type ) {
$( '.show_if_subscription' ).show();
$( '.hide_if_subscription' ).hide();
}
// Restore the sale price row width to half
$( '.sale_price_dates_fields' )
.prev( '.form-row' )
.removeClass( 'form-row-full' )
.addClass( 'form-row-last' );
}
},
enableSubscriptionProductFields: function () {
product_type = $( 'select#product-type' ).val();
enable_type = '';
// Variable subscriptions need to enable variable product fields and subscriptions products need to enable simple product fields.
if ( 'variable-subscription' === product_type ) {
enable_type = 'variable'
} else if ( 'subscription' === product_type ) {
enable_type = 'simple';
}
if ( enable_type ) {
$( `.enable_if_${ enable_type }` ).each( function () {
$( this ).removeClass( 'disabled' );
if ( $( this ).is( 'input' ) ) {
$( this ).prop( 'disabled', false );
}
} );
}
},
showOrHideStockFields: function () {
if ( $( 'input#_manage_stock' ).is( ':checked' ) ) {
$( 'div.stock_fields' ).show();
} else {
$( 'div.stock_fields' ).hide();
}
},
setSubscriptionLengths: function () {
$(
'[name^="_subscription_length"], [name^="variable_subscription_length"]'
).each( function () {
var $lengthElement = $( this ),
selectedLength = $lengthElement.val(),
hasSelectedLength = false,
matches = $lengthElement
.attr( 'name' )
.match( /\[(.*?)\]/ ),
periodSelector,
interval;
if ( matches ) {
// Variation
periodSelector =
'[name="variable_subscription_period[' +
matches[ 1 ] +
']"]';
billingInterval = parseInt(
$(
'[name="variable_subscription_period_interval[' +
matches[ 1 ] +
']"]'
).val()
);
} else {
periodSelector = '#_subscription_period';
billingInterval = parseInt(
$( '#_subscription_period_interval' ).val()
);
}
$lengthElement.empty();
$.each(
WCSubscriptions.subscriptionLengths[
$( periodSelector ).val()
],
function ( length, description ) {
if (
parseInt( length ) == 0 ||
0 == parseInt( length ) % billingInterval
) {
$lengthElement.append(
$( '' )
.attr( 'value', length )
.text( description )
);
}
}
);
$lengthElement.children( 'option' ).each( function () {
if ( this.value == selectedLength ) {
hasSelectedLength = true;
return false;
}
} );
if ( hasSelectedLength ) {
$lengthElement.val( selectedLength );
} else {
$lengthElement.val( 0 );
}
} );
},
setTrialPeriods: function () {
$(
'[name^="_subscription_trial_length"], [name^="variable_subscription_trial_length"]'
).each( function () {
var $trialLengthElement = $( this ),
trialLength = $trialLengthElement.val(),
matches = $trialLengthElement
.attr( 'name' )
.match( /\[(.*?)\]/ ),
periodStrings;
if ( matches ) {
// Variation
$trialPeriodElement = $(
'[name="variable_subscription_trial_period[' +
matches[ 1 ] +
']"]'
);
} else {
$trialPeriodElement = $( '#_subscription_trial_period' );
}
selectedTrialPeriod = $trialPeriodElement.val();
$trialPeriodElement.empty();
if ( parseInt( trialLength ) == 1 ) {
periodStrings = WCSubscriptions.trialPeriodSingular;
} else {
periodStrings = WCSubscriptions.trialPeriodPlurals;
}
$.each( periodStrings, function ( key, description ) {
$trialPeriodElement.append(
$( '' )
.attr( 'value', key )
.text( description )
);
} );
$trialPeriodElement.val( selectedTrialPeriod );
} );
},
setSalePeriod: function () {
$( '#sale-price-period' ).fadeOut( 80, function () {
$( '#sale-price-period' ).text(
$(
'#_subscription_period_interval option:selected'
).text() +
' ' +
$( '#_subscription_period option:selected' ).text()
);
$( '#sale-price-period' ).fadeIn( 180 );
} );
},
setSyncOptions: function ( periodField ) {
if ( typeof periodField != 'undefined' ) {
if (
$( 'select#product-type' ).val() == 'variable-subscription'
) {
var $container = periodField
.closest( '.woocommerce_variable_attributes' )
.find( '.variable_subscription_sync' );
} else {
$container = periodField
.closest( '#general_product_data' )
.find( '.subscription_sync' );
}
var $syncWeekMonthContainer = $container.find(
'.subscription_sync_week_month'
),
$syncWeekMonthSelect = $syncWeekMonthContainer.find(
'select'
),
$syncAnnualContainer = $container.find(
'.subscription_sync_annual'
),
$varSubField = $container.find(
'[name^="variable_subscription_payment_sync_date"]'
),
billingPeriod;
if ( $varSubField.length > 0 ) {
// Variation
var matches = $varSubField
.attr( 'name' )
.match( /\[(.*?)\]/ );
$subscriptionPeriodElement = $(
'[name="variable_subscription_period[' +
matches[ 1 ] +
']"]'
);
} else {
$subscriptionPeriodElement = $( '#_subscription_period' );
}
billingPeriod = $subscriptionPeriodElement.val();
if ( 'day' == billingPeriod ) {
$syncWeekMonthSelect.val( 0 );
$syncAnnualContainer
.find( 'input[type="number"]' )
.val( 0 )
.trigger( 'change' );
} else {
if ( 'year' == billingPeriod ) {
// Make sure the year sync fields are reset
$syncAnnualContainer
.find( 'input[type="number"]' )
.val( 0 )
.trigger( 'change' );
// And the week/month field has no option selected
$syncWeekMonthSelect.val( 0 );
} else {
// Make sure the year sync value is 0
$syncAnnualContainer
.find( 'input[type="number"]' )
.val( 0 )
.trigger( 'change' );
// And the week/month field has the appropriate options
$syncWeekMonthSelect.empty();
$.each(
WCSubscriptions.syncOptions[ billingPeriod ],
function ( key, description ) {
$syncWeekMonthSelect.append(
$( '' )
.attr( 'value', key )
.text( description )
);
}
);
}
}
}
},
showHideSyncOptions: function () {
if (
$( '#_subscription_payment_sync_date' ).length > 0 ||
$( '.wc_input_subscription_payment_sync' ).length > 0
) {
$( '.subscription_sync, .variable_subscription_sync' ).each(
function () {
// loop through all sync field groups
var $syncWeekMonthContainer = $( this ).find(
'.subscription_sync_week_month'
),
$syncWeekMonthSelect = $syncWeekMonthContainer.find(
'select'
),
$syncAnnualContainer = $( this ).find(
'.subscription_sync_annual'
),
$varSubField = $( this ).find(
'[name^="variable_subscription_payment_sync_date"]'
),
$slideSwitch = false, // stop the general sync field group sliding down if editing a variable subscription
billingPeriod;
if ( $varSubField.length > 0 ) {
// Variation
var matches = $varSubField
.attr( 'name' )
.match( /\[(.*?)\]/ );
$subscriptionPeriodElement = $(
'[name="variable_subscription_period[' +
matches[ 1 ] +
']"]'
);
if (
$( 'select#product-type' ).val() ==
'variable-subscription'
) {
$slideSwitch = true;
}
} else {
$subscriptionPeriodElement = $(
'#_subscription_period'
);
if (
$( 'select#product-type' ).val() ==
WCSubscriptions.productType
) {
$slideSwitch = true;
}
}
billingPeriod = $subscriptionPeriodElement.val();
if ( 'day' == billingPeriod ) {
$( this ).slideUp( 400 );
} else {
if ( $slideSwitch ) {
$( this ).slideDown( 400 );
if ( 'year' == billingPeriod ) {
// Make sure the year sync fields are visible
$syncAnnualContainer.slideDown( 400 );
// And the week/month field is hidden
$syncWeekMonthContainer.slideUp( 400 );
} else {
// Make sure the year sync fields are hidden
$syncAnnualContainer.slideUp( 400 );
// And the week/month field is visible
$syncWeekMonthContainer.slideDown( 400 );
}
}
}
}
);
}
},
moveSubscriptionVariationFields: function () {
$( '#variable_product_options .variable_subscription_pricing' )
.not( 'wcs_moved' )
.each( function () {
var $regularPriceRow = $( this ).siblings(
'.variable_pricing'
),
$trialSignUpRow = $( this ).siblings(
'.variable_subscription_trial_sign_up'
),
$saleDatesRow;
$saleDatesRow = $( this ).siblings( '.variable_pricing' );
// Add the subscription price fields above the standard price fields
$( this ).insertBefore( $regularPriceRow );
$trialSignUpRow.insertAfter( $( this ) );
// Replace the regular price field with the trial period field
$regularPriceRow
.children( ':first' )
.addClass( 'hide_if_variable-subscription' );
$( this ).addClass( 'wcs_moved' );
} );
},
moveSubscriptionGiftingFields: function () {
$( '#variable_product_options .variable_subscription_gifting' )
.not( '.wcs_gifting_moved' )
.each( function () {
var $trialSignUpRow = $( this ).siblings(
'.variable_subscription_trial_sign_up'
),
$subscriptionPricingRow = $( this ).siblings(
'.variable_subscription_pricing'
);
// Position gifting field after trial sign up row if it exists, otherwise after subscription pricing
if ( $trialSignUpRow.length > 0 ) {
$( this ).insertAfter( $trialSignUpRow );
} else if ( $subscriptionPricingRow.length > 0 ) {
$( this ).insertAfter( $subscriptionPricingRow );
}
$( this ).addClass( 'wcs_gifting_moved' );
} );
},
getVariationBulkEditValue: function ( variation_action ) {
var value;
switch ( variation_action ) {
case 'variable_subscription_period':
case 'variable_subscription_trial_period':
value = prompt( WCSubscriptions.bulkEditPeriodMessage );
break;
case 'variable_subscription_period_interval':
value = prompt( WCSubscriptions.bulkEditIntervalhMessage );
break;
case 'variable_subscription_trial_length':
case 'variable_subscription_length':
value = prompt( WCSubscriptions.bulkEditLengthMessage );
break;
case 'variable_subscription_sign_up_fee':
value = prompt(
woocommerce_admin_meta_boxes_variations.i18n_enter_a_value
);
value = accounting.unformat(
value,
woocommerce_admin.mon_decimal_point
);
break;
}
return value;
},
disableEnableOneTimeShipping: function () {
var is_synced_or_has_trial = false;
if ( 'variable-subscription' == $( 'select#product-type' ).val() ) {
var variations = $(
'.woocommerce_variations .woocommerce_variation'
),
variations_checked = {},
number_of_pages = $( '.woocommerce_variations' ).attr(
'data-total_pages'
);
$( variations ).each( function () {
var period_field = $( this ).find(
'.wc_input_subscription_period'
),
variation_index = $( period_field )
.attr( 'name' )
.match( /\[(.*?)\]/ ),
variation_id = $(
'[name="variable_post_id[' +
variation_index[ 1 ] +
']"]'
).val(),
period = period_field.val(),
trial = $( this )
.find( '.wc_input_subscription_trial_length' )
.val(),
sync_date = 0;
if ( 0 != trial ) {
is_synced_or_has_trial = true;
// break
return false;
}
if (
$( this ).find( '.variable_subscription_sync' ).length
) {
if ( 'month' == period || 'week' == period ) {
sync_date = $(
'[name="variable_subscription_payment_sync_date[' +
variation_index[ 1 ] +
']"]'
).val();
} else if ( 'year' == period ) {
sync_date = $(
'[name="variable_subscription_payment_sync_date_day[' +
variation_index[ 1 ] +
']"]'
).val();
}
if ( 0 != sync_date ) {
is_synced_or_has_trial = true;
// break
return false;
}
}
variations_checked[ variation_index[ 1 ] ] = variation_id;
} );
// if we haven't found a variation synced or with a trial at this point check the backend for other product variations
if (
( number_of_pages > 1 || 0 == variations.length ) &&
false == is_synced_or_has_trial
) {
var data = {
action: 'wcs_product_has_trial_or_is_synced',
product_id:
woocommerce_admin_meta_boxes_variations.post_id,
variations_checked: variations_checked,
nonce: WCSubscriptions.oneTimeShippingCheckNonce,
};
$.ajax( {
url: WCSubscriptions.ajaxUrl,
data: data,
type: 'POST',
success: function ( response ) {
$( '#_subscription_one_time_shipping' ).prop(
'disabled',
response.is_synced_or_has_trial
);
// trigger an event now we have determined the one time shipping availability, in case we need to update the backend
$(
'#_subscription_one_time_shipping'
).trigger(
'subscription_one_time_shipping_updated',
[ response.is_synced_or_has_trial ]
);
},
} );
} else {
// trigger an event now we have determined the one time shipping availability, in case we need to update the backend
$(
'#_subscription_one_time_shipping'
).trigger( 'subscription_one_time_shipping_updated', [
is_synced_or_has_trial,
] );
}
} else {
var trial = $(
'#general_product_data #_subscription_trial_length'
).val();
if ( 0 != trial ) {
is_synced_or_has_trial = true;
}
if (
$( '.subscription_sync' ).length &&
false == is_synced_or_has_trial
) {
var period = $( '#_subscription_period' ).val(),
sync_date = 0;
if ( 'month' == period || 'week' == period ) {
sync_date = $(
'#_subscription_payment_sync_date'
).val();
} else if ( 'year' == period ) {
sync_date = $(
'#_subscription_payment_sync_date_day'
).val();
}
if ( 0 != sync_date ) {
is_synced_or_has_trial = true;
}
}
}
$( '#_subscription_one_time_shipping' ).prop(
'disabled',
is_synced_or_has_trial
);
},
showHideSubscriptionsPanels: function () {
var tab = $( 'div.panel-wrap' )
.find( 'ul.wc-tabs li' )
.eq( 0 )
.find( 'a' );
var panel = tab.attr( 'href' );
var visible = $( panel )
.children( '.options_group' )
.filter( function () {
return 'none' != $( this ).css( 'display' );
} );
if ( 0 != visible.length ) {
tab.trigger( 'click' ).parent().show();
}
},
maybeDisableRemoveLinks: function () {
$( '#variable_product_options .woocommerce_variation' ).each(
function () {
var $removeLink = $( this ).find( '.remove_variation' );
if ( WCSubscriptions.isLargeSite ) {
$removeLink.addClass( 'wcs_validate_variation_delete' );
$removeLink.removeClass( 'remove_variation' );
} else {
var can_remove_variation =
'1' ===
$( this )
.find( 'input.wcs-can-remove-variation' )
.val();
var $msg = $( this ).find(
'.wcs-can-not-remove-variation-msg'
);
if ( ! can_remove_variation ) {
$msg.text( $removeLink.text() );
$removeLink.replaceWith( $msg );
}
}
}
);
},
} );
$( '.options_group.pricing ._sale_price_field .description' ).prepend(
''
);
// Move the subscription pricing section to the same location as the normal pricing section
$( '.options_group.subscription_pricing' )
.not(
'.variable_subscription_pricing .options_group.subscription_pricing'
)
.insertBefore( $( '.options_group.pricing:first' ) );
$( '.show_if_subscription.clear' ).insertAfter(
$( '.options_group.subscription_pricing' )
);
// Move the subscription variation pricing and gifting sections to a better location in the DOM on load
// We do this because these sections are initially loaded at the end of the variable product options section,
// which is not the best location for them, so we move to before the "Sale price" section.
if (
$( '#variable_product_options .variable_subscription_pricing' ).length >
0
) {
$.moveSubscriptionVariationFields();
}
if (
$( '#variable_product_options .variable_subscription_gifting' ).length >
0
) {
$.moveSubscriptionGiftingFields();
}
// When a variation is added
$( '#woocommerce-product-data' ).on(
'woocommerce_variations_added woocommerce_variations_loaded',
function () {
$.moveSubscriptionVariationFields();
$.moveSubscriptionGiftingFields();
$.showHideVariableSubscriptionMeta();
$.showHideSyncOptions();
$.setSubscriptionLengths();
}
);
if ( $( '.options_group.pricing' ).length > 0 ) {
$.setSalePeriod();
$.showHideSubscriptionMeta();
$.enableSubscriptionProductFields();
$.showHideVariableSubscriptionMeta();
$.setSubscriptionLengths();
$.setTrialPeriods();
$.showHideSyncOptions();
$.disableEnableOneTimeShipping();
$.showHideSubscriptionsPanels();
}
// Update subscription ranges when subscription period or interval is changed
$( '#woocommerce-product-data' ).on(
'change',
'[name^="_subscription_period"], [name^="_subscription_period_interval"], [name^="variable_subscription_period"], [name^="variable_subscription_period_interval"]',
function () {
$.setSubscriptionLengths();
$.showHideSyncOptions();
$.setSyncOptions( $( this ) );
$.setSalePeriod();
$.disableEnableOneTimeShipping();
}
);
$( '#woocommerce-product-data' ).on(
'propertychange keyup input paste change',
'[name^="_subscription_trial_length"], [name^="variable_subscription_trial_length"]',
function () {
$.setTrialPeriods();
}
);
// Handles changes to sync date select/input for yearly subscription products.
$( '#woocommerce-product-data' )
.on(
'change',
'[name^="_subscription_payment_sync_date_day"], [name^="variable_subscription_payment_sync_date_day"]',
function () {
if ( 0 == $( this ).val() ) {
$( this )
.siblings(
'[name^="_subscription_payment_sync_date_month"], [name^="variable_subscription_payment_sync_date_month"]'
)
.val( 0 );
$( this ).prop( 'disabled', true );
}
}
)
.on(
'change',
'[name^="_subscription_payment_sync_date_month"], [name^="variable_subscription_payment_sync_date_month"]',
function () {
var $syncDayOfMonthInput = $( this ).siblings(
'[name^="_subscription_payment_sync_date_day"], [name^="variable_subscription_payment_sync_date_day"]'
);
if ( 0 < $( this ).val() ) {
// Clear existing options.
$syncDayOfMonthInput.empty();
// Add options for each day in the month.
for ( var day = 1; day <= $.daysInMonth( $( this ).val() ); day++ ) {
$syncDayOfMonthInput.append( $( '