function wc_redirects__wc_admin_process_product_meta($post_id) { if (isset($_POST[WC_REDIRECTS__REDIRECTION_TYPE_META_NAME])) { $value = $_POST[WC_REDIRECTS__REDIRECTION_TYPE_META_NAME]; $is_valid = false; if ($value === '') { $is_valid = true; } else { foreach (wc_redirects__get_valid_redirection_types() as $registration_type) { if ($value === $registration_type['name']) { $is_valid = true; break; } } } if (!$is_valid) { WC_Admin_Meta_Boxes::add_error(__('The selected Redirection Type is not valid.', 'wc_redirects')); return; } if ($value === '') { delete_post_meta($post_id, WC_REDIRECTS__REDIRECTION_TYPE_META_NAME); } else { update_post_meta($post_id, WC_REDIRECTS__REDIRECTION_TYPE_META_NAME, $value); } } }
/** * Save subscription options. * * @param int $post_id * @return void */ public static function process_product_meta($post_id) { // Get type. $product_type = empty($_POST['product-type']) ? 'simple' : sanitize_title(stripslashes($_POST['product-type'])); $supported_types = WCS_ATT()->get_supported_product_types(); if (in_array($product_type, $supported_types)) { // Save one time shipping option. update_post_meta($post_id, '_subscription_one_time_shipping', stripslashes(isset($_POST['_subscription_one_time_shipping']) ? 'yes' : 'no')); // Save subscription scheme options. if (isset($_POST['wcsatt_schemes'])) { $posted_schemes = stripslashes_deep($_POST['wcsatt_schemes']); $unique_schemes = array(); foreach ($posted_schemes as $posted_scheme) { // Copy variable type fields. if ('variable' === $product_type) { if (isset($posted_scheme['subscription_regular_price_variable'])) { $posted_scheme['subscription_regular_price'] = $posted_scheme['subscription_regular_price_variable']; } if (isset($posted_scheme['subscription_sale_price_variable'])) { $posted_scheme['subscription_sale_price'] = $posted_scheme['subscription_sale_price_variable']; } if (isset($posted_scheme['subscription_discount_variable'])) { $posted_scheme['subscription_discount'] = $posted_scheme['subscription_discount_variable']; } if (isset($posted_scheme['subscription_pricing_method_variable'])) { $posted_scheme['subscription_pricing_method'] = $posted_scheme['subscription_pricing_method_variable']; } } // Format subscription prices. if (isset($posted_scheme['subscription_regular_price'])) { $posted_scheme['subscription_regular_price'] = $posted_scheme['subscription_regular_price'] === '' ? '' : wc_format_decimal($posted_scheme['subscription_regular_price']); } if (isset($posted_scheme['subscription_sale_price'])) { $posted_scheme['subscription_sale_price'] = $posted_scheme['subscription_sale_price'] === '' ? '' : wc_format_decimal($posted_scheme['subscription_sale_price']); } if ('' !== $posted_scheme['subscription_sale_price']) { $posted_scheme['subscription_price'] = $posted_scheme['subscription_sale_price']; } else { $posted_scheme['subscription_price'] = $posted_scheme['subscription_regular_price'] === '' ? '' : $posted_scheme['subscription_regular_price']; } // Format subscription discount. if (isset($posted_scheme['subscription_discount'])) { if (is_numeric($posted_scheme['subscription_discount'])) { $discount = (double) wc_format_decimal($posted_scheme['subscription_discount']); if ($discount < 0 || $discount > 100) { WC_Admin_Meta_Boxes::add_error(__('Please enter positive subscription discount values, between 0-100.', WCS_ATT::TEXT_DOMAIN)); $posted_scheme['subscription_discount'] = ''; } else { $posted_scheme['subscription_discount'] = $discount; } } else { $posted_scheme['subscription_discount'] = ''; } } else { $posted_scheme['subscription_discount'] = ''; } // Validate price override method. if (isset($posted_scheme['subscription_pricing_method']) && $posted_scheme['subscription_pricing_method'] === 'override') { if ($posted_scheme['subscription_price'] === '' && $posted_scheme['subscription_regular_price'] === '') { $posted_scheme['subscription_pricing_method'] = 'inherit'; } } else { $posted_scheme['subscription_pricing_method'] = 'inherit'; } // Construct scheme id. $scheme_id = $posted_scheme['subscription_period_interval'] . '_' . $posted_scheme['subscription_period'] . '_' . $posted_scheme['subscription_length']; $unique_schemes[$scheme_id] = $posted_scheme; $unique_schemes[$scheme_id]['id'] = $scheme_id; } update_post_meta($post_id, '_wcsatt_schemes', $unique_schemes); } else { delete_post_meta($post_id, '_wcsatt_schemes'); } // Save default status. if (isset($_POST['_wcsatt_default_status'])) { update_post_meta($post_id, '_wcsatt_default_status', stripslashes($_POST['_wcsatt_default_status'])); } // Save one-time status. $force_subscription = isset($_POST['_wcsatt_force_subscription']) ? 'yes' : 'no'; update_post_meta($post_id, '_wcsatt_force_subscription', $force_subscription); // Set regular price as ZERO should the shop owner forget. // This helps make WooCommerce think it's still available for purchase. if ('yes' === $force_subscription && empty($_POST['_regular_price'])) { update_post_meta($post_id, '_regular_price', wc_format_decimal(0)); update_post_meta($post_id, '_price', wc_format_decimal(0)); } // Save prompt. if (!empty($_POST['_wcsatt_subscription_prompt'])) { $prompt = wp_kses_post(stripslashes($_POST['_wcsatt_subscription_prompt'])); update_post_meta($post_id, '_wcsatt_subscription_prompt', $prompt); } else { delete_post_meta($post_id, '_wcsatt_subscription_prompt'); } } else { delete_post_meta($post_id, '_wcsatt_schemes'); delete_post_meta($post_id, '_wcsatt_force_subscription'); delete_post_meta($post_id, '_wcsatt_default_status'); delete_post_meta($post_id, '_wcsatt_subscription_prompt'); } }
/** * Save meta box data. * * @param int $post_id * @param WP_Post $post */ public static function save($post_id, $post) { global $wpdb; // Check for dupe coupons $coupon_code = apply_filters('woocommerce_coupon_code', $post->post_title); $id_from_code = wc_get_coupon_id_by_code($coupon_code, $post_id); if ($id_from_code) { WC_Admin_Meta_Boxes::add_error(__('Coupon code already exists - customers will use the latest coupon with this code.', 'woocommerce')); } $coupon = new WC_Coupon($post_id); $coupon->set_props(array('code' => $post->post_title, 'discount_type' => wc_clean($_POST['discount_type']), 'amount' => wc_format_decimal($_POST['coupon_amount']), 'date_expires' => wc_clean($_POST['expiry_date']), 'individual_use' => isset($_POST['individual_use']), 'product_ids' => array_filter(array_map('intval', explode(',', $_POST['product_ids']))), 'excluded_product_ids' => array_filter(array_map('intval', explode(',', $_POST['exclude_product_ids']))), 'usage_limit' => absint($_POST['usage_limit']), 'usage_limit_per_user' => absint($_POST['usage_limit_per_user']), 'limit_usage_to_x_items' => absint($_POST['limit_usage_to_x_items']), 'free_shipping' => isset($_POST['free_shipping']), 'product_categories' => array_filter(array_map('intval', (array) $_POST['product_categories'])), 'excluded_product_categories' => array_filter(array_map('intval', (array) $_POST['exclude_product_categories'])), 'exclude_sale_items' => isset($_POST['exclude_sale_items']), 'minimum_amount' => wc_format_decimal($_POST['minimum_amount']), 'maximum_amount' => wc_format_decimal($_POST['maximum_amount']), 'email_restrictions' => array_filter(array_map('trim', explode(',', wc_clean($_POST['customer_email'])))))); $coupon->save(); do_action('woocommerce_coupon_options_save', $post_id); }
/** * 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; }
/** * Compatibility function to use the new WC_Admin_Meta_Boxes class for the save_errors() function * * @since 1.0-1 * @return old save_errors function or new class */ public static function save_errors() { if (self::is_wc_version_gte_2_1()) { WC_Admin_Meta_Boxes::save_errors(); } else { woocommerce_meta_boxes_save_errors(); } }
/** * Save meta box data. * * @param int $post_id * @param WP_Post $post */ public static function save_variations($post_id, $post) { if (isset($_POST['variable_post_id'])) { $parent = wc_get_product($post_id); $max_loop = max(array_keys($_POST['variable_post_id'])); $data_store = $parent->get_data_store(); $data_store->sort_all_product_variations($parent->get_id()); for ($i = 0; $i <= $max_loop; $i++) { if (!isset($_POST['variable_post_id'][$i])) { continue; } $variation_id = absint($_POST['variable_post_id'][$i]); $variation = new WC_Product_Variation($variation_id); $errors = $variation->set_props(array('status' => isset($_POST['variable_enabled'][$i]) ? 'publish' : 'private', 'menu_order' => wc_clean($_POST['variation_menu_order'][$i]), 'regular_price' => wc_clean($_POST['variable_regular_price'][$i]), 'sale_price' => wc_clean($_POST['variable_sale_price'][$i]), 'virtual' => isset($_POST['variable_is_virtual'][$i]), 'downloadable' => isset($_POST['variable_is_downloadable'][$i]), 'date_on_sale_from' => wc_clean($_POST['variable_sale_price_dates_from'][$i]), 'date_on_sale_to' => wc_clean($_POST['variable_sale_price_dates_to'][$i]), 'description' => wp_kses_post(wc_sanitize_textarea($_POST['variable_description'][$i])), 'download_limit' => wc_clean($_POST['variable_download_limit'][$i]), 'download_expiry' => wc_clean($_POST['variable_download_expiry'][$i]), 'downloads' => self::prepare_downloads(isset($_POST['_wc_variation_file_names'][$variation_id]) ? $_POST['_wc_variation_file_names'][$variation_id] : array(), isset($_POST['_wc_variation_file_urls'][$variation_id]) ? $_POST['_wc_variation_file_urls'][$variation_id] : array(), isset($_POST['_wc_variation_file_hashes'][$variation_id]) ? $_POST['_wc_variation_file_hashes'][$variation_id] : array()), 'manage_stock' => isset($_POST['variable_manage_stock'][$i]), 'stock_quantity' => wc_clean($_POST['variable_stock'][$i]), 'backorders' => wc_clean($_POST['variable_backorders'][$i]), 'stock_status' => wc_clean($_POST['variable_stock_status'][$i]), 'image_id' => wc_clean($_POST['upload_image_id'][$i]), 'attributes' => self::prepare_set_attributes($parent->get_attributes(), 'attribute_', $i), 'sku' => isset($_POST['variable_sku'][$i]) ? wc_clean($_POST['variable_sku'][$i]) : '', 'weight' => isset($_POST['variable_weight'][$i]) ? wc_clean($_POST['variable_weight'][$i]) : '', 'length' => isset($_POST['variable_length'][$i]) ? wc_clean($_POST['variable_length'][$i]) : '', 'width' => isset($_POST['variable_width'][$i]) ? wc_clean($_POST['variable_width'][$i]) : '', 'height' => isset($_POST['variable_height'][$i]) ? wc_clean($_POST['variable_height'][$i]) : '', 'shipping_class_id' => wc_clean($_POST['variable_shipping_class'][$i]), 'tax_class' => wc_clean($_POST['variable_tax_class'][$i]))); if (is_wp_error($errors)) { WC_Admin_Meta_Boxes::add_error($errors->get_error_message()); } $variation->save(); do_action('woocommerce_save_product_variation', $variation_id, $i); } } }
/** * Sync the variable product with it's children. */ public static function sync($product_id) { global $wpdb; $children = get_posts(array('post_parent' => $product_id, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => 'publish')); // No published variations - product won't be purchasable. if (!$children) { update_post_meta($product_id, '_price', ''); delete_transient('wc_products_onsale'); if (is_admin() && 'publish' === get_post_status($product_id)) { WC_Admin_Meta_Boxes::add_error(__('This variable product has no active variations. Add or enable variations to allow this product to be purchased.', 'woocommerce')); } // Loop the variations } else { // Set the variable product to be virtual/downloadable if all children are virtual/downloadable foreach (array('_downloadable', '_virtual') as $meta_key) { $all_variations_yes = true; foreach ($children as $child_id) { if ('yes' != get_post_meta($child_id, $meta_key, true)) { $all_variations_yes = false; break; } } update_post_meta($product_id, $meta_key, true === $all_variations_yes ? 'yes' : 'no'); } // Main active prices $min_price = null; $max_price = null; $min_price_id = null; $max_price_id = null; // Regular prices $min_regular_price = null; $max_regular_price = null; $min_regular_price_id = null; $max_regular_price_id = null; // Sale prices $min_sale_price = null; $max_sale_price = null; $min_sale_price_id = null; $max_sale_price_id = null; foreach (array('price', 'regular_price', 'sale_price') as $price_type) { foreach ($children as $child_id) { $child_price = get_post_meta($child_id, '_' . $price_type, true); // Skip non-priced variations if ($child_price === '') { continue; } // Skip hidden variations if ('yes' === get_option('woocommerce_hide_out_of_stock_items')) { $stock = get_post_meta($child_id, '_stock', true); if ($stock !== "" && $stock <= get_option('woocommerce_notify_no_stock_amount')) { continue; } } // Find min price if (is_null(${"min_{$price_type}"}) || $child_price < ${"min_{$price_type}"}) { ${"min_{$price_type}"} = $child_price; ${"min_{$price_type}_id"} = $child_id; } // Find max price if ($child_price > ${"max_{$price_type}"}) { ${"max_{$price_type}"} = $child_price; ${"max_{$price_type}_id"} = $child_id; } } // Store prices update_post_meta($product_id, '_min_variation_' . $price_type, ${"min_{$price_type}"}); update_post_meta($product_id, '_max_variation_' . $price_type, ${"max_{$price_type}"}); // Store ids update_post_meta($product_id, '_min_' . $price_type . '_variation_id', ${"min_{$price_type}_id"}); update_post_meta($product_id, '_max_' . $price_type . '_variation_id', ${"max_{$price_type}_id"}); } // Sync _price meta delete_post_meta($product_id, '_price'); add_post_meta($product_id, '_price', $min_price, false); add_post_meta($product_id, '_price', $max_price, false); delete_transient('wc_products_onsale'); // Sync attributes self::sync_attributes($product_id, $children); do_action('woocommerce_variable_product_sync', $product_id, $children); } }
/** * Sync the variable product with it's children */ public static function sync($product_id) { global $wpdb; $children = get_posts(array('post_parent' => $product_id, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => 'publish')); // No published variations - update parent post status. Use $wpdb to prevent endless loop on save_post hooks. if (!$children && get_post_status($product_id) == 'publish') { $wpdb->update($wpdb->posts, array('post_status' => 'draft'), array('ID' => $product_id)); if (is_admin()) { WC_Admin_Meta_Boxes::add_error(__('This variable product has no active variations so cannot be published. Changing status to draft.', 'woocommerce')); } // Loop the variations } else { // Set the variable product to be virtual/downloadable if all children are virtual/downloadable foreach (array('_downloadable', '_virtual') as $meta_key) { $all_variations_yes = true; foreach ($children as $child_id) { if ('yes' != get_post_meta($child_id, $meta_key, true)) { $all_variations_yes = false; break; } } update_post_meta($product_id, $meta_key, true === $all_variations_yes ? 'yes' : 'no'); } // Main active prices $min_price = null; $max_price = null; $min_price_id = null; $max_price_id = null; // Regular prices $min_regular_price = null; $max_regular_price = null; $min_regular_price_id = null; $max_regular_price_id = null; // Sale prices $min_sale_price = null; $max_sale_price = null; $min_sale_price_id = null; $max_sale_price_id = null; foreach (array('price', 'regular_price', 'sale_price') as $price_type) { foreach ($children as $child_id) { $child_price = get_post_meta($child_id, '_' . $price_type, true); // Skip non-priced variations if ($child_price === '') { continue; } // Skip hidden variations if ('yes' === get_option('woocommerce_hide_out_of_stock_items')) { $stock = get_post_meta($child_id, '_stock', true); if ($stock !== "" && $stock <= get_option('woocommerce_notify_no_stock_amount')) { continue; } } // Find min price if (is_null(${"min_{$price_type}"}) || $child_price < ${"min_{$price_type}"}) { ${"min_{$price_type}"} = $child_price; ${"min_{$price_type}_id"} = $child_id; } // Find max price if ($child_price > ${"max_{$price_type}"}) { ${"max_{$price_type}"} = $child_price; ${"max_{$price_type}_id"} = $child_id; } } // Store prices update_post_meta($product_id, '_min_variation_' . $price_type, ${"min_{$price_type}"}); update_post_meta($product_id, '_max_variation_' . $price_type, ${"max_{$price_type}"}); // Store ids update_post_meta($product_id, '_min_' . $price_type . '_variation_id', ${"min_{$price_type}_id"}); update_post_meta($product_id, '_max_' . $price_type . '_variation_id', ${"max_{$price_type}_id"}); } // The VARIABLE PRODUCT price should equal the min price of any type update_post_meta($product_id, '_price', $min_price); delete_transient('wc_products_onsale'); // Sync attributes self::sync_attributes($product_id, $children); do_action('woocommerce_variable_product_sync', $product_id, $children); } }
/** * Sync the variable product with it's children */ public static function sync($product_id) { global $wpdb; $children = get_posts(array('post_parent' => $product_id, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => 'publish')); // No published variations - update parent post status. Use $wpdb to prevent endless loop on save_post hooks. if (!$children && get_post_status($product_id) == 'publish') { $wpdb->update($wpdb->posts, array('post_status' => 'draft'), array('ID' => $product_id)); if (is_admin()) { WC_Admin_Meta_Boxes::add_error(__('This variable product has no active variations so cannot be published. Changing status to draft.', 'woocommerce')); } // Loop the variations } else { $min_price = null; $max_price = null; $min_price_id = null; $max_price_id = null; foreach ($children as $child) { $child_price = get_post_meta($child, '_price', true); if ($child_price === '') { continue; } if ($child_price > $max_price) { $max_price = $child_price; $max_price_id = $child; } if (is_null($min_price) || $child_price < $min_price) { $min_price = $child_price; $min_price_id = $child; } } // Store prices update_post_meta($product_id, '_price', $min_price); update_post_meta($product_id, '_min_variation_price', $min_price); update_post_meta($product_id, '_max_variation_price', $max_price); // Store IDS update_post_meta($product_id, '_min_price_variation_id', $min_price_id); update_post_meta($product_id, '_max_price_variation_id', $max_price_id); do_action('woocommerce_variable_product_sync', $product_id, $children); wc_delete_product_transients($product_id); } }
/** * Add and return admin errors. * * @param string $error * @return string */ private function add_admin_error($error) { WC_Admin_Meta_Boxes::add_error($error); return $error; }
/** * Add admin errors. * * @param string $error * @return string */ public function add_admin_error($error) { WC_Admin_Meta_Boxes::add_error($error); }
public function easy_booking_save_variation_booking_options($variation_id, $i) { $is_bookable = isset($_POST['_var_booking_option'][$i]) ? 'yes' : ''; $data = array('booking_min' => $_POST['_var_booking_min'][$i], 'booking_max' => $_POST['_var_booking_max'][$i], 'first_available_date' => $_POST['_var_first_available_date'][$i]); foreach ($data as $name => $value) { switch ($value) { case '': ${$name} = ''; break; case 0: ${$name} = '0'; break; default: ${$name} = absint($value); break; } } if ($booking_min != 0 && $booking_max != 0 && $booking_min > $booking_max) { WC_Admin_Meta_Boxes::add_error(__('Minimum booking duration must be inferior to maximum booking duration', 'easy_booking')); } else { update_post_meta($variation_id, '_booking_min', $booking_min); update_post_meta($variation_id, '_booking_max', $booking_max); } update_post_meta($variation_id, '_first_available_date', $first_available_date); update_post_meta($variation_id, '_booking_option', $is_bookable); }
/** * return a list of available storedfiles */ public function get_storedfiles() { if (is_null($this->storedfiles)) { // check of er een connectie is if (!$this->connected) { return false; } // Set authentication $args = array('reject_unsafe_urls' => false, 'headers' => array('Authorization' => 'Basic ' . base64_encode($this->contractname . ':' . $this->contractpassword))); $url = str_replace('ACCOUNTKEY', $this->accountkey, WC_BooXtream::listepubfilesurl) . '?limit=0'; $response = wp_safe_remote_get($url, $args); if (is_array($response) && 200 === $response['response']['code']) { $result = json_decode($response['body']); if (is_object($result) && isset($result->message->response)) { $this->storedfiles = array('' => __('Select an e-book', 'woocommerce_booxtream')); foreach ($result->message->response as $file) { if (isset($file->StoredFileKey)) { $this->storedfiles[$file->StoredFileKey] = $file->FileName; } } /* * Save storedfiles */ update_option($this->plugin_id . $this->id . '_storedfiles', $this->storedfiles); } else { WC_Admin_Meta_Boxes::add_error(__('Could not retrieve list of stored files', 'woocommerce_booxtream')); return false; } } else { WC_Admin_Meta_Boxes::add_error(__('Could not retrieve list of stored files', 'woocommerce_booxtream')); return false; } } return $this->storedfiles; }
/** * 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; }
/** * Process, verify and save product data * @param int $post_id * @return void */ public static function process_meta($post_id) { // Min container size $min_qty = intval(get_post_meta($post_id, '_mnm_container_size', true)); // Max container size $max_qty = isset($_POST['mnm_max_container_size']) ? intval(wc_clean($_POST['mnm_max_container_size'])) : $min_qty; if ($max_qty > 0 && $min_qty > $max_qty) { $max_qty = $min_qty; WC_Admin_Meta_Boxes::add_error(__('The maximum Mix & Match container size cannot be smaller than the minimum container size.', 'woocommerce-mix-and-match-min-max-quantities')); } update_post_meta($post_id, '_mnm_max_container_size', $max_qty); return $post_id; }
/** * Save meta box data */ public static function save($post_id, $post) { global $wpdb; // Ensure coupon code is correctly formatted $post->post_title = apply_filters('woocommerce_coupon_code', $post->post_title); $wpdb->update($wpdb->posts, array('post_title' => $post->post_title), array('ID' => $post_id)); // Check for dupe coupons $coupon_found = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT {$wpdb->posts}.ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE {$wpdb->posts}.post_type = 'shop_coupon'\n\t\t\tAND {$wpdb->posts}.post_status = 'publish'\n\t\t\tAND {$wpdb->posts}.post_title = '%s'\n\t\t\tAND {$wpdb->posts}.ID != %s\n\t\t ", $post->post_title, $post_id)); if ($coupon_found) { WC_Admin_Meta_Boxes::add_error(__('Coupon code already exists - customers will use the latest coupon with this code.', 'woocommerce')); } // Add/Replace data to array $type = wc_clean($_POST['discount_type']); $amount = wc_format_decimal($_POST['coupon_amount']); $usage_limit = empty($_POST['usage_limit']) ? '' : absint($_POST['usage_limit']); $usage_limit_per_user = empty($_POST['usage_limit_per_user']) ? '' : absint($_POST['usage_limit_per_user']); $limit_usage_to_x_items = empty($_POST['limit_usage_to_x_items']) ? '' : absint($_POST['limit_usage_to_x_items']); $individual_use = isset($_POST['individual_use']) ? 'yes' : 'no'; $expiry_date = wc_clean($_POST['expiry_date']); $apply_before_tax = isset($_POST['apply_before_tax']) ? 'yes' : 'no'; $free_shipping = isset($_POST['free_shipping']) ? 'yes' : 'no'; $exclude_sale_items = isset($_POST['exclude_sale_items']) ? 'yes' : 'no'; $minimum_amount = wc_format_decimal($_POST['minimum_amount']); $maximum_amount = wc_format_decimal($_POST['maximum_amount']); $customer_email = array_filter(array_map('trim', explode(',', wc_clean($_POST['customer_email'])))); if (isset($_POST['product_ids'])) { $product_ids = implode(',', array_filter(array_map('intval', (array) $_POST['product_ids']))); } else { $product_ids = ''; } if (isset($_POST['exclude_product_ids'])) { $exclude_product_ids = implode(',', array_filter(array_map('intval', (array) $_POST['exclude_product_ids']))); } else { $exclude_product_ids = ''; } $product_categories = isset($_POST['product_categories']) ? array_map('intval', $_POST['product_categories']) : array(); $exclude_product_categories = isset($_POST['exclude_product_categories']) ? array_map('intval', $_POST['exclude_product_categories']) : array(); // Save update_post_meta($post_id, 'discount_type', $type); update_post_meta($post_id, 'coupon_amount', $amount); update_post_meta($post_id, 'individual_use', $individual_use); update_post_meta($post_id, 'product_ids', $product_ids); update_post_meta($post_id, 'exclude_product_ids', $exclude_product_ids); update_post_meta($post_id, 'usage_limit', $usage_limit); update_post_meta($post_id, 'usage_limit_per_user', $usage_limit_per_user); update_post_meta($post_id, 'limit_usage_to_x_items', $limit_usage_to_x_items); update_post_meta($post_id, 'expiry_date', $expiry_date); update_post_meta($post_id, 'apply_before_tax', $apply_before_tax); update_post_meta($post_id, 'free_shipping', $free_shipping); update_post_meta($post_id, 'exclude_sale_items', $exclude_sale_items); update_post_meta($post_id, 'product_categories', $product_categories); update_post_meta($post_id, 'exclude_product_categories', $exclude_product_categories); update_post_meta($post_id, 'minimum_amount', $minimum_amount); update_post_meta($post_id, 'maximum_amount', $maximum_amount); update_post_meta($post_id, 'customer_email', $customer_email); do_action('woocommerce_coupon_options_save', $post_id); }
/** * Save meta box data */ public static function save($post_id, $post) { global $wpdb; // Add any default post meta add_post_meta($post_id, 'total_sales', '0', true); // Get types $product_type = empty($_POST['product-type']) ? 'simple' : sanitize_title(stripslashes($_POST['product-type'])); $is_downloadable = isset($_POST['_downloadable']) ? 'yes' : 'no'; $is_virtual = isset($_POST['_virtual']) ? 'yes' : 'no'; // Product type + Downloadable/Virtual wp_set_object_terms($post_id, $product_type, 'product_type'); update_post_meta($post_id, '_downloadable', $is_downloadable); update_post_meta($post_id, '_virtual', $is_virtual); // Update post meta if (isset($_POST['_regular_price'])) { update_post_meta($post_id, '_regular_price', $_POST['_regular_price'] === '' ? '' : wc_format_decimal($_POST['_regular_price'])); } if (isset($_POST['_sale_price'])) { update_post_meta($post_id, '_sale_price', $_POST['_sale_price'] === '' ? '' : wc_format_decimal($_POST['_sale_price'])); } if (isset($_POST['_tax_status'])) { update_post_meta($post_id, '_tax_status', stripslashes($_POST['_tax_status'])); } if (isset($_POST['_tax_class'])) { update_post_meta($post_id, '_tax_class', stripslashes($_POST['_tax_class'])); } if (isset($_POST['_visibility'])) { update_post_meta($post_id, '_visibility', stripslashes($_POST['_visibility'])); } if (isset($_POST['_purchase_note'])) { update_post_meta($post_id, '_purchase_note', stripslashes($_POST['_purchase_note'])); } update_post_meta($post_id, '_featured', isset($_POST['_featured']) ? 'yes' : 'no'); // Dimensions if ($is_virtual == 'no') { if (isset($_POST['_weight'])) { update_post_meta($post_id, '_weight', $_POST['_weight'] === '' ? '' : wc_format_decimal($_POST['_weight'])); } if (isset($_POST['_length'])) { update_post_meta($post_id, '_length', $_POST['_length'] === '' ? '' : wc_format_decimal($_POST['_length'])); } if (isset($_POST['_width'])) { update_post_meta($post_id, '_width', $_POST['_width'] === '' ? '' : wc_format_decimal($_POST['_width'])); } if (isset($_POST['_height'])) { update_post_meta($post_id, '_height', $_POST['_height'] === '' ? '' : wc_format_decimal($_POST['_height'])); } } else { update_post_meta($post_id, '_weight', ''); update_post_meta($post_id, '_length', ''); update_post_meta($post_id, '_width', ''); update_post_meta($post_id, '_height', ''); } // Save shipping class $product_shipping_class = $_POST['product_shipping_class'] > 0 && $product_type != 'external' ? absint($_POST['product_shipping_class']) : ''; wp_set_object_terms($post_id, $product_shipping_class, 'product_shipping_class'); // Unique SKU $sku = get_post_meta($post_id, '_sku', true); $new_sku = wc_clean(stripslashes($_POST['_sku'])); if ($new_sku == '') { update_post_meta($post_id, '_sku', ''); } elseif ($new_sku !== $sku) { if (!empty($new_sku)) { if ($wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\tSELECT {$wpdb->posts}.ID\n\t\t\t\t\t FROM {$wpdb->posts}\n\t\t\t\t\t LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)\n\t\t\t\t\t WHERE {$wpdb->posts}.post_type = 'product'\n\t\t\t\t\t AND {$wpdb->posts}.post_status = 'publish'\n\t\t\t\t\t AND {$wpdb->postmeta}.meta_key = '_sku' AND {$wpdb->postmeta}.meta_value = '%s'\n\t\t\t\t\t ", $new_sku))) { WC_Admin_Meta_Boxes::add_error(__('Product SKU must be unique.', 'woocommerce')); } else { update_post_meta($post_id, '_sku', $new_sku); } } else { update_post_meta($post_id, '_sku', ''); } } // Save Attributes $attributes = array(); if (isset($_POST['attribute_names']) && isset($_POST['attribute_values'])) { $attribute_names = $_POST['attribute_names']; $attribute_values = $_POST['attribute_values']; if (isset($_POST['attribute_visibility'])) { $attribute_visibility = $_POST['attribute_visibility']; } if (isset($_POST['attribute_variation'])) { $attribute_variation = $_POST['attribute_variation']; } $attribute_is_taxonomy = $_POST['attribute_is_taxonomy']; $attribute_position = $_POST['attribute_position']; $attribute_names_count = sizeof($attribute_names); for ($i = 0; $i < $attribute_names_count; $i++) { if (!$attribute_names[$i]) { continue; } $is_visible = isset($attribute_visibility[$i]) ? 1 : 0; $is_variation = isset($attribute_variation[$i]) ? 1 : 0; $is_taxonomy = $attribute_is_taxonomy[$i] ? 1 : 0; if ($is_taxonomy) { if (isset($attribute_values[$i])) { // Select based attributes - Format values (posted values are slugs) if (is_array($attribute_values[$i])) { $values = array_map('sanitize_title', $attribute_values[$i]); // Text based attributes - Posted values are term names - don't change to slugs } else { $values = array_map('stripslashes', array_map('strip_tags', explode(WC_DELIMITER, $attribute_values[$i]))); } // Remove empty items in the array $values = array_filter($values, 'strlen'); } else { $values = array(); } // Update post terms if (taxonomy_exists($attribute_names[$i])) { wp_set_object_terms($post_id, $values, $attribute_names[$i]); } if ($values) { // Add attribute to array, but don't set values $attributes[sanitize_title($attribute_names[$i])] = array('name' => wc_clean($attribute_names[$i]), 'value' => '', 'position' => $attribute_position[$i], 'is_visible' => $is_visible, 'is_variation' => $is_variation, 'is_taxonomy' => $is_taxonomy); } } elseif (isset($attribute_values[$i])) { // Text based, separate by pipe $values = implode(' ' . WC_DELIMITER . ' ', array_map('wc_clean', explode(WC_DELIMITER, $attribute_values[$i]))); // Custom attribute - Add attribute to array and set the values $attributes[sanitize_title($attribute_names[$i])] = array('name' => wc_clean($attribute_names[$i]), 'value' => $values, 'position' => $attribute_position[$i], 'is_visible' => $is_visible, 'is_variation' => $is_variation, 'is_taxonomy' => $is_taxonomy); } } } if (!function_exists('attributes_cmp')) { function attributes_cmp($a, $b) { if ($a['position'] == $b['position']) { return 0; } return $a['position'] < $b['position'] ? -1 : 1; } } uasort($attributes, 'attributes_cmp'); update_post_meta($post_id, '_product_attributes', $attributes); // Sales and prices if (in_array($product_type, array('variable', 'grouped'))) { // Variable and grouped products have no prices update_post_meta($post_id, '_regular_price', ''); update_post_meta($post_id, '_sale_price', ''); update_post_meta($post_id, '_sale_price_dates_from', ''); update_post_meta($post_id, '_sale_price_dates_to', ''); update_post_meta($post_id, '_price', ''); } else { $date_from = isset($_POST['_sale_price_dates_from']) ? $_POST['_sale_price_dates_from'] : ''; $date_to = isset($_POST['_sale_price_dates_to']) ? $_POST['_sale_price_dates_to'] : ''; // Dates if ($date_from) { update_post_meta($post_id, '_sale_price_dates_from', strtotime($date_from)); } else { update_post_meta($post_id, '_sale_price_dates_from', ''); } if ($date_to) { update_post_meta($post_id, '_sale_price_dates_to', strtotime($date_to)); } else { update_post_meta($post_id, '_sale_price_dates_to', ''); } if ($date_to && !$date_from) { update_post_meta($post_id, '_sale_price_dates_from', strtotime('NOW', current_time('timestamp'))); } // Update price if on sale if ($_POST['_sale_price'] !== '' && $date_to == '' && $date_from == '') { update_post_meta($post_id, '_price', wc_format_decimal($_POST['_sale_price'])); } else { update_post_meta($post_id, '_price', $_POST['_regular_price'] === '' ? '' : wc_format_decimal($_POST['_regular_price'])); } if ($_POST['_sale_price'] !== '' && $date_from && strtotime($date_from) < strtotime('NOW', current_time('timestamp'))) { update_post_meta($post_id, '_price', wc_format_decimal($_POST['_sale_price'])); } if ($date_to && strtotime($date_to) < strtotime('NOW', current_time('timestamp'))) { update_post_meta($post_id, '_price', $_POST['_regular_price'] === '' ? '' : wc_format_decimal($_POST['_regular_price'])); update_post_meta($post_id, '_sale_price_dates_from', ''); update_post_meta($post_id, '_sale_price_dates_to', ''); } } // Update parent if grouped so price sorting works and stays in sync with the cheapest child if ($post->post_parent > 0 || $product_type == 'grouped' || $_POST['previous_parent_id'] > 0) { $clear_parent_ids = array(); if ($post->post_parent > 0) { $clear_parent_ids[] = $post->post_parent; } if ($product_type == 'grouped') { $clear_parent_ids[] = $post_id; } if ($_POST['previous_parent_id'] > 0) { $clear_parent_ids[] = absint($_POST['previous_parent_id']); } if ($clear_parent_ids) { foreach ($clear_parent_ids as $clear_id) { $children_by_price = get_posts(array('post_parent' => $clear_id, 'orderby' => 'meta_value_num', 'order' => 'asc', 'meta_key' => '_price', 'posts_per_page' => 1, 'post_type' => 'product', 'fields' => 'ids')); if ($children_by_price) { foreach ($children_by_price as $child) { $child_price = get_post_meta($child, '_price', true); update_post_meta($clear_id, '_price', $child_price); } } // Clear cache/transients wc_delete_product_transients($clear_id); } } } // Sold Individually if (!empty($_POST['_sold_individually'])) { update_post_meta($post_id, '_sold_individually', 'yes'); } else { update_post_meta($post_id, '_sold_individually', ''); } // Stock Data if (get_option('woocommerce_manage_stock') == 'yes') { if ($product_type == 'grouped') { update_post_meta($post_id, '_manage_stock', 'no'); update_post_meta($post_id, '_backorders', 'no'); update_post_meta($post_id, '_stock', ''); wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status'])); } elseif ($product_type == 'external') { update_post_meta($post_id, '_manage_stock', 'no'); update_post_meta($post_id, '_backorders', 'no'); update_post_meta($post_id, '_stock', ''); wc_update_product_stock_status($post_id, 'instock'); } elseif (!empty($_POST['_manage_stock'])) { update_post_meta($post_id, '_manage_stock', 'yes'); update_post_meta($post_id, '_backorders', wc_clean($_POST['_backorders'])); wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status'])); wc_update_product_stock($post_id, intval($_POST['_stock'])); } else { // Don't manage stock update_post_meta($post_id, '_manage_stock', 'no'); update_post_meta($post_id, '_backorders', wc_clean($_POST['_backorders'])); update_post_meta($post_id, '_stock', ''); wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status'])); } } else { wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status'])); } // Upsells if (isset($_POST['upsell_ids'])) { $upsells = array(); $ids = $_POST['upsell_ids']; foreach ($ids as $id) { if ($id && $id > 0) { $upsells[] = $id; } } update_post_meta($post_id, '_upsell_ids', $upsells); } else { delete_post_meta($post_id, '_upsell_ids'); } // Cross sells if (isset($_POST['crosssell_ids'])) { $crosssells = array(); $ids = $_POST['crosssell_ids']; foreach ($ids as $id) { if ($id && $id > 0) { $crosssells[] = $id; } } update_post_meta($post_id, '_crosssell_ids', $crosssells); } else { delete_post_meta($post_id, '_crosssell_ids'); } // Downloadable options if ($is_downloadable == 'yes') { $_download_limit = absint($_POST['_download_limit']); if (!$_download_limit) { $_download_limit = ''; // 0 or blank = unlimited } $_download_expiry = absint($_POST['_download_expiry']); if (!$_download_expiry) { $_download_expiry = ''; // 0 or blank = unlimited } // file paths will be stored in an array keyed off md5(file path) $files = array(); if (isset($_POST['_wc_file_urls'])) { $file_names = isset($_POST['_wc_file_names']) ? array_map('wc_clean', $_POST['_wc_file_names']) : array(); $file_urls = isset($_POST['_wc_file_urls']) ? array_map('wc_clean', $_POST['_wc_file_urls']) : array(); $file_url_size = sizeof($file_urls); for ($i = 0; $i < $file_url_size; $i++) { if (!empty($file_urls[$i])) { $files[md5($file_urls[$i])] = array('name' => $file_names[$i], 'file' => $file_urls[$i]); } } } // grant permission to any newly added files on any existing orders for this product prior to saving do_action('woocommerce_process_product_file_download_paths', $post_id, 0, $files); update_post_meta($post_id, '_downloadable_files', $files); update_post_meta($post_id, '_download_limit', $_download_limit); update_post_meta($post_id, '_download_expiry', $_download_expiry); if (isset($_POST['_download_type'])) { update_post_meta($post_id, '_download_type', wc_clean($_POST['_download_type'])); } } // Product url if ($product_type == 'external') { if (isset($_POST['_product_url'])) { update_post_meta($post_id, '_product_url', esc_attr($_POST['_product_url'])); } if (isset($_POST['_button_text'])) { update_post_meta($post_id, '_button_text', esc_attr($_POST['_button_text'])); } } // Save variations if ($product_type == 'variable') { self::save_variations($post_id, $post); } // Do action for product type do_action('woocommerce_process_product_meta_' . $product_type, $post_id); // Clear cache/transients wc_delete_product_transients($post_id); }
/** * Check if we're saving, the trigger an action based on the post type * * @param int $post_id * @param object $post */ public function save_meta_boxes($post_id, $post) { // $post_id and $post are required if (empty($post_id) || empty($post) || self::$saved_meta_boxes) { return; } // Dont' save meta boxes for revisions or autosaves if (defined('DOING_AUTOSAVE') || is_int(wp_is_post_revision($post)) || is_int(wp_is_post_autosave($post))) { return; } // Check the nonce if (empty($_POST['woocommerce_meta_nonce']) || !wp_verify_nonce($_POST['woocommerce_meta_nonce'], 'woocommerce_save_data')) { return; } // Check the post being saved == the $post_id to prevent triggering this call for other save_post events if (empty($_POST['post_ID']) || $_POST['post_ID'] != $post_id) { return; } // Check user has permission to edit if (!current_user_can('edit_post', $post_id)) { return; } // We need this save event to run once to avoid potential endless loops. This would have been perfect: // remove_action( current_filter(), __METHOD__ ); // But cannot be used due to https://github.com/woothemes/woocommerce/issues/6485 // When that is patched in core we cna use the above. For now: self::$saved_meta_boxes = true; // Check the post type if (in_array($post->post_type, wc_get_order_types('order-meta-boxes'))) { do_action('woocommerce_process_shop_order_meta', $post_id, $post); } elseif (in_array($post->post_type, array('product', 'shop_coupon'))) { do_action('woocommerce_process_' . $post->post_type . '_meta', $post_id, $post); } }
/** * Save subscription options. * * @param int $post_id * @return void */ public static function process_product_meta($post_id) { // Get type. $product_type = empty($_POST['product-type']) ? 'simple' : sanitize_title(stripslashes($_POST['product-type'])); $supported_types = WCS_ATT()->get_supported_product_types(); if (in_array($product_type, $supported_types)) { // Save subscription scheme options. if (isset($_POST['wcsatt_schemes'])) { $posted_schemes = stripslashes_deep($_POST['wcsatt_schemes']); $scheme_ids = array(); $clean_schemes = array(); foreach ($posted_schemes as $posted_scheme) { // Format subscription prices. if (isset($posted_scheme['subscription_regular_price'])) { $posted_scheme['subscription_regular_price'] = $posted_scheme['subscription_regular_price'] === '' ? '' : wc_format_decimal($posted_scheme['subscription_regular_price']); } if (isset($posted_scheme['subscription_sale_price'])) { $posted_scheme['subscription_sale_price'] = $posted_scheme['subscription_sale_price'] === '' ? '' : wc_format_decimal($posted_scheme['subscription_sale_price']); } if ('' !== $posted_scheme['subscription_sale_price']) { $posted_scheme['subscription_price'] = $posted_scheme['subscription_sale_price']; } else { $posted_scheme['subscription_price'] = $posted_scheme['subscription_regular_price'] === '' ? '' : $posted_scheme['subscription_regular_price']; } // Format subscription discount. if (isset($posted_scheme['subscription_discount'])) { if (is_numeric($posted_scheme['subscription_discount'])) { $discount = (double) wc_format_decimal($posted_scheme['subscription_discount']); if ($discount < 0 || $discount > 100) { WC_Admin_Meta_Boxes::add_error(__('Please enter positive subscription discount values, between 0-100.', WCS_ATT::TEXT_DOMAIN)); $posted_scheme['subscription_discount'] = ''; } else { $posted_scheme['subscription_discount'] = $discount; } } else { $posted_scheme['subscription_discount'] = ''; } } else { $posted_scheme['subscription_discount'] = ''; } // Validate price override method. if (isset($posted_scheme['subscription_pricing_method']) && $posted_scheme['subscription_pricing_method'] === 'override') { if ($posted_scheme['subscription_price'] === '' && $posted_scheme['subscription_regular_price'] === '') { $posted_scheme['subscription_pricing_method'] = 'inherit'; } } else { $posted_scheme['subscription_pricing_method'] = 'inherit'; } // Construct scheme id. $scheme_id = $posted_scheme['subscription_period_interval'] . '_' . $posted_scheme['subscription_period'] . '_' . $posted_scheme['subscription_length']; if (in_array($scheme_id, $scheme_ids)) { continue; } $posted_scheme['id'] = $scheme_id; $scheme_ids[] = $scheme_id; $clean_schemes[] = $posted_scheme; } update_post_meta($post_id, '_wcsatt_schemes', $clean_schemes); } else { delete_post_meta($post_id, '_wcsatt_schemes'); } // Save default status if (isset($_POST['_wcsatt_default_status'])) { update_post_meta($post_id, '_wcsatt_default_status', stripslashes($_POST['_wcsatt_default_status'])); } // Save one-time status $force_subscription = isset($_POST['_wcsatt_force_subscription']) ? 'yes' : 'no'; update_post_meta($post_id, '_wcsatt_force_subscription', $force_subscription); // Save prompt if (!empty($_POST['_wcsatt_subscription_prompt'])) { $prompt = wp_kses_post(stripslashes($_POST['_wcsatt_subscription_prompt'])); update_post_meta($post_id, '_wcsatt_subscription_prompt', $prompt); } else { delete_post_meta($post_id, '_wcsatt_subscription_prompt'); } } else { delete_post_meta($post_id, '_wcsatt_schemes'); delete_post_meta($post_id, '_wcsatt_force_subscription'); delete_post_meta($post_id, '_wcsatt_default_status'); delete_post_meta($post_id, '_wcsatt_subscription_prompt'); } }
/** * Save meta box data * * @deprecated 2.4.0 Deprecated in favor to WC_AJAX::save_variations() */ public static function save_variations($post_id, $post) { global $wpdb; $attributes = (array) maybe_unserialize(get_post_meta($post_id, '_product_attributes', true)); if (isset($_POST['variable_sku'])) { $variable_post_id = $_POST['variable_post_id']; $variable_sku = $_POST['variable_sku']; $variable_regular_price = $_POST['variable_regular_price']; $variable_sale_price = $_POST['variable_sale_price']; $upload_image_id = $_POST['upload_image_id']; $variable_download_limit = $_POST['variable_download_limit']; $variable_download_expiry = $_POST['variable_download_expiry']; $variable_shipping_class = $_POST['variable_shipping_class']; $variable_tax_class = isset($_POST['variable_tax_class']) ? $_POST['variable_tax_class'] : array(); $variable_menu_order = $_POST['variation_menu_order']; $variable_sale_price_dates_from = $_POST['variable_sale_price_dates_from']; $variable_sale_price_dates_to = $_POST['variable_sale_price_dates_to']; $variable_weight = isset($_POST['variable_weight']) ? $_POST['variable_weight'] : array(); $variable_length = isset($_POST['variable_length']) ? $_POST['variable_length'] : array(); $variable_width = isset($_POST['variable_width']) ? $_POST['variable_width'] : array(); $variable_height = isset($_POST['variable_height']) ? $_POST['variable_height'] : array(); $variable_enabled = isset($_POST['variable_enabled']) ? $_POST['variable_enabled'] : array(); $variable_is_virtual = isset($_POST['variable_is_virtual']) ? $_POST['variable_is_virtual'] : array(); $variable_is_downloadable = isset($_POST['variable_is_downloadable']) ? $_POST['variable_is_downloadable'] : array(); $variable_manage_stock = isset($_POST['variable_manage_stock']) ? $_POST['variable_manage_stock'] : array(); $variable_stock = isset($_POST['variable_stock']) ? $_POST['variable_stock'] : array(); $variable_backorders = isset($_POST['variable_backorders']) ? $_POST['variable_backorders'] : array(); $variable_stock_status = isset($_POST['variable_stock_status']) ? $_POST['variable_stock_status'] : array(); $variable_description = isset($_POST['variable_description']) ? $_POST['variable_description'] : array(); $max_loop = max(array_keys($_POST['variable_post_id'])); for ($i = 0; $i <= $max_loop; $i++) { if (!isset($variable_post_id[$i])) { continue; } $variation_id = absint($variable_post_id[$i]); // Checkboxes $is_virtual = isset($variable_is_virtual[$i]) ? 'yes' : 'no'; $is_downloadable = isset($variable_is_downloadable[$i]) ? 'yes' : 'no'; $post_status = isset($variable_enabled[$i]) ? 'publish' : 'private'; $manage_stock = isset($variable_manage_stock[$i]) ? 'yes' : 'no'; // Generate a useful post title $variation_post_title = sprintf(__('Variation #%s of %s', 'woocommerce'), absint($variation_id), esc_html(get_the_title($post_id))); // Update or Add post if (!$variation_id) { $variation = array('post_title' => $variation_post_title, 'post_content' => '', 'post_status' => $post_status, 'post_author' => get_current_user_id(), 'post_parent' => $post_id, 'post_type' => 'product_variation', 'menu_order' => $variable_menu_order[$i]); $variation_id = wp_insert_post($variation); do_action('woocommerce_create_product_variation', $variation_id); } else { $wpdb->update($wpdb->posts, array('post_status' => $post_status, 'post_title' => $variation_post_title, 'menu_order' => $variable_menu_order[$i]), array('ID' => $variation_id)); do_action('woocommerce_update_product_variation', $variation_id); } // Only continue if we have a variation ID if (!$variation_id) { continue; } // Unique SKU $sku = get_post_meta($variation_id, '_sku', true); $new_sku = wc_clean(stripslashes($variable_sku[$i])); if ('' == $new_sku) { update_post_meta($variation_id, '_sku', ''); } elseif ($new_sku !== $sku) { if (!empty($new_sku)) { $unique_sku = wc_product_has_unique_sku($variation_id, $new_sku); if (!$unique_sku) { WC_Admin_Meta_Boxes::add_error(__('Variation SKU must be unique.', 'woocommerce')); } else { update_post_meta($variation_id, '_sku', $new_sku); } } else { update_post_meta($variation_id, '_sku', ''); } } // Update post meta update_post_meta($variation_id, '_thumbnail_id', absint($upload_image_id[$i])); update_post_meta($variation_id, '_virtual', wc_clean($is_virtual)); update_post_meta($variation_id, '_downloadable', wc_clean($is_downloadable)); if (isset($variable_weight[$i])) { update_post_meta($variation_id, '_weight', '' === $variable_weight[$i] ? '' : wc_format_decimal($variable_weight[$i])); } if (isset($variable_length[$i])) { update_post_meta($variation_id, '_length', '' === $variable_length[$i] ? '' : wc_format_decimal($variable_length[$i])); } if (isset($variable_width[$i])) { update_post_meta($variation_id, '_width', '' === $variable_width[$i] ? '' : wc_format_decimal($variable_width[$i])); } if (isset($variable_height[$i])) { update_post_meta($variation_id, '_height', '' === $variable_height[$i] ? '' : wc_format_decimal($variable_height[$i])); } // Stock handling update_post_meta($variation_id, '_manage_stock', $manage_stock); // Only update stock status to user setting if changed by the user, but do so before looking at stock levels at variation level if (!empty($variable_stock_status[$i])) { wc_update_product_stock_status($variation_id, $variable_stock_status[$i]); } if ('yes' === $manage_stock) { update_post_meta($variation_id, '_backorders', wc_clean($variable_backorders[$i])); wc_update_product_stock($variation_id, wc_stock_amount($variable_stock[$i])); } else { delete_post_meta($variation_id, '_backorders'); delete_post_meta($variation_id, '_stock'); } // Price handling $regular_price = wc_format_decimal($variable_regular_price[$i]); $sale_price = $variable_sale_price[$i] === '' ? '' : wc_format_decimal($variable_sale_price[$i]); $date_from = wc_clean($variable_sale_price_dates_from[$i]); $date_to = wc_clean($variable_sale_price_dates_to[$i]); update_post_meta($variation_id, '_regular_price', $regular_price); update_post_meta($variation_id, '_sale_price', $sale_price); // Save Dates update_post_meta($variation_id, '_sale_price_dates_from', $date_from ? strtotime($date_from) : ''); update_post_meta($variation_id, '_sale_price_dates_to', $date_to ? strtotime($date_to) : ''); if ($date_to && !$date_from) { update_post_meta($variation_id, '_sale_price_dates_from', strtotime('NOW', current_time('timestamp'))); } // Update price if on sale if ('' !== $sale_price && '' === $date_to && '' === $date_from) { update_post_meta($variation_id, '_price', $sale_price); } else { update_post_meta($variation_id, '_price', $regular_price); } if ('' !== $sale_price && $date_from && strtotime($date_from) < strtotime('NOW', current_time('timestamp'))) { update_post_meta($variation_id, '_price', $sale_price); } if ($date_to && strtotime($date_to) < strtotime('NOW', current_time('timestamp'))) { update_post_meta($variation_id, '_price', $regular_price); update_post_meta($variation_id, '_sale_price_dates_from', ''); update_post_meta($variation_id, '_sale_price_dates_to', ''); } if (isset($variable_tax_class[$i]) && $variable_tax_class[$i] !== 'parent') { update_post_meta($variation_id, '_tax_class', wc_clean($variable_tax_class[$i])); } else { delete_post_meta($variation_id, '_tax_class'); } if ('yes' == $is_downloadable) { update_post_meta($variation_id, '_download_limit', wc_clean($variable_download_limit[$i])); update_post_meta($variation_id, '_download_expiry', wc_clean($variable_download_expiry[$i])); $files = array(); $file_names = isset($_POST['_wc_variation_file_names'][$variation_id]) ? array_map('wc_clean', $_POST['_wc_variation_file_names'][$variation_id]) : array(); $file_urls = isset($_POST['_wc_variation_file_urls'][$variation_id]) ? array_map('wc_clean', $_POST['_wc_variation_file_urls'][$variation_id]) : array(); $file_url_size = sizeof($file_urls); $allowed_file_types = get_allowed_mime_types(); for ($ii = 0; $ii < $file_url_size; $ii++) { if (!empty($file_urls[$ii])) { // Find type and file URL if (0 === strpos($file_urls[$ii], 'http')) { $file_is = 'absolute'; $file_url = esc_url_raw($file_urls[$ii]); } elseif ('[' === substr($file_urls[$ii], 0, 1) && ']' === substr($file_urls[$ii], -1)) { $file_is = 'shortcode'; $file_url = wc_clean($file_urls[$ii]); } else { $file_is = 'relative'; $file_url = wc_clean($file_urls[$ii]); } $file_name = wc_clean($file_names[$ii]); $file_hash = md5($file_url); // Validate the file extension if (in_array($file_is, array('absolute', 'relative'))) { $file_type = wp_check_filetype(strtok($file_url, '?')); $parsed_url = parse_url($file_url, PHP_URL_PATH); $extension = pathinfo($parsed_url, PATHINFO_EXTENSION); if (!empty($extension) && !in_array($file_type['type'], $allowed_file_types)) { WC_Admin_Meta_Boxes::add_error(sprintf(__('The downloadable file %s cannot be used as it does not have an allowed file type. Allowed types include: %s', 'woocommerce'), '<code>' . basename($file_url) . '</code>', '<code>' . implode(', ', array_keys($allowed_file_types)) . '</code>')); continue; } } // Validate the file exists if ('relative' === $file_is && !apply_filters('woocommerce_downloadable_file_exists', file_exists($file_url), $file_url)) { WC_Admin_Meta_Boxes::add_error(sprintf(__('The downloadable file %s cannot be used as it does not exist on the server.', 'woocommerce'), '<code>' . $file_url . '</code>')); continue; } $files[$file_hash] = array('name' => $file_name, 'file' => $file_url); } } // grant permission to any newly added files on any existing orders for this product prior to saving do_action('woocommerce_process_product_file_download_paths', $post_id, $variation_id, $files); update_post_meta($variation_id, '_downloadable_files', $files); } else { update_post_meta($variation_id, '_download_limit', ''); update_post_meta($variation_id, '_download_expiry', ''); update_post_meta($variation_id, '_downloadable_files', ''); } update_post_meta($variation_id, '_variation_description', wp_kses_post($variable_description[$i])); // Save shipping class $variable_shipping_class[$i] = !empty($variable_shipping_class[$i]) ? (int) $variable_shipping_class[$i] : ''; wp_set_object_terms($variation_id, $variable_shipping_class[$i], 'product_shipping_class'); // Update Attributes $updated_attribute_keys = array(); foreach ($attributes as $attribute) { if ($attribute['is_variation']) { $attribute_key = 'attribute_' . sanitize_title($attribute['name']); $updated_attribute_keys[] = $attribute_key; if ($attribute['is_taxonomy']) { // Don't use wc_clean as it destroys sanitized characters $value = isset($_POST[$attribute_key][$i]) ? sanitize_title(stripslashes($_POST[$attribute_key][$i])) : ''; } else { $value = isset($_POST[$attribute_key][$i]) ? wc_clean(stripslashes($_POST[$attribute_key][$i])) : ''; } update_post_meta($variation_id, $attribute_key, $value); } } // Remove old taxonomies attributes so data is kept up to date - first get attribute key names $delete_attribute_keys = $wpdb->get_col($wpdb->prepare("SELECT meta_key FROM {$wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND meta_key NOT IN ( '" . implode("','", $updated_attribute_keys) . "' ) AND post_id = %d;", $variation_id)); foreach ($delete_attribute_keys as $key) { delete_post_meta($variation_id, $key); } do_action('woocommerce_save_product_variation', $variation_id, $i); } } // Update parent if variable so price sorting works and stays in sync with the cheapest child WC_Product_Variable::sync($post_id); // Update default attribute options setting $default_attributes = array(); foreach ($attributes as $attribute) { if ($attribute['is_variation']) { // Don't use wc_clean as it destroys sanitized characters if (isset($_POST['default_attribute_' . sanitize_title($attribute['name'])])) { $value = sanitize_title(trim(stripslashes($_POST['default_attribute_' . sanitize_title($attribute['name'])]))); } else { $value = ''; } if ($value) { $default_attributes[sanitize_title($attribute['name'])] = $value; } } } update_post_meta($post_id, '_default_attributes', $default_attributes); }
/** * @param $language * * @return bool */ private function validate_language($language) { if (array_key_exists($language, $this->settings->languages)) { return $language; } else { WC_Admin_Meta_Boxes::add_error(sprintf(__('Unsupported language %s', 'woocommerce_booxtream'), '<code>' . $language . '</code>')); return false; } }