/** * Save a variable subscription's details when the edit product page is submitted for a variable * subscription product type (or the bulk edit product is saved). * * @param int $post_id ID of the parent WC_Product_Variable_Subscription * @return null * @since 1.3 */ public static function process_product_meta_variable_subscription($post_id) { if (!WC_Subscriptions_Product::is_subscription($post_id)) { return; } // Make sure WooCommerce calculates correct prices $_POST['variable_regular_price'] = isset($_POST['variable_subscription_price']) ? $_POST['variable_subscription_price'] : 0; // Run WooCommerce core saving routine if (!class_exists('WC_Meta_Box_Product_Data')) { // WC < 2.1 process_product_meta_variable($post_id); } elseif (!is_ajax()) { // WC < 2.4 WC_Meta_Box_Product_Data::save_variations($post_id, get_post($post_id)); } if (isset($_REQUEST['_subscription_limit'])) { update_post_meta($post_id, '_subscription_limit', stripslashes($_REQUEST['_subscription_limit'])); } if (!isset($_REQUEST['variable_post_id'])) { return; } $variable_post_ids = $_POST['variable_post_id']; $max_loop = max(array_keys($variable_post_ids)); // Save each variations details for ($i = 0; $i <= $max_loop; $i++) { if (!isset($variable_post_ids[$i])) { continue; } $variation_id = absint($variable_post_ids[$i]); if (isset($_POST['variable_subscription_price']) && is_array($_POST['variable_subscription_price'])) { $subscription_price = wc_format_decimal($_POST['variable_subscription_price'][$i]); update_post_meta($variation_id, '_subscription_price', $subscription_price); update_post_meta($variation_id, '_regular_price', $subscription_price); } // Make sure trial period is within allowable range $subscription_ranges = WC_Subscriptions_Manager::get_subscription_ranges(); $max_trial_length = count($subscription_ranges[$_POST['variable_subscription_trial_period'][$i]]) - 1; $_POST['variable_subscription_trial_length'][$i] = absint($_POST['variable_subscription_trial_length'][$i]); if ($_POST['variable_subscription_trial_length'][$i] > $max_trial_length) { $_POST['variable_subscription_trial_length'][$i] = $max_trial_length; } // Work around a WPML bug which means 'variable_subscription_trial_period' is not set when using "Edit Product" as the product translation interface if ($_POST['variable_subscription_trial_length'][$i] < 0) { $_POST['variable_subscription_trial_length'][$i] = 0; } $subscription_fields = array('_subscription_sign_up_fee', '_subscription_period', '_subscription_period_interval', '_subscription_length', '_subscription_trial_period', '_subscription_trial_length'); foreach ($subscription_fields as $field_name) { if (isset($_POST['variable' . $field_name][$i])) { update_post_meta($variation_id, $field_name, woocommerce_clean($_POST['variable' . $field_name][$i])); } } } // Now that all the varation's meta is saved, sync the min variation price $variable_subscription = get_product($post_id); $variable_subscription->variable_product_sync(); }
/** * Save a variable subscription's details when the edit product page is submitted for a variable * subscription product type (or the bulk edit product is saved). * * @param $post_id int ID of the parent WC_Product_Variable_Subscription * @return null * @since 1.3 */ public static function process_product_meta_variable_subscription($post_id) { if (!WC_Subscriptions_Product::is_subscription($post_id)) { return; } // Make sure WooCommerce calculates correct prices $_POST['variable_regular_price'] = $_POST['variable_subscription_price']; // Run WooCommerce core saving routine process_product_meta_variable($post_id); if (!isset($_REQUEST['_subscription_limit'])) { $_REQUEST['_subscription_limit'] = 'no'; } update_post_meta($post_id, '_subscription_limit', stripslashes($_REQUEST['_subscription_limit'])); if (!isset($_REQUEST['variable_post_id'])) { return; } $variable_post_ids = $_POST['variable_post_id']; $max_loop = max(array_keys($variable_post_ids)); // Save each variations details for ($i = 0; $i <= $max_loop; $i++) { if (!isset($variable_post_ids[$i])) { continue; } $variation_id = absint($variable_post_ids[$i]); if (isset($_POST['variable_subscription_price']) && is_array($_POST['variable_subscription_price'])) { $subscription_price = self::clean_number(woocommerce_clean($_POST['variable_subscription_price'][$i])); update_post_meta($variation_id, '_subscription_price', $subscription_price); update_post_meta($variation_id, '_regular_price', $subscription_price); } // Make sure trial period is within allowable range $subscription_ranges = WC_Subscriptions_Manager::get_subscription_ranges(); $max_trial_length = count($subscription_ranges[$_POST['variable_subscription_trial_period'][$i]]) - 1; $_POST['variable_subscription_trial_length'][$i] = absint($_POST['variable_subscription_trial_length'][$i]); if ($_POST['variable_subscription_trial_length'][$i] > $max_trial_length) { $_POST['variable_subscription_trial_length'][$i] = $max_trial_length; } $subscription_fields = array('_subscription_sign_up_fee', '_subscription_period', '_subscription_period_interval', '_subscription_length', '_subscription_trial_period', '_subscription_trial_length'); foreach ($subscription_fields as $field_name) { if (isset($_POST['variable' . $field_name][$i])) { update_post_meta($variation_id, $field_name, woocommerce_clean($_POST['variable' . $field_name][$i])); } } } // Now that all the varation's meta is saved, sync the min variation price $variable_subscription = get_product($post_id); $variable_subscription->variable_product_sync(); }
/** * Save meta data for registration product type when the "Edit Product" form is submitted. * * @param array Array of Product types & their labels, excluding the Course product type. * @return array Array of Product types & their labels, including the Course product type. * @since 1.0 */ public static function save_variable_fields($post_id) { // Run WooCommerce core saving routine if (!class_exists('WC_Meta_Box_Product_Data')) { // WC < 2.1 process_product_meta_variable($post_id); } elseif (!is_ajax()) { // WC < 2.4 WC_Meta_Box_Product_Data::save_variations($post_id, get_post($post_id)); } if (!isset($_REQUEST['variable_post_id'])) { return; } $variable_post_ids = $_POST['variable_post_id']; isset($_POST['_event_start_time']) ? $_event_start_time = $_POST['_event_start_time'] : ($_event_start_time = null); isset($_POST['_event_end_time']) ? $_event_end_time = $_POST['_event_end_time'] : ($_event_end_time = null); isset($_POST['_week_days']) ? $_week_days = $_POST['_week_days'] : ($_week_days = null); $max_loop = max(array_keys($variable_post_ids)); // Save each variations details for ($i = 0; $i <= $max_loop; $i++) { if (!isset($variable_post_ids[$i])) { continue; } $variation_id = (int) $variable_post_ids[$i]; if (isset($_event_start_time[$i])) { update_post_meta($variation_id, '_event_start_time', stripslashes($_event_start_time[$i])); } if (isset($_event_end_time[$i])) { update_post_meta($variation_id, '_event_end_time', stripslashes($_event_end_time[$i])); } if (isset($_week_days[$i])) { update_post_meta($variation_id, '_week_days', $_week_days[$i]); } } }