/** * Save variations via AJAX. */ public static function save_variations() { ob_start(); check_ajax_referer('save-variations', 'security'); // Check permissions again and make sure we have what we need if (!current_user_can('edit_products') || empty($_POST) || empty($_POST['product_id'])) { die(-1); } // Remove previous meta box errors WC_Admin_Meta_Boxes::$meta_box_errors = array(); $product_id = absint($_POST['product_id']); $product_type = empty($_POST['product-type']) ? 'simple' : sanitize_title(stripslashes($_POST['product-type'])); $product_type_terms = wp_get_object_terms($product_id, 'product_type'); // If the product type hasn't been set or it has changed, update it before saving variations if (empty($product_type_terms) || $product_type !== sanitize_title(current($product_type_terms)->name)) { wp_set_object_terms($product_id, $product_type, 'product_type'); } WC_Meta_Box_Product_Data::save_variations($product_id, get_post($product_id)); do_action('woocommerce_ajax_save_product_variations', $product_id); // Clear cache/transients wc_delete_product_transients($product_id); if ($errors = WC_Admin_Meta_Boxes::$meta_box_errors) { echo '<div class="error notice is-dismissible">'; foreach ($errors as $error) { echo '<p>' . wp_kses_post($error) . '</p>'; } echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __('Dismiss this notice.', 'woocommerce') . '</span></button>'; echo '</div>'; delete_option('woocommerce_meta_box_errors'); } die; }
/** * Save variations via AJAX */ public static function save_variations() { ob_start(); check_ajax_referer('save-variations', 'security'); // Check permissions again and make sure we have what we need if (!current_user_can('edit_products') || empty($_POST) || empty($_POST['product_id'])) { die(-1); } // Remove previous meta box errors WC_Admin_Meta_Boxes::$meta_box_errors = array(); $product_id = absint($_POST['product_id']); WC_Meta_Box_Product_Data::save_variations($product_id, get_post($product_id)); do_action('woocommerce_ajax_save_product_variations', $product_id); // Clear cache/transients wc_delete_product_transients($product_id); if ($errors = WC_Admin_Meta_Boxes::$meta_box_errors) { echo '<div class="error notice is-dismissible">'; foreach ($errors as $error) { echo '<p>' . wp_kses_post($error) . '</p>'; } echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __('Dismiss this notice.', 'woocommerce') . '</span></button>'; echo '</div>'; delete_option('woocommerce_meta_box_errors'); } die; }
/** * Save variations via AJAX */ public static function save_variations() { ob_start(); check_ajax_referer('save-variations', 'security'); // Check permissions again and make sure we have what we need if (!current_user_can('edit_products') || empty($_POST) || empty($_POST['product_id'])) { die(-1); } $product_id = absint($_POST['product_id']); WC_Meta_Box_Product_Data::save_variations($product_id, get_post($product_id)); do_action('woocommerce_ajax_save_product_variations', $product_id); // Clear cache/transients wc_delete_product_transients($product_id); }
/** * 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) || empty($_POST['_wcsnonce_save_variations']) || !wp_verify_nonce($_POST['_wcsnonce_save_variations'], 'wcs_subscription_variations')) { 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 for WC < 2.4 if (!is_ajax()) { 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']; $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 = wcs_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, wc_clean($_POST['variable' . $field_name][$i])); } } } // Now that all the variation's meta is saved, sync the min variation price $variable_subscription = wc_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]); } } }