{"id":7600,"date":"2021-09-30T16:08:12","date_gmt":"2021-09-30T23:08:12","guid":{"rendered":"https:\/\/codedcommerce.com\/?post_type=product&#038;p=7600"},"modified":"2026-05-26T19:25:07","modified_gmt":"2026-05-27T02:25:07","slug":"bulk-variable-subscription-pricing","status":"publish","type":"woo-code","link":"https:\/\/codedcommerce.com\/woo\/bulk-variable-subscription-pricing\/","title":{"rendered":"Bulk variable subscription pricing"},"content":{"rendered":"\n<pre class=\"wp-block-code language-php\"><code>\/\/ Add Settings Tab\nadd_filter( 'woocommerce_get_sections_products', function( $sections ) {\n\n\t$sections&#91;'ccom_bulkpricing'] = 'Bulk variable subscription pricing';\n\treturn $sections;\n\n} );\n\n\/\/ Add Settings Fields\nadd_filter( 'woocommerce_get_settings_products', function( $settings, $current_section ) {\n\n\t\/\/ Only This Section\n\tif( $current_section != 'ccom_bulkpricing' ) {\n\t\treturn $settings;\n\t}\n\t\n\t$run_now_url = wp_nonce_url(\n\t\tadd_query_arg( 'run_now', 1 ) , 'ccom_bulkpricing'\n\t);\n\n\t\/\/ Return Settings Array\n\treturn &#91;\n\n\t\t\/\/ Add Section Title\n\t\t&#91; 'id' => 'ccom_bvsp_title', 'name' => 'Bulk Variable Subscription Pricing', 'type' => 'title' ],\n\n\t\t\/\/ Settings\n\t\t&#91;\n\t\t\t'id' => 'ccom_bvsp_percentage',\n\t\t\t'name' => 'Multiplier',\n\t\t\t'type' => 'text',\n\t\t\t'desc' => 'Enter a positive or negative multiplier.',\n\t\t\t'desc_tip' => 'Example: To increase prices by 10% enter 1.1 in this field and empty the below field Basing.',\n\t\t],\n\t\t&#91;\n\t\t\t'id' => 'ccom_bvsp_field',\n\t\t\t'name' => 'Basing',\n\t\t\t'type' => 'text',\n\t\t\t'desc' => 'Which custom product field to base price on? Leave blank to use the current variation price.',\n\t\t\t'desc_tip' => ' Example: _retail_price field then set the above Multiplier field to 0.10 for 10% of retail price.',\n\t\t],\n\t\t&#91;\n\t\t\t'id' => 'ccom_bvsp_signup',\n\t\t\t'name' => 'Signup fee',\n\t\t\t'type' => 'checkbox',\n\t\t\t'desc' => 'Check to set the signup fee to the same amount, otherwise will set it to zero.',\n\t\t],\n\t\t&#91;\n\t\t\t'id' => 'ccom_bvsp_attr_name',\n\t\t\t'name' => 'Attribute name',\n\t\t\t'type' => 'text',\n\t\t\t'default' => 'attribute_pa_rental-period',\n\t\t\t'desc' => 'Which subscription variation attribute shall we affect?',\n\t\t\t'desc_tip' => 'Example: attribute_pa_rental-period',\n\t\t],\n\t\t&#91;\n\t\t\t'id' => 'ccom_bvsp_attr_value',\n\t\t\t'name' => 'Attribute value',\n\t\t\t'type' => 'text',\n\t\t\t'default' => 'rent-monthly',\n\t\t\t'desc' => 'Which value from the above selected attribute shall we affect?',\n\t\t\t'desc_tip' => 'Example: rent-monthly, rent-1-7-days, rent-livein-12, rent-livein-6',\n\t\t],\n\n\t\t\/\/ Add Run Now Field\n\t\t&#91;\n\t\t\t'id' => 'ccom_bvsp_run',\n\t\t\t'name' => 'Run Now',\n\t\t\t'type' => 'checkbox',\n\t\t\t'desc' => '\n\t\t\t\t&lt;p>\n\t\t\t\t\t&lt;a href=\"' . $run_now_url . '\" class=\"button\">\n\t\t\t\t\t\tRun variable subscription price updater\n\t\t\t\t\t&lt;\/a>\n\t\t\t\t&lt;\/p>\n\t\t\t\t&lt;p>\n\t\t\t\t\tIf you have changed any values on this page, please click Save changes button before running.\n\t\t\t\t&lt;\/p>\n\t\t\t',\n\t\t],\n\n\t\t\/\/ End Settings Section\n\t\t&#91; 'id' => 'ccom_bvsp_section_end', 'type' => 'sectionend' ],\n\t];\n\n}, 10, 2 );\n\nadd_action( 'admin_notices', function() {\n\n\tif(\n\t\tempty( $_GET&#91;'section'] )\n\t\t|| $_GET&#91;'section'] !== 'ccom_bulkpricing'\n\t\t|| empty( $_GET&#91;'run_now'] )\n\t\t|| $_GET&#91;'run_now'] !== '1'\n\t\t|| ! wp_verify_nonce( $_REQUEST&#91;'_wpnonce'], 'ccom_bulkpricing' )\n\t) {\n\t\treturn;\n\t}\n\n\t$products = wc_get_products(\n\t\t&#91;\n\t\t\t'limit' => -1,\n\t\t\t'status' => 'publish',\n\t\t\t'type' => 'variable-subscription',\n\t\t\t'return' => 'ids',\n\t\t]\n\t);\n\n\tforeach( $products as $product_id ) {\n\t\tas_schedule_single_action(\n\t\t\tcurrent_time( 'timestamp', 1 ),\n\t\t\t'ccom_bulkpricing_runner',\n\t\t\t&#91; 'product_id' => $product_id ]\n\t\t);\n\t}\n\n\twp_admin_notice(\n\t\t'&lt;a href=\"\/wp-admin\/tools.php?page=action-scheduler\">Scheduled bulk job.&lt;\/a>',\n\t\t&#91; 'type' => 'success' ]\n\t);\n\n} );\n\nadd_action( 'ccom_bulkpricing_runner', function( $product_id ) {\n\n\t\/\/ Get Variations\n\t$product = wc_get_product( $product_id );\n\t$variations = $product->get_available_variations();\n\tforeach( $variations as $variation ) {\n\n\t\t\/\/ Update Requested Variation Only\n\t\tforeach( $variation&#91;'attributes'] as $attribute_name => $attribute_value ) {\n\t\t\tif(\n\t\t\t\t$attribute_name == get_option( 'ccom_bvsp_attr_name' )\n\t\t\t\t&amp;&amp; $attribute_value == get_option( 'ccom_bvsp_attr_value' )\n\t\t\t) {\n\t\t\t\t$variation_id = $variation&#91;'variation_id'];\n\t\t\t\t$product_variation = wc_get_product( $variation_id );\n\n\t\t\t\t\/\/ Get Price\n\t\t\t\t$price = ( float ) $product_variation->get_price();\n\t\t\t\t$ccom_bvsp_field = get_option( 'ccom_bvsp_field' )\n\t\t\t\t\t? get_post_meta(\n\t\t\t\t\t\t$product->get_id(), get_option( 'ccom_bvsp_field' ), true\n\t\t\t\t\t) : $price;\n\t\t\t\tif( ! $ccom_bvsp_field ) { continue; }\n\t\t\t\t$new_price = number_format(\n\t\t\t\t\t$ccom_bvsp_field * get_option( 'ccom_bvsp_percentage' ), 2\n\t\t\t\t);\n\n\t\t\t\t\/\/ Set Price\n\t\t\t\tupdate_post_meta( $variation_id, '_regular_price', $new_price );\n\t\t\t\tupdate_post_meta( $variation_id, '_price', $new_price );\n\t\t\t\tupdate_post_meta( $variation_id, '_subscription_price', $new_price );\n\t\t\t\tupdate_post_meta(\n\t\t\t\t\t$variation_id, '_subscription_sign_up_fee',\n\t\t\t\t\tget_option( 'ccom_bvsp_signup' ) == 'yes' ? $new_price : 0\n\t\t\t\t);\n\t\t\t\twc_delete_product_transients( $variation_id );\n\t\t\t}\n\t\t}\n\t}\n\n\t\/\/ Sync Parent Price Ranges\n\twc_delete_product_transients( $product->get_id() );\n\t$product->variable_product_sync();\n\n} );<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-7600","woo-code","type-woo-code","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/woo-code\/7600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/woo-code"}],"about":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/types\/woo-code"}],"wp:attachment":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/media?parent=7600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}