/**
  * Save ML product data
  *
  * @action( hook: "woocommerce_process_product_meta_variable" )
  * @action( hook: "woocommerce_process_product_meta_simple" )
  */
 public function save_product_metaboxes_data($post_id)
 {
     $ml_product = new ML_Product($post_id);
     $ml_product->video_id = $_POST['ml_video_id'];
     if (!$ml_product->is_published()) {
         // Fields for a new product
         $ml_product->category_id = $_POST['ml_category_id'];
         $ml_product->listing_type_id = $_POST['ml_listing_type_id'];
         if (!empty($_POST['ml_official_store_id'])) {
             $ml_product->official_store_id = $_POST['ml_official_store_id'];
         }
     }
     if ($ml_product->can_update_special_fields()) {
         // Fields that can only be updated when the product has no sales
         $ml_product->title = sanitize_text_field($_POST['ml_title']);
         //$ml_product->buying_mode  = $_POST['ml_buying_mode'];
         //$ml_product->condition    = $_POST['ml_condition'];
         $ml_product->warranty = $_POST['ml_warranty'];
         if (isset($_POST['ml_shipping_mode']) && $_POST['ml_shipping_mode'] == 'custom') {
             // Custom Shipping
             $costs = array();
             for ($i = 0; $i < 10; $i++) {
                 if (empty($_POST['ml_shipment_data'][$i]['description']) || empty($_POST['ml_shipment_data'][$i]['cost'])) {
                     continue;
                 }
                 $costs[] = array('description' => sanitize_text_field($_POST['ml_shipment_data'][$i]['description']), 'cost' => floatval($_POST['ml_shipment_data'][$i]['cost']));
             }
             $ml_product->shipping_mode = 'custom';
             $ml_product->shipment_costs = $costs;
             $ml_product->shipment_local_pickup = $_POST['ml_shipment_local_pickup'] == 'yes';
             if (isset($_POST['ml_shipment_free_shipping'])) {
                 $ml_product->shipment_free_shipping = $_POST['ml_shipment_free_shipping'] == 'yes';
             } else {
                 $ml_product->shipment_free_shipping = false;
             }
         }
     }
     if (!$ml_product->is_published() || !$ml_product->is_variable()) {
         $ml_product->price = sanitize_text_field($_POST['ml_price']);
     }
     if ($ml_product->is_variable()) {
         // Set variations
         $number_of_variations = count($_POST['ml_variations']['child']);
         $attributes = array_diff(array_keys($_POST['ml_variations']), array('child', 'price'));
         for ($position = 0; $position < $number_of_variations; $position++) {
             $child_product = new ML_Product(intval($_POST['ml_variations']['child'][$position]));
             if (!$child_product->is_published()) {
                 $attribute_combinations = array();
                 foreach ($attributes as $attribute) {
                     if (!empty($_POST['ml_variations'][$attribute][$position])) {
                         $attribute_combinations[] = array('id' => strval($attribute), 'value_id' => strval($_POST['ml_variations'][$attribute][$position]));
                     }
                 }
                 $child_product->attribute_combinations = $attribute_combinations;
             }
             $child_product->price = sanitize_text_field($_POST['ml_variations']['price'][$position]);
         }
     }
     if ($ml_product->is_published() && (ML()->ml_auto_update == 'yes' || isset($_POST['ml_publish']) && $_POST['ml_publish'] == 'yes')) {
         // Verify aditional changes and update
         try {
             // Update the product
             $new_ml_product = $ml_product->update();
             if ($_POST['ml_listing_type_id'] != $new_ml_product->listing_type_id) {
                 $ml_product->update_listing_type($_POST['ml_listing_type_id']);
             }
             if (!empty($ml_product->get_wc_product()->get_post_data()->post_content)) {
                 $ml_product->update_description();
             }
             // Enqueue a message for the user
             ML()->add_notice(sprintf('%s: <a href="%s" target="_blank">%s</a>', __('The product has been updated successfully on MercadoLivre', ML()->textdomain), $new_ml_product->permalink, $new_ml_product->permalink), 'success');
         } catch (ML_Exception $e) {
             ML()->add_notice(sprintf('%s: %s', __('The product could not be updated on MercadoLivre', ML()->textdomain), $e->getMessage()), 'error');
         }
     } else {
         if (!$ml_product->is_published() && (ML()->ml_auto_export == 'yes' || isset($_POST['ml_publish']) && $_POST['ml_publish'] == 'yes')) {
             // Post
             try {
                 // Post the product
                 $new_ml_product = $ml_product->post();
                 // Enqueue a message for the user
                 ML()->add_notice(sprintf('%s: <a href="%s" target="_blank">%s</a>', __('The product was successfully published on MercadoLivre', ML()->textdomain), $new_ml_product->permalink, $new_ml_product->permalink), 'success');
             } catch (ML_Exception $e) {
                 ML()->add_notice(sprintf('%s: %s', __('The product could not be posted on MercadoLivre', ML()->textdomain), $e->getMessage()), 'error');
             }
         }
     }
 }
		jQuery(document).ready(function($) {
			$('#ml_shipping_mode').change(function() {
				// Remove previous shipping mode content
				$('#ml_shipment_content').remove();
				// append image
				$('.shipment_fields').append('<img src="' + "<?php 
echo WooCommerce_MercadoLivre::get_plugin_url('/assets/img/ajax-loader.gif');
?>
" + '" class="loading_shipment">');
				$.getJSON( "<?php 
echo add_query_arg(array('action' => 'get_ml_shipment', 'security' => wp_create_nonce('ml-shipment-action'), 'product_id' => $thepostid), admin_url('admin-ajax.php'));
?>
" + '&shipping_mode=' + $(this).val() + '&category_id=' + $('#ml_category_id').val() , function( data ) {
					// remove image and insert content
					$('.loading_shipment').remove();
					$('.shipment_fields').append(data);
				});
			});
		});
	</script>
	<div class="options_group">
		<?php 
//depending on sold_quantity == 0 to modify
woocommerce_wp_text_input(array('id' => 'ml_title', 'label' => __('Title', $textdomain), 'description' => __('Product title on MercadoLivre', $textdomain), 'desc_tip' => true, 'value' => isset($_POST['ml_title']) ? $_POST['ml_title'] : $ml_product->title, 'custom_attributes' => $ml_product->can_update_special_fields() ? array() : array('disabled' => 'disabled')));
// Modifiable
if (!$ml_product->is_variable()) {
    woocommerce_wp_text_input(array('id' => 'ml_price', 'label' => __('Price', $textdomain), 'wrapper_class' => 'show_if_simple', 'description' => __('Leave blank to use the same value of the site', $textdomain), 'desc_tip' => true, 'data_type' => 'price', 'value' => isset($_POST['ml_price']) ? $_POST['ml_price'] : $ml_product->price));
}
?>
	</div>
</div>