Skip to content

Commit

Permalink
Added button sync_woo_siigo
Browse files Browse the repository at this point in the history
Added filter class_field_dni
Added filter class_field_type_document
  • Loading branch information
Saul Morales committed Sep 30, 2024
1 parent 8a109eb commit 28dfb4f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 26 deletions.
24 changes: 18 additions & 6 deletions assets/js/integration-siigo.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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
Expand Down
19 changes: 15 additions & 4 deletions includes/admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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. <br> <strong>Nota:</strong> 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' ),
Expand Down
35 changes: 24 additions & 11 deletions includes/class-integration-siigo-wc-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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]);
}

Expand Down Expand Up @@ -297,51 +308,53 @@ 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())
);


$fields['shipping']['shipping_type_document'] = 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;
Expand Down
28 changes: 27 additions & 1 deletion includes/class-integration-siigo-wc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion includes/class-siigo-integration-wc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down
4 changes: 2 additions & 2 deletions integration-siigo-woo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 28dfb4f

Please sign in to comment.