Changeset 3425553
- Timestamp:
- 12/22/2025 05:44:40 PM (2 months ago)
- File:
-
- 1 edited
-
wp-facturaone/trunk/wp-facturaone.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-facturaone/trunk/wp-facturaone.php
r3425202 r3425553 4 4 Plugin URI: https://www.facturaone.com/ 5 5 Description: La forma más fácil de trabajar con tu tienda online desde nuestro ERP FacturaONE. 6 Version: 4.5 66 Version: 4.58 7 7 Author: FacturaONE 8 8 Author URI: https://www.facturaone.com/ … … 353 353 if ($mod_wordpress <= 0) {return 0;} 354 354 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); 356 356 $itemnameorig=$item_name; 357 357 … … 2910 2910 * - PHP 7.4 compatible 2911 2911 */ 2912 function fone_get_product_id_by_idfactura($idfactura ){2912 function fone_get_product_id_by_idfactura($idfactura,$sku=''){ 2913 2913 $idfactura = trim((string)$idfactura); 2914 $sku = trim((string)$sku); 2914 2915 if ($idfactura === '') return 0; 2915 2916 2916 $cache_key = 'fone_idfactura_' . md5($idfactura );2917 $cache_key = 'fone_idfactura_' . md5($idfactura.'|'.$sku); 2917 2918 2918 2919 // OJO: si cachea 0, no retornamos todavía (evita duplicados) … … 2923 2924 2924 2925 global $wpdb; 2926 $found = 0; 2925 2927 2926 2928 // 1) Buscar por meta fone_idfactura (lógica ERP) 2927 $ sql ="2929 $found = (int)$wpdb->get_var($wpdb->prepare(" 2928 2930 SELECT p.ID 2929 2931 FROM {$wpdb->posts} p … … 2935 2937 ORDER BY p.post_type ASC, p.ID DESC 2936 2938 LIMIT 1 2937 "; 2938 $found = (int) $wpdb->get_var($wpdb->prepare($sql, 'fone_idfactura', $idfactura)); 2939 ", 'fone_idfactura', $idfactura)); 2939 2940 2940 2941 // 2) Fallback: probar como post_id (más directo en 1 query) … … 2942 2943 $pid = (int)$idfactura; 2943 2944 if ($pid > 0) { 2944 $found = (int) $wpdb->get_var($wpdb->prepare(2945 $found = (int)$wpdb->get_var($wpdb->prepare( 2945 2946 "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') 2947 2950 LIMIT 1", 2948 2951 $pid … … 2950 2953 } 2951 2954 } 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 } 2955 3007 2956 3008 return (int)$found; 2957 3009 } 2958 3010 function 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 } 3010 3015 3011 3016
Note: See TracChangeset
for help on using the changeset viewer.