Plugin Directory

Changeset 3453301


Ignore:
Timestamp:
02/03/2026 09:11:17 PM (8 weeks ago)
Author:
meliconnect
Message:

Update trunk to version 1.6.0

Location:
meliconnect/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • meliconnect/trunk/includes/Core/ApiManager.php

    r3372090 r3453301  
    99use Meliconnect\Meliconnect\Core\Helpers\Helper;
    1010use Meliconnect\Meliconnect\Core\Models\UserConnection;
     11use Meliconnect\Meliconnect\Modules\Importer\Services\ProductDataFacade;
     12use Meliconnect\Meliconnect\Modules\Importer\Services\WooCommerceProductAdapter;
     13use Meliconnect\Meliconnect\Modules\Importer\Services\WooCommerceProductCreationService;
    1114
    1215class ApiManager {
     
    4144            )
    4245        );
     46
     47        register_rest_route(
     48            'meliconnect/v1',
     49            '/sync/product',
     50            array(
     51                'methods'               => 'POST',
     52                'callback'              => array( $this, 'meliconnect_sync_product_callback' ),
     53                //'permission_callback' => [ ApiAuth::class, 'authorize' ],
     54                'permission_callback' => '__return_true',
     55            )
     56        );
    4357    }
     58
     59    public static function meliconnect_sync_product_callback( $request )
     60    {
     61        $meli_listing_id = sanitize_text_field( $request->get_param( 'meli_listing_id' ) );
     62        $seller_id  = sanitize_text_field( $request->get_param( 'seller_id' ) );
     63
     64        Helper::logData(
     65            sprintf(
     66                'Sync product request: listing=%s seller=%s',
     67                $meli_listing_id,
     68                $seller_id
     69            ),
     70            'sync_product_callback'
     71        );
     72
     73        if ( empty( $meli_listing_id ) || empty( $seller_id ) ) {
     74            return new \WP_Error(
     75                'invalid_payload',
     76                'Missing parameters',
     77                [ 'status' => 400 ]
     78            );
     79        }
     80
     81        try {
     82
     83            /* ---- */
     84            $woo_product_id = Helper::meliconnect_find_product_by_listing_id( $meli_listing_id );
     85
     86            if(! $woo_product_id ) {
     87                Helper::logData( 'Product not found: ' . $meli_listing_id, 'sync_product_callback' );
     88               
     89                return new \WP_Error(
     90                    'product_not_found',
     91                    'Product not found',
     92                    [ 'status' => 404 ]
     93                );
     94            }
     95           
     96            Helper::logData( 'Product found: ' . $woo_product_id, 'sync_product_callback' );
     97
     98            $template_id = get_post_meta($woo_product_id,'meliconnect_asoc_template_id',true ) ?? null;
     99
     100            if ( empty( $template_id ) ) {
     101                Helper::logData(
     102                    'Template not found for product ' . $woo_product_id,
     103                    'sync_product_callback'
     104                );
     105            }
     106               
     107            $wooCommerceAdapter     = new WooCommerceProductAdapter();
     108            $productCreationService = new WooCommerceProductCreationService();
     109            $productDataFacade      = new ProductDataFacade( $wooCommerceAdapter, $productCreationService );
     110
     111
     112            // 2- Format items data to send, Send data to API server and Get response and process response creating or updating items in WooCommerce
     113            $productDataFacade->importAndCreateProduct( $meli_listing_id, $seller_id, $template_id, $woo_product_id );
     114            /* ---- */
     115
     116            return rest_ensure_response([
     117                'success' => true,
     118                'meli_listing_id' => $meli_listing_id,
     119                'woo_product_id' => $woo_product_id
     120            ]);
     121
     122        } catch ( \Throwable $e ) {
     123
     124
     125            Helper::logData( 'Sync product failed: ' . $e->getMessage() . ' On listing: ' . $meli_listing_id, 'sync_product_callback' );
     126
     127            return new \WP_Error(
     128                'sync_failed',
     129                'Could not sync product',
     130                [ 'status' => 500 ]
     131            );
     132        }
     133    }
    44134
    45135    // Callback del endpoint
  • meliconnect/trunk/includes/Core/CronManager.php

    r3439817 r3453301  
    240240        $lock_acquired = false;
    241241
    242         Helper::logData(
     242        /* Helper::logData(
    243243            'CUSTOM IMPORT CRON START PID ' . getmypid(),
    244244            'custom-import'
    245         );
     245        ); */
    246246
    247247        try {
     
    267267
    268268            if ( empty($items) ) {
    269                 Helper::logData('No pending items. Import finished.', 'custom-import');
     269                /* Helper::logData('No pending items. Import finished.', 'custom-import'); */
    270270                return;
    271271            }
     
    361361            }
    362362
    363             Helper::logData(
     363            /* Helper::logData(
    364364                'CUSTOM IMPORT CRON END',
    365365                'custom-import'
    366             );
     366            ); */
    367367        }
    368368    }
  • meliconnect/trunk/includes/Core/Helpers/Helper.php

    r3439817 r3453301  
    811811        }
    812812    }
     813
     814    public static function meliconnect_find_product_by_listing_id( string $listingId ): ?int
     815    {
     816        global $wpdb;
     817
     818        $postId = $wpdb->get_var(
     819            $wpdb->prepare(
     820                "
     821                SELECT post_id
     822                FROM {$wpdb->postmeta}
     823                WHERE meta_key = 'meliconnect_meli_listing_id'
     824                AND meta_value = %s
     825                LIMIT 1
     826                ",
     827                $listingId
     828            )
     829        );
     830
     831        return $postId ? (int) $postId : null;
     832    }
     833
     834    public static function meliconnect_convert_weight_to_store_unit( $grams ) {
     835        $unit = get_option( 'woocommerce_weight_unit', 'kg' );
     836
     837        return match ( $unit ) {
     838            'kg' => $grams / 1000,
     839            'g'  => $grams,
     840            'lbs'=> $grams * 0.00220462,
     841            'oz' => $grams * 0.035274,
     842            default => $grams,
     843        };
     844    }
     845
     846    public static function meliconnect_convert_dimension_to_store_unit( $cm ) {
     847        $unit = get_option( 'woocommerce_dimension_unit', 'cm' );
     848
     849        return match ( $unit ) {
     850            'cm' => $cm,
     851            'm'  => $cm / 100,
     852            'mm' => $cm * 10,
     853            'in' => $cm * 0.393701,
     854            default => $cm,
     855        };
     856    }
    813857}
  • meliconnect/trunk/includes/Modules/Importer/Services/WooCommerceProductCreationService.php

    r3439817 r3453301  
    195195        // Dimensiones
    196196        if ( isset( $product_data['weight'] ) && $product_data['weight'] !== false ) {
    197             $woo_product->set_weight( $product_data['weight'] );
     197            $woo_product->set_weight(
     198                Helper::meliconnect_convert_weight_to_store_unit(   $product_data['weight'] )
     199            );
    198200        } else {
    199201            Helper::logData( 'Weight ignored by settings', 'custom-import' );
     
    201203
    202204        if ( isset( $product_data['length'] ) && $product_data['length'] !== false ) {
    203             $woo_product->set_length( $product_data['length'] );
     205            $woo_product->set_length(
     206                Helper::meliconnect_convert_dimension_to_store_unit(    $product_data['length'] )
     207            );
    204208        } else {
    205209            Helper::logData( 'Length ignored by settings', 'custom-import' );
     
    207211
    208212        if ( isset( $product_data['width'] ) && $product_data['width'] !== false ) {
    209             $woo_product->set_width( $product_data['width'] );
     213            $woo_product->set_width(
     214                 Helper::meliconnect_convert_dimension_to_store_unit(   $product_data['width']  )
     215            );
    210216        } else {
    211217            Helper::logData( 'Width ignored by settings', 'custom-import' );
     
    213219
    214220        if ( isset( $product_data['height'] ) && $product_data['height'] !== false ) {
    215             $woo_product->set_height( $product_data['height'] );
     221            $woo_product->set_height(
     222                Helper::meliconnect_convert_dimension_to_store_unit(   $product_data['height'] )           
     223            );
    216224        } else {
    217225            Helper::logData( 'Height ignored by settings', 'custom-import' );
     
    594602        $log_text .= "\n\n\n";
    595603
    596         // Helper::logData($log_text, 'custom-import');
     604        /* Helper::logData($log_text, 'custom-import'); */
    597605
    598606        return $productAttributes;
  • meliconnect/trunk/meliconnect.php

    r3439817 r3453301  
    44Plugin URI: https://mercadolibre.meliconnect.com/
    55Description: WooCommerce & Mercado Libre integration to import, export, and synchronize products between your WooCommerce store and Mercado Libre accounts.
    6 Version: 1.4.0
     6Version: 1.6.0
    77Author: meliconnect
    88Text Domain: meliconnect
     
    2626 * Define constantes del plugin
    2727 */
    28 define( 'MELICONNECT_VERSION', '1.4.0' );
     28define( 'MELICONNECT_VERSION', '1.6.0' );
    2929define( 'MELICONNECT_DATABASE_VERSION', '1.1.1' );
    3030define( 'MELICONNECT_TEXTDOMAIN', 'meliconnect' );
  • meliconnect/trunk/readme.txt

    r3439817 r3453301  
    66Requires PHP:    8.0
    77Tested up to: 6.9
    8 Stable tag: 1.4.0
     8Stable tag: 1.6.0
    99License: GPLv3
    1010License URI: https://www.gnu.org/licenses/gpl-3.0.html
Note: See TracChangeset for help on using the changeset viewer.