WC_Meta_Box_Product_Data::save_variations() public WC 1.0
Save variation meta box data.
{} Это метод класса: WC_Meta_Box_Product_Data{}
Возвращает
Null. Ничего.
Использование
$result = WC_Meta_Box_Product_Data::save_variations( $post_id, $post );
- $post_id(число) (обязательный)
- WP post id.
- $post(WP_Post) (обязательный)
- Post object.
Код WC_Meta_Box_Product_Data::save_variations() WC Meta Box Product Data::save variations WC 5.0.0
<?php
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 ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
}
}
// 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 ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
}
}
$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 );
$variation->save();
do_action( 'woocommerce_save_product_variation', $variation_id, $i );
}
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
}