/** * Remove saved fields and options */ public function remove_plugin_data() { foreach (ML_Product::get_fields() as $field) { delete_post_meta_by_key(ML_Product::ML_PREFIX . $field); } delete_option($this->options_slug); }
/** * Update a product at ML * * @ajax( action: "change_status_ml_product" , only_logged: true ) */ public function change_product_status() { check_ajax_referer('ml-change-status-action', 'security'); if (!isset($_GET['post_id'], $_GET['status'])) { die; } try { $ml_product = new ML_Product(intval($_GET['post_id'])); $ml_product->update_status($_GET['status']); } catch (ML_Exception $e) { ML()->add_notice(sprintf('%s: %s', __('Error while trying to update product status on MercadoLivre', ML()->textdomain), $e->getMessage()), 'error'); } wp_safe_redirect(wp_get_referer() ? wp_get_referer() : admin_url('edit.php?post_type=product')); die; }
/** * 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'); } } } }
<?php /** * Product Tab View * * @author Carlos Cardoso Dias * */ /** * Anti cheating code */ defined('ABSPATH') or die('A Agência Magma não deixa você trapacear ;)'); global $thepostid; $ml_product = new ML_Product(intval($thepostid)); $textdomain = ML()->textdomain; ?> <div id="ml_product_data" class="panel woocommerce_options_panel"> <?php if (ML()->ml_auto_export == 'no') { ?> <div class="options_group"> <?php woocommerce_wp_checkbox(array('id' => 'ml_publish', 'label' => $ml_product->is_published() ? __('Update at ML', $textdomain) : __('Post at ML', $textdomain), 'description' => __('Check if this product should be published at ML', $textdomain), 'desc_tip' => true, 'value' => isset($_POST['ml_publish']) || $ml_product->is_published() ? 'yes' : 'no')); ?> </div> <?php } ?> <div class="options_group ml_required_fields"> <p class="form-field ml_category_id_field">
/** * Get ML json format for variable products */ private function get_variable_body(&$ml_data) { $variation_counter = 0; foreach ($this->product->get_children() as $child_id) { $child_product = new ML_Product($child_id); // Check if this variation is linked with ML if (empty($child_product->id) && empty($child_product->attribute_combinations)) { continue; } $ml_data['variations'][$variation_counter] = array('available_quantity' => $child_product->get_wc_product()->get_stock_quantity(), 'price' => empty($child_product->price) ? $child_product->get_wc_product()->get_price() : $child_product->price, 'seller_custom_field' => strval($child_id)); $this->set_pictures_to_body($child_product->get_wc_product(), $ml_data, $variation_counter); $this->set_pictures_to_body($child_product->get_wc_product(), $ml_data); if ($child_product->is_published()) { $ml_data['variations'][$variation_counter]['id'] = intval($child_product->id); } else { $ml_data['variations'][$variation_counter]['attribute_combinations'] = $child_product->attribute_combinations; } $variation_counter++; } return $ml_data; }
<?php /** * Product Column View * * @author Carlos Cardoso Dias * */ /** * Anti cheating code */ defined('ABSPATH') or die('A Agência Magma não deixa você trapacear ;)'); $ml_product = new ML_Product($post_id); if ($ml_product->is_published()) { ?> <a href="<?php echo add_query_arg(array('action' => 'change_status_ml_product', 'post_id' => $post_id, 'status' => 'closed', 'security' => wp_create_nonce('ml-change-status-action')), admin_url('admin-ajax.php')); ?> " class="button tips ml_delete ml_button" data-tip="<?php _e('Delete', ML()->textdomain); ?> "></a> <?php } if (isset($ml_product->status)) { ?> <?php if ($ml_product->status == 'closed') { ?> <a href="<?php echo add_query_arg(array('action' => 'relist_ml_product', 'post_id' => $post_id, 'security' => wp_create_nonce('ml-relist-product-action')), admin_url('admin-ajax.php'));