Plugin Directory

Changeset 3425553


Ignore:
Timestamp:
12/22/2025 05:44:40 PM (2 months ago)
Author:
facturaone
Message:

v4.58

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-facturaone/trunk/wp-facturaone.php

    r3425202 r3425553  
    44Plugin URI: https://www.facturaone.com/
    55Description: La forma más fácil de trabajar con tu tienda online desde nuestro ERP FacturaONE.
    6 Version: 4.56
     6Version: 4.58
    77Author: FacturaONE
    88Author URI: https://www.facturaone.com/
     
    353353    if ($mod_wordpress <= 0) {return 0;}
    354354
    355     $_product_id = (int) fone_get_product_id_by_idfactura($item_lookup_id);
     355    $_product_id = (int) fone_get_product_id_by_idfactura($item_lookup_id,$item_sku);
    356356    $itemnameorig=$item_name;
    357357   
     
    29102910 * - PHP 7.4 compatible
    29112911 */
    2912 function fone_get_product_id_by_idfactura($idfactura){
     2912function fone_get_product_id_by_idfactura($idfactura,$sku=''){
    29132913    $idfactura = trim((string)$idfactura);
     2914    $sku       = trim((string)$sku);
    29142915    if ($idfactura === '') return 0;
    29152916
    2916     $cache_key = 'fone_idfactura_' . md5($idfactura);
     2917    $cache_key = 'fone_idfactura_' . md5($idfactura.'|'.$sku);
    29172918
    29182919    // OJO: si cachea 0, no retornamos todavía (evita duplicados)
     
    29232924
    29242925    global $wpdb;
     2926    $found = 0;
    29252927
    29262928    // 1) Buscar por meta fone_idfactura (lógica ERP)
    2927     $sql = "
     2929    $found = (int)$wpdb->get_var($wpdb->prepare("
    29282930        SELECT p.ID
    29292931        FROM {$wpdb->posts} p
     
    29352937        ORDER BY p.post_type ASC, p.ID DESC
    29362938        LIMIT 1
    2937     ";
    2938     $found = (int) $wpdb->get_var($wpdb->prepare($sql, 'fone_idfactura', $idfactura));
     2939    ", 'fone_idfactura', $idfactura));
    29392940
    29402941    // 2) Fallback: probar como post_id (más directo en 1 query)
     
    29422943        $pid = (int)$idfactura;
    29432944        if ($pid > 0) {
    2944             $found = (int) $wpdb->get_var($wpdb->prepare(
     2945            $found = (int)$wpdb->get_var($wpdb->prepare(
    29452946                "SELECT ID FROM {$wpdb->posts}
    2946                  WHERE ID=%d AND post_type IN ('product','product_variation')
     2947                 WHERE ID=%d
     2948                    AND post_type IN ('product','product_variation')
     2949                    AND post_status IN ('publish','draft','private','pending')
    29472950                 LIMIT 1",
    29482951                $pid
     
    29502953        }
    29512954    }
    2952 
    2953     // Cachea resultado final
    2954     wp_cache_set($cache_key, (int)$found, 'fone', 300);
     2955   
     2956    // 3) sku
     2957    if ($found <= 0 && $sku !== '') {
     2958        $lookup_table = $wpdb->prefix . 'wc_product_meta_lookup';
     2959        static $has_lookup = null;
     2960
     2961        if ($has_lookup === null) {
     2962            $has_lookup = ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $lookup_table)) === $lookup_table);
     2963        }
     2964
     2965        if ($has_lookup) {
     2966            $found = (int)$wpdb->get_var($wpdb->prepare(
     2967                "SELECT product_id FROM {$lookup_table} WHERE sku = %s LIMIT 1",
     2968                $sku
     2969            ));
     2970        }
     2971
     2972        if ($found <= 0) {
     2973            $found = (int)$wpdb->get_var($wpdb->prepare(
     2974                "SELECT p.ID
     2975                 FROM {$wpdb->posts} p
     2976                 INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID
     2977                 WHERE pm.meta_key = '_sku'
     2978                   AND pm.meta_value = %s
     2979                   AND p.post_type IN ('product','product_variation')
     2980                   AND p.post_status IN ('publish','draft','private','pending')
     2981                 ORDER BY p.post_type ASC, p.ID DESC
     2982                 LIMIT 1",
     2983                $sku
     2984            ));
     2985        }
     2986
     2987        if ($found <= 0) {
     2988            $found = (int)$wpdb->get_var($wpdb->prepare(
     2989                "SELECT p.ID
     2990                 FROM {$wpdb->posts} p
     2991                 INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID
     2992                 WHERE pm.meta_key = '_sku'
     2993                   AND LOWER(pm.meta_value) = LOWER(%s)
     2994                   AND p.post_type IN ('product','product_variation')
     2995                   AND p.post_status IN ('publish','draft','private','pending')
     2996                 ORDER BY p.post_type ASC, p.ID DESC
     2997                 LIMIT 1",
     2998                $sku
     2999            ));
     3000        }
     3001    }
     3002   
     3003    // cache solo si hay resultado
     3004    if ($found > 0) {
     3005        wp_cache_set($cache_key, (int)$found, 'fone', 300);
     3006    }
    29553007
    29563008    return (int)$found;
    29573009}
    29583010function fone_get_variation_id_by_idfactura($idfactura){
    2959     global $wpdb;
    2960     $idfactura = trim((string)$idfactura);
    2961     if ($idfactura==='') return 0;
    2962 
    2963     // Cache
    2964     $cache_key = 'fone_variation_idfactura_' . md5($idfactura);
    2965     $cached = wp_cache_get($cache_key, 'fone');
    2966     if ($cached !== false && (int)$cached > 0) {
    2967         return (int)$cached;
    2968     }
    2969    
    2970     // 1) Buscar por meta fone_idfactura (lógica ERP)
    2971     $sql = "
    2972         SELECT p.ID
    2973         FROM {$wpdb->posts} p
    2974         INNER JOIN {$wpdb->postmeta} pm ON pm.post_id=p.ID
    2975         WHERE pm.meta_key=%s
    2976           AND pm.meta_value=%s
    2977           AND p.post_type='product_variation'
    2978           AND p.post_status IN ('publish','draft','private','pending')
    2979         ORDER BY p.ID DESC
    2980         LIMIT 1
    2981     ";
    2982     //return (int)$wpdb->get_var($wpdb->prepare($sql,'fone_idfactura',$idfactura));
    2983     $found = (int) $wpdb->get_var(
    2984         $wpdb->prepare($sql, 'fone_idfactura', $idfactura)
    2985     );
    2986    
    2987    
    2988     // 2) Fallback: tratar idfactura como post_id (solo si numérico)
    2989     if ($found <= 0 && ctype_digit($idfactura)) {
    2990         $pid = (int) $idfactura;
    2991         if ($pid > 0) {
    2992             $post_type = (string) $wpdb->get_var(
    2993                 $wpdb->prepare(
    2994                     "SELECT post_type FROM {$wpdb->posts} WHERE ID = %d LIMIT 1",
    2995                     $pid
    2996                 )
    2997             );
    2998             if ($post_type === 'product_variation') {
    2999                 $found = $pid;
    3000             }
    3001         }
    3002     }
    3003 
    3004     // Cachear resultado final (incluye 0)
    3005     wp_cache_set($cache_key, (int) $found, 'fone', 300);
    3006 
    3007     return (int) $found;   
    3008 }
    3009 
     3011    $id = (int) fone_get_product_id_by_idfactura($idfactura, '');
     3012    if ($id <= 0) return 0;
     3013    return (get_post_type($id) === 'product_variation') ? $id : 0;
     3014}
    30103015
    30113016
Note: See TracChangeset for help on using the changeset viewer.