ID; if ( $terms = wp_get_object_terms( $post->ID, 'product_type' ) ) { $product_type = sanitize_title( current( $terms )->name ); } else { $product_type = apply_filters( 'default_product_type', 'simple' ); } $type_box = ''; $product_type_options = apply_filters( 'product_type_options', array( 'virtual' => array( 'id' => '_virtual', 'wrapper_class' => 'show_if_simple', 'label' => __( 'Virtual', 'woocommerce' ), 'description' => __( 'Virtual products are intangible and aren\'t shipped.', 'woocommerce' ), 'default' => 'no', ), 'downloadable' => array( 'id' => '_downloadable', 'wrapper_class' => 'show_if_simple', 'label' => __( 'Downloadable', 'woocommerce' ), 'description' => __( 'Downloadable products give access to a file upon purchase.', 'woocommerce' ), 'default' => 'no', ), ) ); foreach ( $product_type_options as $key => $option ) { $selected_value = get_post_meta( $post->ID, '_' . $key, true ); if ( '' == $selected_value && isset( $option['default'] ) ) { $selected_value = $option['default']; } $type_box .= ''; } ?>
'; // External URL woocommerce_wp_text_input( array( 'id' => '_product_url', 'label' => __( 'Product URL', 'woocommerce' ), 'placeholder' => 'http://', 'description' => __( 'Enter the external URL to the product.', 'woocommerce' ) ) ); // Button text woocommerce_wp_text_input( array( 'id' => '_button_text', 'label' => __( 'Button text', 'woocommerce' ), 'placeholder' => esc_attr__( 'Buy product', 'woocommerce' ), 'description' => __( 'This text will be shown on the button linking to the external product.', 'woocommerce' ) ) ); echo '
'; echo ''; echo ''; if ( wc_tax_enabled() ) { echo '
'; // Tax woocommerce_wp_select( array( 'id' => '_tax_status', 'label' => __( 'Tax status', 'woocommerce' ), 'options' => array( 'taxable' => __( 'Taxable', 'woocommerce' ), 'shipping' => __( 'Shipping only', 'woocommerce' ), 'none' => _x( 'None', 'Tax status', 'woocommerce' ), ), 'desc_tip' => 'true', 'description' => __( 'Define whether or not the entire product is taxable, or just the cost of shipping it.', 'woocommerce' ), ) ); $tax_classes = WC_Tax::get_tax_classes(); $classes_options = array(); $classes_options[''] = __( 'Standard', 'woocommerce' ); if ( ! empty( $tax_classes ) ) { foreach ( $tax_classes as $class ) { $classes_options[ sanitize_title( $class ) ] = esc_html( $class ); } } woocommerce_wp_select( array( 'id' => '_tax_class', 'label' => __( 'Tax class', 'woocommerce' ), 'options' => $classes_options, 'desc_tip' => 'true', 'description' => __( 'Choose a tax class for this product. Tax classes are used to apply different tax rates specific to certain types of product.', 'woocommerce' ), ) ); do_action( 'woocommerce_product_options_tax' ); echo '
'; } do_action( 'woocommerce_product_options_general_product_data' ); ?>
'; echo '
'; // Individual product woocommerce_wp_checkbox( array( 'id' => '_sold_individually', 'wrapper_class' => 'show_if_simple show_if_variable', 'label' => __( 'Sold individually', 'woocommerce' ), 'description' => __( 'Enable this to only allow one of this item to be bought in a single order', 'woocommerce' ) ) ); do_action( 'woocommerce_product_options_sold_individually' ); echo '
'; do_action( 'woocommerce_product_options_inventory_product_data' ); ?> ID, '_product_attributes', true ) ); // See if any are set $variation_attribute_found = false; if ( $attributes ) { foreach ( $attributes as $attribute ) { if ( ! empty( $attribute['is_variation'] ) ) { $variation_attribute_found = true; break; } } } $variations_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'product_variation' AND post_status IN ('publish', 'private')", $post->ID ) ) ); $variations_per_page = absint( apply_filters( 'woocommerce_admin_meta_boxes_variations_per_page', 15 ) ); $variations_total_pages = ceil( $variations_count / $variations_per_page ); ?> 0 && 'external' !== $product_type ) ? 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 = (string) wc_clean( $_POST['_sku'] ); if ( '' == $new_sku ) { update_post_meta( $post_id, '_sku', '' ); } elseif ( $new_sku !== $sku ) { if ( ! empty( $new_sku ) ) { $unique_sku = wc_product_has_unique_sku( $post_id, $new_sku ); if ( ! $unique_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_max_key = max( array_keys( $attribute_names ) ); for ( $i = 0; $i <= $attribute_names_max_key; $i++ ) { if ( empty( $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 ) { $values_are_slugs = false; 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 ] ); $values_are_slugs = true; // 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 ] ) ) { foreach ( $values as $key => $value ) { $term = get_term_by( $values_are_slugs ? 'slug' : 'name', trim( $value ), $attribute_names[ $i ] ); if ( $term ) { $values[ $key ] = intval( $term->term_id ); } else { $term = wp_insert_term( trim( $value ), $attribute_names[ $i ] ); if ( isset( $term->term_id ) ) { $values[ $key ] = intval( $term->term_id ); } } } wp_set_object_terms( $post_id, $values, $attribute_names[ $i ] ); } if ( ! empty( $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, possibly separated by pipes (WC_DELIMITER). Preserve line breaks in non-variation attributes. $values = $is_variation ? wc_clean( $attribute_values[ $i ] ) : implode( "\n", array_map( 'wc_clean', explode( "\n", $attribute_values[ $i ] ) ) ); $values = implode( ' ' . WC_DELIMITER . ' ', wc_get_text_attributes( $values ) ); // 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, ); } } } uasort( $attributes, 'wc_product_attribute_uasort_comparison' ); /** * Unset removed attributes by looping over previous values and * unsetting the terms. */ $old_attributes = array_filter( (array) maybe_unserialize( get_post_meta( $post_id, '_product_attributes', true ) ) ); if ( ! empty( $old_attributes ) ) { foreach ( $old_attributes as $key => $value ) { if ( empty( $attributes[ $key ] ) && ! empty( $value['is_taxonomy'] ) && taxonomy_exists( $key ) ) { wp_set_object_terms( $post_id, array(), $key ); } } } /** * After removed attributes are unset, we can set the new attribute data. */ 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', '' ); } else { $date_from = (string) isset( $_POST['_sale_price_dates_from'] ) ? wc_clean( $_POST['_sale_price_dates_from'] ) : ''; $date_to = (string) isset( $_POST['_sale_price_dates_to'] ) ? wc_clean( $_POST['_sale_price_dates_to'] ) : ''; $regular_price = (string) isset( $_POST['_regular_price'] ) ? wc_clean( $_POST['_regular_price'] ) : ''; $sale_price = (string) isset( $_POST['_sale_price'] ) ? wc_clean( $_POST['_sale_price'] ) : ''; update_post_meta( $post_id, '_regular_price', '' === $regular_price ? '' : wc_format_decimal( $regular_price ) ); update_post_meta( $post_id, '_sale_price', '' === $sale_price ? '' : wc_format_decimal( $sale_price ) ); // Dates update_post_meta( $post_id, '_sale_price_dates_from', $date_from ? strtotime( $date_from ) : '' ); update_post_meta( $post_id, '_sale_price_dates_to', $date_to ? strtotime( $date_to ) : '' ); if ( $date_to && ! $date_from ) { $date_from = date( 'Y-m-d' ); update_post_meta( $post_id, '_sale_price_dates_from', strtotime( $date_from ) ); } // Update price if on sale if ( '' !== $sale_price && '' === $date_to && '' === $date_from ) { update_post_meta( $post_id, '_price', wc_format_decimal( $sale_price ) ); } elseif ( '' !== $sale_price && $date_from && strtotime( $date_from ) <= strtotime( 'NOW', current_time( 'timestamp' ) ) ) { update_post_meta( $post_id, '_price', wc_format_decimal( $sale_price ) ); } else { update_post_meta( $post_id, '_price', '' === $regular_price ? '' : wc_format_decimal( $regular_price ) ); } if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) { update_post_meta( $post_id, '_price', '' === $regular_price ? '' : wc_format_decimal( $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', '' ); } } // 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 $manage_stock = ! empty( $_POST['_manage_stock'] ) && 'grouped' !== $product_type ? 'yes' : 'no'; $backorders = ! empty( $_POST['_backorders'] ) && 'yes' === $manage_stock ? wc_clean( $_POST['_backorders'] ) : 'no'; $stock_status = ! empty( $_POST['_stock_status'] ) ? wc_clean( $_POST['_stock_status'] ) : 'instock'; $stock_amount = 'yes' === $manage_stock ? wc_stock_amount( $_POST['_stock'] ) : ''; if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) { // Apply product type constraints to stock status if ( 'external' === $product_type ) { // External always in stock $stock_status = 'instock'; } elseif ( 'variable' === $product_type ) { // Stock status is always determined by children so sync later $stock_status = ''; } update_post_meta( $post_id, '_manage_stock', $manage_stock ); update_post_meta( $post_id, '_backorders', $backorders ); if ( $stock_status ) { wc_update_product_stock_status( $post_id, $stock_status ); } wc_update_product_stock( $post_id, $stock_amount ); } else { wc_update_product_stock_status( $post_id, $stock_status ); } // Cross sells, upsells, and grouped products. $upsells = isset( $_POST['upsell_ids'] ) ? array_filter( array_map( 'intval', explode( ',', $_POST['upsell_ids'] ) ) ) : array(); $crosssells = isset( $_POST['crosssell_ids'] ) ? array_filter( array_map( 'intval', explode( ',', $_POST['crosssell_ids'] ) ) ) : array(); $grouped_products = isset( $_POST['grouped_products'] ) ? array_filter( array_map( 'intval', explode( ',', $_POST['grouped_products'] ) ) ) : array(); update_post_meta( $post_id, '_upsell_ids', $upsells ); update_post_meta( $post_id, '_crosssell_ids', $crosssells ); update_post_meta( $post_id, '_children', $grouped_products ); // Downloadable options if ( 'yes' === $is_downloadable ) { $_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'] ) ? $_POST['_wc_file_names'] : array(); $file_urls = isset( $_POST['_wc_file_urls'] ) ? wp_unslash( array_map( 'trim', $_POST['_wc_file_urls'] ) ) : array(); $file_url_size = sizeof( $file_urls ); $allowed_file_types = apply_filters( 'woocommerce_downloadable_file_allowed_mime_types', get_allowed_mime_types() ); for ( $i = 0; $i < $file_url_size; $i ++ ) { if ( ! empty( $file_urls[ $i ] ) ) { // Find type and file URL if ( 0 === strpos( $file_urls[ $i ], 'http' ) ) { $file_is = 'absolute'; $file_url = esc_url_raw( $file_urls[ $i ] ); } elseif ( '[' === substr( $file_urls[ $i ], 0, 1 ) && ']' === substr( $file_urls[ $i ], -1 ) ) { $file_is = 'shortcode'; $file_url = wc_clean( $file_urls[ $i ] ); } else { $file_is = 'relative'; $file_url = wc_clean( $file_urls[ $i ] ); } $file_name = wc_clean( $file_names[ $i ] ); $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, '?' ), $allowed_file_types ); $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 ) ) { /* translators: 1: file url 2: allowed file types */ WC_Admin_Meta_Boxes::add_error( sprintf( __( 'The downloadable file %1$s cannot be used as it does not have an allowed file type. Allowed types include: %2$s', 'woocommerce' ), '' . basename( $file_url ) . '', '' . implode( ', ', array_keys( $allowed_file_types ) ) . '' ) ); continue; } } // Validate the file exists if ( 'relative' === $file_is ) { $_file_url = $file_url; if ( '..' === substr( $file_url, 0, 2 ) || '/' !== substr( $file_url, 0, 1 ) ) { $_file_url = realpath( ABSPATH . $file_url ); } if ( ! 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' ), '' . $file_url . '' ) ); 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, 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 ( 'external' === $product_type ) { if ( isset( $_POST['_product_url'] ) ) { update_post_meta( $post_id, '_product_url', esc_url_raw( $_POST['_product_url'] ) ); } if ( isset( $_POST['_button_text'] ) ) { update_post_meta( $post_id, '_button_text', wc_clean( $_POST['_button_text'] ) ); } } // Sync related products. if ( 'variable' === $product_type ) { WC_Product_Variable::sync( $post_id ); WC_Product_Variable::sync_stock_status( $post_id ); } // Update version after saving update_post_meta( $post_id, '_product_version', WC_VERSION ); // Do action for product type do_action( 'woocommerce_process_product_meta_' . $product_type, $post_id ); // Clear cache/transients wc_delete_product_transients( $post_id ); } /** * Save meta box data. * * @param int $post_id * @param WP_Post $post */ 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 /* translators: 1: variation id 2: product name */ $variation_post_title = sprintf( __( 'Variation #%1$s of %2$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 { $modified_date = date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp' ) ); $wpdb->update( $wpdb->posts, array( 'post_status' => $post_status, 'post_title' => $variation_post_title, 'menu_order' => $variable_menu_order[ $i ], 'post_modified' => $modified_date, 'post_modified_gmt' => get_gmt_from_date( $modified_date ), ), array( 'ID' => $variation_id ) ); clean_post_cache( $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( $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 ) { /* translators: %s: variation id */ WC_Admin_Meta_Boxes::add_error( sprintf( __( '#%s – Variation SKU must be unique.', 'woocommerce' ), $variation_id ) ); } 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 ); 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' ); wc_update_product_stock( $variation_id, '' ); } // 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 ] ); } // Price handling _wc_save_product_price( $variation_id, $variable_regular_price[ $i ], $variable_sale_price[ $i ], $variable_sale_price_dates_from[ $i ], $variable_sale_price_dates_to[ $i ] ); if ( isset( $variable_tax_class[ $i ] ) && 'parent' !== $variable_tax_class[ $i ] ) { 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, '?' ), $allowed_file_types ); $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 ) ) { /* translators: 1: variation id 2: file url 3: allowed file types */ WC_Admin_Meta_Boxes::add_error( sprintf( __( '#%1$s – The downloadable file %2$s cannot be used as it does not have an allowed file type. Allowed types include: %3$s', 'woocommerce' ), $variation_id, '' . basename( $file_url ) . '', '' . implode( ', ', array_keys( $allowed_file_types ) ) . '' ) ); continue; } } // Validate the file exists if ( 'relative' === $file_is && ! apply_filters( 'woocommerce_downloadable_file_exists', file_exists( $file_url ), $file_url ) ) { /* translators: 1: variation id 2: file url */ WC_Admin_Meta_Boxes::add_error( sprintf( __( '#%1$s – The downloadable file %2$s cannot be used as it does not exist on the server.', 'woocommerce' ), $variation_id, '' . $file_url . '' ) ); 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'] ) { $value = ''; if ( isset( $_POST[ 'default_attribute_' . sanitize_title( $attribute['name'] ) ] ) ) { if ( $attribute['is_taxonomy'] ) { // Don't use wc_clean as it destroys sanitized characters $value = sanitize_title( trim( stripslashes( $_POST[ 'default_attribute_' . sanitize_title( $attribute['name'] ) ] ) ) ); } else { $value = wc_clean( trim( stripslashes( $_POST[ 'default_attribute_' . sanitize_title( $attribute['name'] ) ] ) ) ); } } if ( $value ) { $default_attributes[ sanitize_title( $attribute['name'] ) ] = $value; } } } update_post_meta( $post_id, '_default_attributes', $default_attributes ); } }