Bulk variable subscription pricing

Changes subscription variation prices by a multiplier in batches of products. Located in WooCommerce > Settings > Products > Bulk variable subscription pricing.

// Add Settings Tab
add_filter( 'woocommerce_get_sections_products', function( $sections ) {

	$sections['ccom_bulkpricing'] = 'Bulk variable subscription pricing';
	return $sections;

} );

// Add Settings Fields
add_filter( 'woocommerce_get_settings_products', function( $settings, $current_section ) {

	// Only This Section
	if ( $current_section != 'ccom_bulkpricing' ) {
		return $settings;
	}

	// Return Settings Array
	return [

		// Add Section Title
		[ 'id' => 'ccom_bvsp_title', 'name' => 'Bulk Variable Subscription Pricing', 'type' => 'title' ],

		// Settings
		[
			'id' => 'ccom_bvsp_percentage', 'name' => 'Multiplier', 'type' => 'text',
			'desc' => 'Enter a positive or negative multiplier.',
			'desc_tip' => 'Example: To increase prices by 10% enter 1.1 in this field and empty the below field Basing.',
		],
		[
			'id' => 'ccom_bvsp_field', 'name' => 'Basing', 'type' => 'text',
			'desc' => 'Which custom product field to base price on? Leave blank to use the current variation price.',
			'desc_tip' => ' Example: _retail_price field then set the above Multiplier field to 0.10 for 10% of retail price.',
		],
		[
			'id' => 'ccom_bvsp_signup', 'name' => 'Signup fee', 'type' => 'checkbox',
			'desc' => 'Check to set the signup fee to the same amount, otherwise will set it to zero.',
		],
		[
			'id' => 'ccom_bvsp_attr_name', 'name' => 'Attribute name', 'type' => 'text',
			'desc' => 'Which subscription variation attribute shall we affect?',
			'desc_tip' => 'Example: attribute_pa_rental-period',
		],
		[
			'id' => 'ccom_bvsp_attr_value', 'name' => 'Attribute value', 'type' => 'text',
			'desc' => 'Which value from the above selected attribute shall we affect?',
			'desc_tip' => 'Example: rent-monthly or rent-1-7-days',
		],

		// Add Run Now Field
		[
			'id' => 'ccom_bvsp_run',
			'name' => 'Run Now',
			'type' => 'textarea',
			'placeholder' =>
				'Updates 25 products at a time.'
				. ' Results appear inside this box.'
				. ' When done box it will read [EOF].',
			'desc' => '
				<p>
					<a href="#" class="button" id="ccom_bulkpricing_button">
						Run variable subscription price updater
					</a>
				</p>
				<p>
					If you have changed any values on this page,
					please click Save changes button before running.
				</p>
			',
		],

		// End Settings Section
		[ 'id' => 'ccom_bvsp_section_end', 'type' => 'sectionend' ],
	];

}, 10, 2 );

// AJAX Handler
add_action( 'wp_ajax_ccom_bulkpricing', function() {

	// Get Set Of Parent / Variable Products
	$offset = empty( $_POST['offset'] ) ? 0 : intval( $_POST['offset'] );
	$args = [
		'post_type' => 'product',
		'posts_per_page' => 25,
		'offset' => $offset,
		'orderby' => 'ID',
		'order' => 'ASC',
		'post_status' => 'publish',
	];
	$posts = get_posts( $args );
	if( is_wp_error( $posts ) || ! is_array( $posts ) || ! $posts ) { wp_die(); }

	// Loop Products
	foreach( $posts as $post ) {

		// Print Product ID
		$product = wc_get_product( $post->ID );
		printf( '[%d] ', $product->get_id() );

		// Variations Only
		if( ! $product->is_type( 'variable' ) ) { continue; }

		// Get Variations
		$variations = $product->get_available_variations();
		foreach( $variations as $variation ) {

			// Update Signup Fee For Monthly Variation
			foreach( $variation['attributes'] as $attribute_name => $attribute_value ) {
				if(
					$attribute_name == get_option( 'ccom_bvsp_attr_name' )
					&& $attribute_value == get_option( 'ccom_bvsp_attr_value' )
				) {
					$variation_id = $variation['variation_id'];
					$product_variation = wc_get_product( $variation_id );
					printf( '(%d) ', $variation_id );

					// Get Price
					$price = ( float ) $product_variation->get_price();
					$ccom_bvsp_field = get_option( 'ccom_bvsp_field' )
						? get_post_meta(
							$product->get_id(), get_option( 'ccom_bvsp_field' ), true
						) : $price;
					if( ! $ccom_bvsp_field ) { continue; }
					$new_price = number_format(
						$ccom_bvsp_field * get_option( 'ccom_bvsp_percentage' ), 2
					);

					// Set Price
					update_post_meta( $variation_id, '_regular_price', $new_price );
					update_post_meta( $variation_id, '_price', $new_price );
					update_post_meta( $variation_id, '_subscription_price', $new_price );
					update_post_meta(
						$variation_id, '_subscription_sign_up_fee',
						get_option( 'ccom_bvsp_signup' ) == '1' ? $new_price : 0
					);
					wc_delete_product_transients( $variation_id );
				}
			}
		}

		// Sync Parent Price Ranges
		wc_delete_product_transients( $product->get_id() );
		$product->variable_product_sync();
	}

	// Exit
	wp_die();

} );

// jQuery
add_action( 'admin_enqueue_scripts', function( $page ) {

	// Only Load On Our Settings Page
	if( $page != 'woocommerce_page_wc-settings' ) { return; }
	if( empty( $_REQUEST['section'] ) || $_REQUEST['section'] != 'ccom_bulkpricing' ) {
		return;
	}
	ob_start();
	?>

	<script type="text/javascript">
	var ccom_bulkpricing_offset = 0;
	jQuery( document ).ready( function( $ ) {
		$( "#ccom_bulkpricing_button" ).click( function() {
			ccom_bulkpricing( $ );
			return false;
		} );
	} );
	function ccom_bulkpricing( $ ) {
		var data = {
			"action": "ccom_bulkpricing",
			"offset": ccom_bulkpricing_offset
		};
		$.post( ajaxurl, data, function( response ) {

			// Handle EOF Or Error
			if( response == "" ) {
				$( "textarea#ccom_bvsp_run" ).append( " [EOF] " );
				return;
			}

			// Display Page Processed
			$( "textarea#ccom_bvsp_run" ).append( " [" + ccom_bulkpricing_offset + "] " );

			// Advance
			ccom_bulkpricing_offset += 25;
			ccom_bulkpricing( $ );
		} );
	}
	</script>

	<?php
	$js = ob_get_clean();
	wp_add_inline_script( 'jquery', $js );

} );
  1. Log into a staging, development, or locally hosted clone of your site.
  2. Install and activate the Code Snippets plugin.
  3. Navigate to WP Admin > Snippets > Add New.
  4. Copy and paste the code from the Description tab above.
  5. Check to ensure formatting came over properly and no syntax errors show up in the editor.
  6. Customize the code as desired.
  7. Add a meaningful title and description.
  8. Select whether to run on front-end or back-end (or both). Some snippets require both.
  9. Click the "Save and Activate" button.
  10. Test your site to ensure it works as expected.
  11. Disable if any problems, or recover as necessary.
  12. Repeat steps two onward on your live environment.
  1. Describe the issue and what you've observed.
  2. Describe your expected outcome(s).
  3. List steps to reproduce the issue.
  4. Optionally provide screen-shot or video URLs.

We code all of our code snippets directly. Our clients provide most of the ideas and demand for the functionality provided by our code snippets.

There are several sources one can find on Google for code that has inspired or contributed to this open-source library. Here's some main ones:

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Disclaimer of warranty:

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.