WC_Meta_Box_Product_Data::save_variations PHP Method

save_variations() public static method

Save meta box data.
public static save_variations ( integer $post_id, WP_Post $post )
$post_id integer
$post WP_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']));
            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);
            }
        }
    }

Usage Example

コード例 #1
0
ファイル: class-wc-ajax.php プロジェクト: tronsha/woocommerce
 /**
  * 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;
 }
All Usage Examples Of WC_Meta_Box_Product_Data::save_variations