• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WooCommerce a2z

WooCommerce a2z

WooCommerce

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / WC_Meta_Box_Product_Data::save_variations() – Save variation meta box data.

You appear to be a bot. Output may be restricted

Description

Save variation meta box data.

Usage

WC_Meta_Box_Product_Data::save_variations( $post_id, $post );

Parameters

$post_id
( int ) required – WP post id.
$post
( WP_Post ) required – Post object.

Returns

void

Source

File name: woocommerce/includes/admin/meta-boxes/class-wc-meta-box-product-data.php
Lines:

1 to 100 of 107
  public static function save_variations( $post_id, $post ) {
    // phpcs:disable WordPress.Security.NonceVerification.Missing
    if ( isset( $_POST['variable_post_id'] ) ) {
      $parent = wc_get_product( $post_id );
      $parent->set_default_attributes( self::prepare_set_attributes( $parent->get_attributes(), 'default_attribute_' ) );
      $parent->save();

      $max_loop   = max( array_keys( wp_unslash( $_POST['variable_post_id'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
      $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    = wc_get_product_object( 'variation', $variation_id );
        $stock        = null;

        // Handle stock changes.
        if ( isset( $_POST['variable_stock'], $_POST['variable_stock'][ $i ] ) ) {
          if ( isset( $_POST['variable_original_stock'], $_POST['variable_original_stock'][ $i ] ) && wc_stock_amount( $variation->get_stock_quantity( 'edit' ) ) !== wc_stock_amount( wp_unslash( $_POST['variable_original_stock'][ $i ] ) ) ) {
            /* translators: 1: product ID 2: quantity in stock */
            WC_Admin_Meta_Boxes::add_error( sprintf( __( 'The stock has not been updated because the value has changed since editing. Product %1$d has %2$d units in stock.', 'woocommerce' ), $variation->get_id(), $variation->get_stock_quantity( 'edit' ) ) );
          } else {
            $stock = wc_stock_amount( wp_unslash( $_POST['variable_stock'][ $i ] ) );
          }
        }

        // Handle dates.
        $date_on_sale_from = '';
        $date_on_sale_to   = '';

        // Force date from to beginning of day.
        if ( isset( $_POST['variable_sale_price_dates_from'][ $i ] ) ) {
          $date_on_sale_from = wc_clean( wp_unslash( $_POST['variable_sale_price_dates_from'][ $i ] ) );

          if ( ! empty( $date_on_sale_from ) ) {
            $date_on_sale_from = date( 'Y-m-d 00:00:00', strtotime( $date_on_sale_from ) );
          }
        }

        // Force date to to the end of the day.
        if ( isset( $_POST['variable_sale_price_dates_to'][ $i ] ) ) {
          $date_on_sale_to = wc_clean( wp_unslash( $_POST['variable_sale_price_dates_to'][ $i ] ) );

          if ( ! empty( $date_on_sale_to ) ) {
            $date_on_sale_to = date( 'Y-m-d 23:59:59', strtotime( $date_on_sale_to ) );
          }
        }

        $errors = $variation->set_props(
          array(
            'status'            => isset( $_POST['variable_enabled'][ $i ] ) ? 'publish' : 'private',
            'menu_order'        => isset( $_POST['variation_menu_order'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variation_menu_order'][ $i ] ) ) : null,
            'regular_price'     => isset( $_POST['variable_regular_price'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_regular_price'][ $i ] ) ) : null,
            'sale_price'        => isset( $_POST['variable_sale_price'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_sale_price'][ $i ] ) ) : null,
            'virtual'           => isset( $_POST['variable_is_virtual'][ $i ] ),
            'downloadable'      => isset( $_POST['variable_is_downloadable'][ $i ] ),
            'date_on_sale_from' => $date_on_sale_from,
            'date_on_sale_to'   => $date_on_sale_to,
            'description'       => isset( $_POST['variable_description'][ $i ] ) ? wp_kses_post( wp_unslash( $_POST['variable_description'][ $i ] ) ) : null,
            'download_limit'    => isset( $_POST['variable_download_limit'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_download_limit'][ $i ] ) ) : null,
            'download_expiry'   => isset( $_POST['variable_download_expiry'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_download_expiry'][ $i ] ) ) : null,
            // Those are sanitized inside prepare_downloads.
            'downloads'         => self::prepare_downloads(
              isset( $_POST['_wc_variation_file_names'][ $variation_id ] ) ? wp_unslash( $_POST['_wc_variation_file_names'][ $variation_id ] ) : array(), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
              isset( $_POST['_wc_variation_file_urls'][ $variation_id ] ) ? wp_unslash( $_POST['_wc_variation_file_urls'][ $variation_id ] ) : array(), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
              isset( $_POST['_wc_variation_file_hashes'][ $variation_id ] ) ? wp_unslash( $_POST['_wc_variation_file_hashes'][ $variation_id ] ) : array() // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
            ),
            'manage_stock'      => isset( $_POST['variable_manage_stock'][ $i ] ),
            'stock_quantity'    => $stock,
            'backorders'        => isset( $_POST['variable_backorders'], $_POST['variable_backorders'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_backorders'][ $i ] ) ) : null,
            'stock_status'      => isset( $_POST['variable_stock_status'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_stock_status'][ $i ] ) ) : null,
            'image_id'          => isset( $_POST['upload_image_id'][ $i ] ) ? wc_clean( wp_unslash( $_POST['upload_image_id'][ $i ] ) ) : null,
            'attributes'        => self::prepare_set_attributes( $parent->get_attributes(), 'attribute_', $i ),
            'sku'               => isset( $_POST['variable_sku'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_sku'][ $i ] ) ) : '',
            'weight'            => isset( $_POST['variable_weight'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_weight'][ $i ] ) ) : '',
            'length'            => isset( $_POST['variable_length'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_length'][ $i ] ) ) : '',
            'width'             => isset( $_POST['variable_width'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_width'][ $i ] ) ) : '',
            'height'            => isset( $_POST['variable_height'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_height'][ $i ] ) ) : '',
            'shipping_class_id' => isset( $_POST['variable_shipping_class'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_shipping_class'][ $i ] ) ) : null,
            'tax_class'         => isset( $_POST['variable_tax_class'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_tax_class'][ $i ] ) ) : null,
          )
        );

        if ( is_wp_error( $errors ) ) {
          WC_Admin_Meta_Boxes::add_error( $errors->get_error_message() );
        }

        
/**
 * Set variation props before save.
 *
 * @param object $variation WC_Product_Variation object.
 * @param int $i
 * @since 3.8.0
 */
        do_action( 'woocommerce_admin_process_variation_object', $variation, $i );

 
[1] [2] Next »

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 6th April 2020

Primary Sidebar

Information

Function name: WC_Meta_Box_Product_Data::save_variations
Class ref: WC_Meta_Box_Product_Data
Plugin ref: WooCommerce
Version: 4.8.0
Sourcefile: includes/admin/meta-boxes/class-wc-meta-box-product-data.php
File ref: includes/admin/meta-boxes/class-wc-meta-box-product-data.php
Deprecated?: No
API Letters: B,D,M,P,W

Footer

WooCommerce a2z
WooCommerce a2z
WooCommerce
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  woocommerce.wp-a2z.org
© Copyright WooCommerce a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites