diff --git a/assets/js/integration-siigo.js b/assets/js/integration-siigo.js index c7592db..b7d83d1 100644 --- a/assets/js/integration-siigo.js +++ b/assets/js/integration-siigo.js @@ -1,23 +1,35 @@ (function($){ - const buttons = 'button.siigo-sync, button.siigo-sync-webhook'; + const buttons = 'button.siigo-sync, button.siigo-sync-woo-siigo, button.siigo-sync-webhook'; const actionSyncProducts = 'integration_siigo_sync_products'; + const actionSyncProductsWooSiigo = 'integration_siigo_sync_woo_siigo'; const actionSyncWebhook = 'integration_siigo_sync_webhook'; const messages = { [actionSyncProducts]: { - title: 'Sincronizando productos', - successTitle: 'Productos sincronizados' + title: 'Agendando sincronización', + successText: 'La sincronización de productos desde Siigo se ha agendado correctamente' + }, + [actionSyncProductsWooSiigo]: { + title: 'Agendando sincronización', + successText: 'La sincronización de productos hacia Siigo se ha agendado correctamente' }, [actionSyncWebhook]: { title: 'Habilitando webhook', - successTitle: 'Webhook habilitado' + successText: 'Webhook habilitado' } } $(buttons).click(function (e) { const self = $(this); - const action = self.hasClass('siigo-sync') ? actionSyncProducts : actionSyncWebhook; + + let action = actionSyncWebhook + + if(self.hasClass('siigo-sync')){ + action = actionSyncProducts; + } else if (self.hasClass('siigo-sync-woo-siigo')) { + action = actionSyncProductsWooSiigo; + } e.preventDefault(); @@ -42,7 +54,7 @@ if (r.status){ Swal.fire({ icon: 'success', - title: messages[action].successTitle, + text: messages[action].successText, allowOutsideClick: false, showCloseButton: true, showConfirmButton: false diff --git a/includes/admin/settings.php b/includes/admin/settings.php index 92e6a7d..8495a3c 100644 --- a/includes/admin/settings.php +++ b/includes/admin/settings.php @@ -6,7 +6,7 @@ siigoIntegrationFields: '#woocommerce_wc_siigo_integration_username, #woocommerce_wc_siigo_integration_access_key, #woocommerce_wc_siigo_integration_webhook_button', siigoIntegrationSandboxFields: '#woocommerce_wc_siigo_integration_sandbox_username, #woocommerce_wc_siigo_integration_sandbox_access_key', webhookButton: '#woocommerce_wc_siigo_integration_webhook_button', - syncButton: '#woocommerce_wc_siigo_integration_sync_button', + syncButton: '#woocommerce_wc_siigo_integration_sync_siigo_woo', webhookButtonHeader: 'h3#woocommerce_wc_siigo_integration_webhook_button', productsTitle: 'h3#woocommerce_wc_siigo_integration_products', environmentSelector: '#woocommerce_wc_siigo_integration_environment', @@ -201,12 +201,23 @@ function toggleFields() { 'title' => __( 'Productos' ), 'type' => 'title' ), - 'sync_button' => array( - 'title' => 'Sincronizar productos manualmente', + 'sync_siigo_woo' => array( + 'title' => 'Sincronizar productos Siigo -> WooCommerce', 'type' => 'button', - 'description' => "Sincroniza los productos de Siigo a WooCommerce.
Nota: Este proceso puede tardar varios minutos.", + 'class' => 'button-secondary siigo-sync', + 'description' => "Sincroniza los productos de Siigo a WooCommerce", 'text' => 'Sincronizar ahora', ), + 'sync_woo_siigo' => array( + 'title' => 'Sincronizar productos WooCommerce -> Siigo', + 'type' => 'button', + 'class' => 'button-secondary siigo-sync-woo-siigo', + 'description' => "Sincroniza los productos de WooCommerce a Siigo", + 'text' => 'Sincronizar ahora', + 'custom_attributes' => [ + 'data-nonce' => wp_create_nonce( 'integration_siigo_sync_woo_siigo' ), + ] + ), ...$webhook, 'invoice' => array( 'title' => __( 'Factura' ), diff --git a/includes/class-integration-siigo-wc-plugin.php b/includes/class-integration-siigo-wc-plugin.php index 162eca2..9f1829c 100644 --- a/includes/class-integration-siigo-wc-plugin.php +++ b/includes/class-integration-siigo-wc-plugin.php @@ -115,10 +115,12 @@ private function _run(): void add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts')); add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts_admin')); add_action('woocommerce_order_status_changed', array('Integration_Siigo_WC', 'generate_invoice'), 10, 3); - add_action('integration_siigo_wc_smp_schedule', array('Integration_Siigo_WC', 'sync_products')); + add_action('integration_siigo_wc_smp_schedule', array('Integration_Siigo_WC', 'sync_products_siigo')); + add_action('integration_siigo_wc_smp_schedule_sync_woo_siigo', array('Integration_Siigo_WC', 'sync_products_woo')); add_action('wp_ajax_integration_siigo_sync_products', array($this, 'ajax_integration_siigo_sync_products')); + add_action('wp_ajax_integration_siigo_sync_woo_siigo', array($this, 'ajax_integration_siigo_sync_woo_siigo')); add_action('wp_ajax_integration_siigo_sync_webhook', array($this, 'ajax_integration_siigo_sync_webhook')); - add_action('woocommerce_admin_order_data_after_order_details', array($this, 'display_custom_editable_field_on_admin_orders') ); + add_action('woocommerce_admin_order_data_after_order_details', array($this, 'display_custom_editable_field_on_admin_orders'), 10); add_action('woocommerce_process_shop_order_meta', array($this, 'save_order_custom_field_meta'), 10); add_action('manage_shop_order_posts_custom_column', array($this, 'content_column_invoice'), 10, 2 ); @@ -221,7 +223,16 @@ public function ajax_integration_siigo_sync_products(): void if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'integration_siigo_sync_products' ) ) return; - Integration_Siigo_WC::sync_products(); + wp_schedule_single_event(time() + 5, 'integration_siigo_wc_smp_schedule'); + wp_send_json(['status' => true]); + } + + public function ajax_integration_siigo_sync_woo_siigo(): void + { + if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'integration_siigo_sync_woo_siigo' ) ) + return; + + wp_schedule_single_event(time() + 5, 'integration_siigo_wc_smp_schedule_sync_woo_siigo'); wp_send_json(['status' => true]); } @@ -297,25 +308,26 @@ public function document_woocommerce_fields(array $fields): array 'label' => __('Tipo de documento'), 'placeholder' => _x('', 'placeholder'), 'required' => true, - 'clear' => false, + 'clear' => true, 'type' => 'select', 'default' => 'CC', 'options' => array( 'CC' => __('Cédula de ciudadanía' ), 'NIT' => __('(NIT) Número de indentificación tributaria') - ) + ), + 'class' => apply_filters('class_field_type_document', array()) ); $fields['billing']['billing_dni'] = array( 'label' => __('Número de documento'), 'placeholder' => _x('', 'placeholder'), 'required' => true, - 'clear' => false, + 'clear' => true, 'type' => 'number', 'custom_attributes' => array( 'minlength' => 5 ), - 'class' => array('my-css') + 'class' => apply_filters('class_field_dni', array()) ); @@ -323,25 +335,26 @@ public function document_woocommerce_fields(array $fields): array 'label' => __('Tipo de documento'), 'placeholder' => _x('', 'placeholder'), 'required' => true, - 'clear' => false, + 'clear' => true, 'type' => 'select', 'default' => 'CC', 'options' => array( 'CC' => __('Cédula de ciudadanía' ), 'NIT' => __('(NIT) Número de indentificación tributaria') - ) + ), + 'class' => apply_filters('class_field_type_document', array()) ); $fields['shipping']['shipping_dni'] = array( 'label' => __('Número de documento'), 'placeholder' => _x('', 'placeholder'), 'required' => true, - 'clear' => false, + 'clear' => true, 'type' => 'number', 'custom_attributes' => array( 'minlength' => 5 ), - 'class' => array('my-css') + 'class' => apply_filters('class_field_dni', array()) ); return $fields; diff --git a/includes/class-integration-siigo-wc.php b/includes/class-integration-siigo-wc.php index f963b63..f76be65 100644 --- a/includes/class-integration-siigo-wc.php +++ b/includes/class-integration-siigo-wc.php @@ -51,7 +51,7 @@ public static function get_instance(): ?Client return self::$siigo; } - public static function sync_products(): void + public static function sync_products_siigo(): void { if (!self::get_instance()) return; @@ -106,6 +106,32 @@ public static function sync_products(): void } } + public static function sync_products_woo(): void + { + if (!self::get_instance()) return; + + $products_ids = get_posts( array( + 'post_type' => ['product','product_variation'], + 'numberposts' => -1, + 'post_status' => 'publish', + 'fields' => 'ids', + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => '_sku', + 'value' => '', + 'compare' => '!=' + ), + array( + 'key' => '_sync_siigo', + 'compare' => 'NOT EXISTS', + ), + ) + )); + + self::sync_products_to_siigo($products_ids); + } + public static function sync_products_to_siigo(array $ids): void { if (!self::get_instance()) return; diff --git a/includes/class-siigo-integration-wc.php b/includes/class-siigo-integration-wc.php index c7e6a55..07aa565 100644 --- a/includes/class-siigo-integration-wc.php +++ b/includes/class-siigo-integration-wc.php @@ -74,7 +74,7 @@ public function generate_button_html( $key, $data ): string { $field = $this->plugin_id . $this->id . '_' . $key; $defaults = array( - 'class' => 'button-secondary siigo-sync', + 'class' => '', 'css' => '', 'custom_attributes' => [ 'data-nonce' => wp_create_nonce( 'integration_siigo_sync_products' ), diff --git a/integration-siigo-woo.php b/integration-siigo-woo.php index 5d81ab9..507d57d 100644 --- a/integration-siigo-woo.php +++ b/integration-siigo-woo.php @@ -2,7 +2,7 @@ /** * Plugin Name: Integration Siigo Woocommerce * Description: Integración del sistama contable y de facturación Siigo para Woocoommerce - * Version: 0.0.15 + * Version: 0.0.16 * Author: Saul Morales Pacheco * Author URI: https://saulmoralespa.com * License: GNU General Public License v3.0 @@ -17,7 +17,7 @@ } if(!defined('INTEGRATION_SIIGO_WC_SMP_VERSION')){ - define('INTEGRATION_SIIGO_WC_SMP_VERSION', '0.0.15'); + define('INTEGRATION_SIIGO_WC_SMP_VERSION', '0.0.16'); } add_action( 'plugins_loaded', 'integration_siigo_wc_smp_init'); diff --git a/readme.txt b/readme.txt index 5525cef..fd2af10 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: commerce, e-commerce, commerce, WordPress ecommerce, store, sales, sell, s Requires at least: 6.0 Tested up to: 6.6.1 Requires PHP: 8.1 -Stable tag: 0.0.15 +Stable tag: 0.0.16 License: GNU General Public License v3.0 License URI: https://www.gnu.org/licenses/gpl-3.0.html