/** * 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'); } } } }
</div> <div class="options_group"> <?php if (!empty(ML()->ml_official_stores)) { woocommerce_wp_select(array('id' => 'ml_official_store_id', 'label' => __('Official Store', $textdomain), 'description' => __('Determines which official store the product will be posted', $textdomain), 'desc_tip' => true, 'options' => ML()->ml_official_stores, 'value' => isset($_POST['ml_official_store_id']) ? $_POST['ml_official_store_id'] : $ml_product->official_store_id, 'custom_attributes' => $ml_product->is_published() ? array('disabled' => 'disabled') : array())); } //Modifiable with conditions woocommerce_wp_select(array('id' => 'ml_listing_type_id', 'label' => __('Listing Type', $textdomain), 'description' => __('Determines how your product will be listed on MercadoLivre', $textdomain), 'desc_tip' => true, 'options' => wp_list_pluck(ML()->ml_communication->get_listing_types(), 'name', 'id'), 'value' => isset($_POST['ml_listing_type_id']) ? $_POST['ml_listing_type_id'] : $ml_product->listing_type_id)); ?> </div> <div class="options_group"> <?php // Modifiable woocommerce_wp_text_input(array('id' => 'ml_video_id', 'label' => __('Video ID or URL', $textdomain), 'description' => __('Use the URL or the ID of the video on youtube', $textdomain), 'desc_tip' => true, 'value' => isset($_POST['ml_video_id']) ? $_POST['ml_video_id'] : $ml_product->video_id)); //depending on sold_quantity == 0 to modify woocommerce_wp_textarea_input(array('id' => 'ml_warranty', 'label' => __('Warranty', $textdomain), 'description' => __('Describe the product warranty for buyers on MercadoLivre', $textdomain), 'desc_tip' => true, 'value' => isset($_POST['ml_warranty']) ? $_POST['ml_warranty'] : $ml_product->warranty, 'custom_attributes' => $ml_product->can_update_special_fields() ? array() : array('disabled' => 'disabled'))); ?> </div> <div class="options_group shipment_fields"> <?php woocommerce_wp_select(array('id' => 'ml_shipping_mode', 'label' => __('Shipping', $textdomain), 'description' => __('Determine how your product will be delivered for buyers on MercadoLivre', $textdomain), 'desc_tip' => true, 'options' => ML()->ml_communication->get_shipping_modes(empty($ml_product->category_id) ? null : $ml_product->category_id), 'value' => isset($_POST['ml_shipping_mode']) ? $_POST['ml_shipping_mode'] : $ml_product->shipping_mode, 'custom_attributes' => $ml_product->can_update_special_fields() ? array() : array('disabled' => 'disabled'))); if (!empty($ml_product->shipping_mode) && $ml_product->shipping_mode != 'not_specified') { $category_id = $ml_product->category_id; include_once "html-{$ml_product->shipping_mode}-shipment.php"; } ?> </div> <script type="text/javascript"> jQuery(document).ready(function($) { $('#ml_shipping_mode').change(function() { // Remove previous shipping mode content