Plugin Directory

Changeset 3457385


Ignore:
Timestamp:
02/09/2026 07:33:06 PM (4 weeks ago)
Author:
saulmorales
Message:

Added determine product type by weigh

Location:
shipping-servientrega-woocommerce/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • shipping-servientrega-woocommerce/trunk/includes/admin/general.php

    r3311699 r3457385  
    3535    $wc_main_settings['servientrega_product_type'] = (isset($_POST['servientrega_product_type'])) ? sanitize_text_field($_POST['servientrega_product_type']) : '';
    3636    $wc_main_settings['servientrega_print_type'] = (isset($_POST['servientrega_print_type'])) ? sanitize_text_field($_POST['servientrega_print_type']) : '';
     37
     38    // Guardar configuración de rangos de peso.
     39    $wc_main_settings['servientrega_premier_max_weight'] = '';
     40    $wc_main_settings['servientrega_industrial_min_weight'] = '';
     41
     42    if (isset($_POST['servientrega_premier_max_weight']) && is_numeric($_POST['servientrega_premier_max_weight'])) {
     43        $premier_max = floatval(sanitize_text_field($_POST['servientrega_premier_max_weight']));
     44        if ($premier_max > 0) {
     45            $wc_main_settings['servientrega_premier_max_weight'] = $premier_max;
     46        }
     47    }
     48
     49    if (isset($_POST['servientrega_industrial_min_weight']) && is_numeric($_POST['servientrega_industrial_min_weight'])) {
     50        $industrial_min = floatval(sanitize_text_field($_POST['servientrega_industrial_min_weight']));
     51        if ($industrial_min > 0) {
     52            $wc_main_settings['servientrega_industrial_min_weight'] = $industrial_min;
     53        }
     54    }
    3755
    3856    update_option('woocommerce_servientrega_shipping_settings', $wc_main_settings);
     
    8199        ];
    82100
     101        // Usar peso de prueba para determinar tipo de producto.
     102        $test_weight = 3; // kg de prueba.
     103        $product_type_test = Shipping_Servientrega_WC::determine_product_type_by_weight(
     104            $test_weight,
     105            $wc_main_settings['servientrega_product_type']
     106        );
     107
    83108        $params = array(
    84109            'Num_Guia' => 0,
     
    86111            'Num_Piezas' => 1,
    87112            'Des_TipoTrayecto' => 1,
    88             'Ide_Producto' => (int)$wc_main_settings['servientrega_product_type'],
     113            'Ide_Producto' => (int)$product_type_test,
    89114            'Ide_Destinatarios' => '00000000-0000-0000-0000-000000000000',
    90115            'Ide_Manifiesto' => '00000000-0000-0000-0000-000000000000',
     
    110135            'idePaisOrigen' => 1,
    111136            'idePaisDestino' => 1,
    112             'Des_IdArchivoOrigen' => 1,
     137            'Des_IdArchivoOrigen' => 0,
    113138            'Des_DireccionRemitente' => '',
    114139            'Num_PesoFacturado' => 0,
     
    135160        );
    136161
    137         if($wc_main_settings['servientrega_product_type'] == 6){
     162        if($product_type_test == 6){
    138163            $params['objEnviosUnidadEmpaqueCargue'] = [
    139164                'EnviosUnidadEmpaqueCargue' => $unidades
     
    150175
    151176        $params = [
    152             'IdProducto'          => $wc_main_settings['servientrega_product_type'],
     177            'IdProducto'          => $product_type_test,
    153178            'NumeroPiezas'        => 1,
    154179            'Piezas'              =>
    155180                [
    156181                    [
    157                         'Peso'  => $wc_main_settings['servientrega_product_type'] == 2 ? 3 : 1,
     182                        'Peso'  => $product_type_test == 2 ? 3 : 1,
    158183                        'Largo' => 10,
    159184                        'Ancho' => 5,
     
    299324        </td>
    300325     </tr>';
     326
     327    // Sección de configuración automática por peso.
     328    $htmlGeneral .= '<tr valign="top" id="weight_range_config">
     329        <td colspan="2">
     330            <hr style="margin: 20px 0; border: 0; border-top: 1px solid #ddd;">
     331            <h3 style="margin: 10px 0;">' . __('Configuración automática por peso') . '</h3>
     332            <p style="color: #666; font-size: 12px; margin: 5px 0 15px 0;">
     333                ' . __('Configure rangos de peso para seleccionar automáticamente el tipo de producto entre Mercancía Premier y Mercancía Industrial. Si no se configura, se usará el tipo seleccionado manualmente arriba. El tipo "Documento unitario" se mantiene como selección manual.') . '
     334            </p>
     335        </td>
     336    </tr>';
     337
     338    $htmlGeneral .= '<tr valign="top">
     339        <td style="width:25%;font-weight:bold;">
     340            <label for="servientrega_premier_max_weight">' . __('Peso máximo para Premier (kg)') . '</label>
     341            <span class="woocommerce-help-tip" data-tip="' . __('Peso máximo en kilogramos para usar Mercancía Premier. Pesos superiores usarán Mercancía Industrial automáticamente. Ejemplo: 6') . '"></span>
     342        </td>
     343        <td scope="row" class="titledesc" style="display: block;margin-bottom: 20px;margin-top: 3px;">
     344            <fieldset style="padding:3px;">
     345                <input
     346                    type="number"
     347                    step="1"
     348                    min="1"
     349                    name="servientrega_premier_max_weight"
     350                    id="servientrega_premier_max_weight"
     351                    value="' . (isset($general_settings['servientrega_premier_max_weight']) ? esc_attr($general_settings['servientrega_premier_max_weight']) : '') . '"
     352                    placeholder="6"
     353                    class="input-text regular-input"
     354                    style="width: 150px;"
     355                />
     356                <p style="color: #666; font-size: 11px; margin-top: 5px;">
     357                    ' . __('Ejemplo: Si configura 6 kg, pedidos hasta 6 kg usarán Premier (tipo 2), y desde 7 kg usarán Industrial (tipo 6).') . '
     358                </p>
     359            </fieldset>
     360        </td>
     361    </tr>';
     362
     363    $htmlGeneral .= '<tr valign="top">
     364        <td style="width:25%;font-weight:bold;">
     365            <label for="servientrega_industrial_min_weight">
     366                ' . __('Peso mínimo para Industrial (kg)') . ' <em style="font-weight:normal;">' . __('(Opcional)') . '</em>
     367            </label>
     368            <span class="woocommerce-help-tip" data-tip="' . __('Peso mínimo en kilogramos para usar Mercancía Industrial. Dejar vacío para usar automáticamente el siguiente valor al máximo de Premier.') . '"></span>
     369        </td>
     370        <td scope="row" class="titledesc" style="display: block;margin-bottom: 20px;margin-top: 3px;">
     371            <fieldset style="padding:3px;">
     372                <input
     373                    type="number"
     374                    step="1"
     375                    min="1"
     376                    name="servientrega_industrial_min_weight"
     377                    id="servientrega_industrial_min_weight"
     378                    value="' . (isset($general_settings['servientrega_industrial_min_weight']) ? esc_attr($general_settings['servientrega_industrial_min_weight']) : '') . '"
     379                    placeholder="7"
     380                    class="input-text regular-input"
     381                    style="width: 150px;"
     382                />
     383                <p style="color: #666; font-size: 11px; margin-top: 5px;">
     384                    ' . __('Dejar vacío para usar el rango automático. Solo configure si necesita un gap entre rangos (ej: Premier hasta 6kg, Industrial desde 7kg).') . '
     385                </p>
     386            </fieldset>
     387        </td>
     388    </tr>';
     389
    301390    $htmlGeneral .= '<tr valign="top">
    302391        <td style="width:25%;font-weight:bold;">
  • shipping-servientrega-woocommerce/trunk/includes/class-method-shipping-servientrega-wc.php

    r3037317 r3457385  
    3434        $this->servientrega_print_type = isset($wc_main_settings['servientrega_print_type']) ? $wc_main_settings['servientrega_print_type'] : 2;
    3535        $this->rates_servientrega = isset($wc_main_settings['rate']) ? $wc_main_settings['rate'] : [];
     36        // Cargar configuración de rangos de peso.
     37        $this->servientrega_premier_max_weight = isset($wc_main_settings['servientrega_premier_max_weight']) ? floatval($wc_main_settings['servientrega_premier_max_weight']) : 0;
     38        $this->servientrega_industrial_min_weight = isset($wc_main_settings['servientrega_industrial_min_weight']) ? floatval($wc_main_settings['servientrega_industrial_min_weight']) : 0;
    3639
    3740        $this->supports = array(
     
    195198    public function calculate_cost(array $data_products, $origin, $destine)
    196199    {
     200        // Determinar el tipo de producto basado en el peso.
     201        $product_type = Shipping_Servientrega_WC::determine_product_type_by_weight(
     202            $data_products['weight'],
     203            $this->servientrega_product_type
     204        );
     205
    197206        $params = [
    198             'IdProducto'          => $this->servientrega_product_type,
    199             'NumeroPiezas'        => $this->servientrega_product_type === 6 ? count($data_products['pieces']) : 1,
    200             'Piezas'              => $this->servientrega_product_type === 6 ? $data_products['pieces'] :
     207            'IdProducto'          => $product_type,
     208            'NumeroPiezas'        => $product_type === 6 ? count($data_products['pieces']) : 1,
     209            'Piezas'              => $product_type === 6 ? $data_products['pieces'] :
    201210                [
    202211                    [
  • shipping-servientrega-woocommerce/trunk/includes/class-shipping-servientrega-wc.php

    r3311655 r3457385  
    137137            $namesProducts = implode(" ",  $data_products['name_products']);
    138138
     139            // Determinar tipo de producto basado en peso del pedido.
     140            $product_type_by_weight = self::determine_product_type_by_weight(
     141                $data_products['weight'],
     142                $instance->servientrega_product_type
     143            );
     144
    139145            $params = [
    140146                'Num_Guia' => 0,
     
    142148                'Num_Piezas' => count($data_products['units']),
    143149                'Des_TipoTrayecto' => 1, //nacional 2 internacional
    144                 'Ide_Producto' => (int)$instance->servientrega_product_type, //mercancia premier
     150                'Ide_Producto' => (int)$product_type_by_weight,
    145151                'Ide_Destinatarios' => '00000000-0000-0000-0000-000000000000',
    146152                'Ide_Manifiesto' => '00000000-0000-0000-0000-000000000000',
     
    191197            $order_id_origin = self::get_parent_id($order);
    192198
    193             if($instance->servientrega_product_type == 6){
     199            if($product_type_by_weight == 6){
    194200                $params['objEnviosUnidadEmpaqueCargue'] = [
    195201                    'EnviosUnidadEmpaqueCargue' => $data_products['units']
     
    370376    }
    371377
     378    /**
     379     * Determina el tipo de producto basándose en el peso y la configuración de rangos.
     380     *
     381     * @param float $weight Peso en kilogramos del pedido.
     382     * @param int $manual_type Tipo de producto seleccionado manualmente (1, 2 o 6).
     383     * @return int Tipo de producto a usar (1, 2 o 6).
     384     */
     385    public static function determine_product_type_by_weight(float $weight, int $manual_type): int
     386    {
     387        // Si es tipo 1 (Documento unitario), siempre usar el manual.
     388        if ($manual_type == 1) {
     389            return 1;
     390        }
     391
     392        // Obtener configuración de rangos de peso.
     393        $wc_settings = get_option('woocommerce_servientrega_shipping_settings');
     394        $premier_max_weight = isset($wc_settings['servientrega_premier_max_weight'])
     395            ? floatval($wc_settings['servientrega_premier_max_weight'])
     396            : 0;
     397        $industrial_min_weight = isset($wc_settings['servientrega_industrial_min_weight'])
     398            ? floatval($wc_settings['servientrega_industrial_min_weight'])
     399            : 0;
     400
     401        // Si no hay configuración de rangos, usar el tipo manual.
     402        if ($premier_max_weight <= 0) {
     403            return (int)$manual_type;
     404        }
     405
     406        // Determinar el peso mínimo para Industrial.
     407        // Si no está configurado, usar premier_max + 0.1.
     408        if ($industrial_min_weight <= 0) {
     409            $industrial_min_weight = $premier_max_weight + 1;
     410        }
     411
     412        // Aplicar lógica de rangos.
     413        if ($weight <= $premier_max_weight) {
     414            return 2; // Mercancía Premier.
     415        } elseif ($weight >= $industrial_min_weight) {
     416            return 6; // Mercancía Industrial.
     417        }
     418
     419        // Si está en el gap entre rangos, usar el tipo manual.
     420        return (int)$manual_type;
     421    }
     422
    372423    public static function generate_stickers($guide_number)
    373424    {
     
    523574
    524575}
     576
     577
     578
  • shipping-servientrega-woocommerce/trunk/readme.txt

    r3311699 r3457385  
    44Tags: commerce, e-commerce, commerce, WordPress ecommerce, store, sales, sell, shop, shopping, cart, checkout, configurable, Colombia, servientrega
    55Requires at least: 6.0
    6 Tested up to: 6.8.1
    7 Requires PHP: 8.1
    8 Stable tag: 7.0.6
     6Tested up to: 6.9
     7Requires PHP: 8.2
     8Stable tag: 7.0.7
     9WC requires at least: 9.0
     10WC tested up to: 10.4
    911License: GNU General Public License v3.0
    1012License URI: http://www.gnu.org/licenses/gpl-3.0.html
  • shipping-servientrega-woocommerce/trunk/shipping-servientrega-wc.php

    r3311699 r3457385  
    33 * Plugin Name: Shipping Servientrega Woocommerce
    44 * Description: Shipping Servientrega Woocommerce is available for Colombia
    5  * Version: 7.0.6
     5 * Version: 7.0.7
    66 * Author: Saúl Morales Pacheco
    77 * Author URI: https://saulmoralespa.com
    88 * License: GNU General Public License v3.0
    99 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
    10  * WC tested up to: 9.8.5
     10 * WC tested up to: 10.4
    1111 * WC requires at least: 4.0
     12 * Requires at least: 6.0
     13 * Tested up to: 6.9
     14 * Requires PHP: 8.2
     15 * Requires Plugins: woocommerce,departamentos-y-ciudades-de-colombia-para-woocommerce
    1216 */
    1317
     
    1721
    1822if(!defined('SHIPPING_SERVIENTREGA_WC_SS_VERSION')){
    19     define('SHIPPING_SERVIENTREGA_WC_SS_VERSION', '7.0.6');
     23    define('SHIPPING_SERVIENTREGA_WC_SS_VERSION', '7.0.7');
    2024}
    2125
     
    3034);
    3135
    32 function shipping_servientrega_wc_ss_init()
     36function shipping_servientrega_wc_ss_init(): void
    3337{
    3438    if ( ! shipping_servientrega_wc_ss_requirements() )
     
    3943}
    4044
    41 function shipping_servientrega_wc_ss_notices( $notice ) {
     45function shipping_servientrega_wc_ss_notices( $notice ): void
     46{
    4247    ?>
    4348    <div class="error notice">
     
    4752}
    4853
    49 function shipping_servientrega_wc_ss_requirements(){
     54function shipping_servientrega_wc_ss_requirements(): bool
     55{
    5056
    5157    if ( ! function_exists( 'is_plugin_active' ) ) require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
     
    97103                function() {
    98104                    shipping_servientrega_wc_ss_notices( 'Shipping Servientrega Woocommerce requiere la extensión soap se encuentre instalada' );
    99                 }
    100             );
    101         }
    102         return false;
    103     }
    104 
    105     if ( ! is_plugin_active(
    106         'woocommerce/woocommerce.php'
    107     ) ) {
    108         if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
    109             add_action(
    110                 'admin_notices',
    111                 function() {
    112                     shipping_servientrega_wc_ss_notices( 'Shipping Servientrega Woocommerce requiere que se encuentre instalado y activo el plugin: Woocommerce' );
    113105                }
    114106            );
     
    168160    $default_country = $woo_countries->get_base_country();
    169161
    170     if ( ! in_array( $default_country, array( 'CO' ), true ) ) {
     162    if ($default_country !== 'CO') {
    171163        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
    172164            add_action(
     
    216208}
    217209
    218 function activate_shipping_servientrega_wc_ss(){
     210function activate_shipping_servientrega_wc_ss(): void
     211{
    219212    wp_schedule_event( time(), 'twicedaily', 'shipping_servientrega_wc_ss_schedule' );
    220213}
    221214
    222 function deactivation_shipping_servientrega_wc_ss(){
     215function deactivation_shipping_servientrega_wc_ss(): void
     216{
    223217    delete_option('servientrega_validation_error');
    224218    wp_clear_scheduled_hook( 'shipping_servientrega_wc_ss_schedule' );
Note: See TracChangeset for help on using the changeset viewer.