Plugin Directory

Changeset 3208968


Ignore:
Timestamp:
12/17/2024 08:42:46 AM (15 months ago)
Author:
extendago
Message:

Version 1.5.7

Location:
extendago-wp-connection/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • extendago-wp-connection/trunk/extendago-wp-connection.php

    r3176970 r3208968  
    44Plugin URI:  http://www.arture.nl/extendago
    55Description: The Wordpress plugin for connecting Woocommerce with Extenda GO / Wallmob. You can manage your products inside Extenda GO or make your webshop as leading foor product manangement. You Stock changes will be two-way binding.
    6 Version:     1.5.6
     6Version:     1.5.7
    77Requires Plugins: woocommerce
    88Author:      Arture B.V.
  • extendago-wp-connection/trunk/includes/api/class-extendago-web-api-functions.php

    r3176970 r3208968  
    2424    public function extendago_update_wc_product( $product, $full_sync = false ){
    2525
     26        $logging = new ExtendaGo_WP_Connection_Logging();
     27
    2628        $product = apply_filters('before_extendago_product_process', $product);
    2729
     
    3537        // Find product by SKU
    3638        $post_id = $this->functions->custom_get_product_id_by_sku( $product['id'], $product['sku'] );
     39
     40
     41        $logging->log_file_write('DEBUG | POST ID: ' .$post_id);
    3742
    3843        $hide_from_web = 0;
     
    127132            }
    128133
     134            $logging->log_file_write('DEBUG | INSERT POST');
    129135            $post_id = wp_insert_post( $args );
    130136        }
     
    183189            }
    184190
     191            $logging->log_file_write('DEBUG | UPDATE POST');
    185192            wp_update_post( $args );
    186193        }
     
    467474                if (!isset($extendago_keep_product_status) || !$extendago_keep_product_status) {
    468475                    $Product->set_status($no_image_post_status);
    469 
    470                     // LOG
    471                     $logging = new ExtendaGo_WP_Connection_Logging();
    472476                    $logging->log_file_write('Status | Post (' . $post_id . ') has no image. Updating post-status to: ' . $no_image_post_status);
    473477                }
  • extendago-wp-connection/trunk/includes/class-extendago-wp-connection-functions.php

    r2945883 r3208968  
    4040    }
    4141
    42     public function custom_get_product_id_by_sku( $product_id, $sku ) {
     42    public function custom_get_product_id_by_sku( $extendago_product_id, $sku ) {
     43
     44        $logging = new ExtendaGo_WP_Connection_Logging();
    4345
    4446        $args = array(
    45             'post_type' => 'product',
    46             'post_status' => 'any',
     47            'post_type'  => 'product',
    4748            'posts_per_page' => -1,
     49            'post_status' => array('publish', 'draft', 'pending', 'private', 'trash'),
    4850            'meta_query' => array(
    4951                array(
    50                     'key'       => 'extendago_product_id',
    51                     'value'     => $product_id
     52                    'key'     => 'extendago_product_id',
     53                    'value'   => $extendago_product_id,
     54                    'compare' => '='
    5255                )
    5356            ),
    54             'fields' => 'ids'
     57            'fields' => 'ids', // Retourneer alleen IDs
    5558        );
    5659        $product_query = new WP_Query($args);
     
    7477        }
    7578
     79        // Fallback if Product Query not working
     80        if( empty($product_ids) ) {
     81            global $wpdb;
     82            $results = $wpdb->get_results("
     83                SELECT p.ID
     84                FROM {$wpdb->posts} p
     85                JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
     86                WHERE pm.meta_key = 'extendago_product_id'
     87                AND pm.meta_value = '{$extendago_product_id}'
     88                AND p.post_type = 'product'
     89                AND p.post_status IN ('publish', 'draft', 'pending', 'private', 'trash')
     90            ");
     91            $product_ids = wp_list_pluck($results, 'ID');
     92        }
     93
    7694        // Remove duplicates based on SKU ( Unique identifier )
    7795        if( count($product_ids) > 1 ){
    78             $logging = new ExtendaGo_WP_Connection_Logging();
    7996            $product_id = end($product_ids);
    8097
     
    93110        }
    94111
    95         return ( $product_id ) ? intval( $product_id ) : 0;
     112        return $product_id;
    96113    }
    97114
  • extendago-wp-connection/trunk/includes/cronjob/class-extendago-cronjob-functions.php

    r3176970 r3208968  
    4646        if( $this->endsWith($_SERVER["REQUEST_URI"], '/extendago-process-changes') ){
    4747
     48            $this->logging->log_file_write( 'Cronjob Changes | SERVER' );
     49
    4850            // Check for existing export
    4951            if (isset($files) && count($files) > 3) {
     
    5153            }
    5254            else{
    53                 $this->logging->log_file_write( 'Cronjob Changes | SERVER' );
    5455                $this->process_changes_wc_products();
    5556            }
     
    569570
    570571            $this->logging->log_file_write( 'Product cronjob | Processing batches' );
     572            $processing_batch_time = time();
     573            update_option( 'processing_batch_time', $processing_batch_time );
    571574
    572575            natcasesort( $files );
     
    12661269        $extendago_location_id = get_option( "extendago_location_id" );
    12671270
    1268         $params = array(
    1269             'from'      => time() - 60*12, // Afgelopen 12 minuten
    1270             'include'   => json_encode(array('products', 'promotion_campaigns', 'product_categories'))
    1271         );
     1271        $new_processed_changes_time = time();
     1272        $last_processed_changes_time = get_option('last_processed_changes_time');
     1273        if( isset($last_processed_changes_time) && !empty($last_processed_changes_time) ){
     1274            $params = array(
     1275                'from'      => $last_processed_changes_time,
     1276                'include'   => json_encode(array('products', 'promotion_campaigns', 'product_categories'))
     1277            );
     1278        }
     1279        else{
     1280            $params = array(
     1281                'from'      => time() - 60*12, // Afgelopen 12 minuten
     1282                'include'   => json_encode(array('products', 'promotion_campaigns', 'product_categories'))
     1283            );
     1284        }
    12721285        $changes = $web_api->listChanges($params);
    12731286
     
    15311544                            $product = $product_change;
    15321545
    1533                             $remove_from_web = false;
    1534                             foreach( $product['attributes'] as $product_attribute ){
    1535                                 foreach( $product_attribute as $attribute_key => $attribute_value ) {
    1536                                     if( ( $attribute_key == 'hide_from_web' && $attribute_value == '1' ) || ( $attribute_key == 'active' && $attribute_value == '0' ) ){
    1537                                         $remove_from_web = true;
    1538                                     }
     1546                            $hide_from_web = false;
     1547                            foreach( $product['attributes'] as $key => $value){
     1548                                $value = (array)$value;
     1549                                if( isset($value['hide_from_web']) ){
     1550                                    $hide_from_web = $value['hide_from_web'];
     1551                                    break;
    15391552                                }
    15401553                            }
    15411554
    15421555                            // Delete producten met de optie "Hide form web"
    1543                             if( $remove_from_web || isset($product['active']) && $product['active'] == '0' ){
    1544                                 $post_id = $this->functions->custom_get_product_id_by_sku($product_change['id'], $product_change['sku']);
     1556                            if( $hide_from_web || ( isset($product['active']) && $product['active'] == '0' ) ){
     1557                                $post_id = $this->functions->custom_get_product_id_by_sku($product['id'], $product['sku']);
    15451558                                if (isset($post_id) && !empty($post_id)) {
    15461559                                    $this->logging->log_file_write('GetProductRemoved | Product removed from webshop ' . $post_id);
     
    16421655            }
    16431656
     1657            update_option('last_processed_changes_time', $new_processed_changes_time );
    16441658        }
    16451659
  • extendago-wp-connection/trunk/includes/woocommerce/class-extendago-woocommerce-functions.php

    r3176970 r3208968  
    1515        add_action( 'woocommerce_process_product_meta', array($this, 'woocommerce_extendago_product_fields_save') );
    1616
     17        add_filter('bulk_actions-edit-product', array($this, 'add_extenda_bulk_action'), 10, 1);
     18        add_filter('handle_bulk_actions-edit-product', array($this, 'handle_extenda_bulk_action'), 10, 3);
     19        add_action('admin_notices', array($this, 'show_extenda_bulk_succes_message'));
    1720
    1821        add_action( 'woocommerce_order_status_changed', array($this, 'process_cancelled_order_products'), 10, 4 );
     22    }
     23
     24    public function add_extenda_bulk_action($bulk_actions){
     25        $bulk_actions['extenda-export'] = 'Bulk export | Woocommerce --> Extenda GO';
     26        return $bulk_actions;
     27    }
     28
     29    public function handle_extenda_bulk_action($redirect_url, $action, $product_ids){
     30        if ($action == 'extenda-export' && !empty($product_ids) ) {
     31
     32            // Upload directory
     33            $upload = wp_upload_dir();
     34            $upload_dir = $upload['basedir'];
     35            $new_upload_dir = $upload_dir . '/extendago/exports';
     36
     37            $logging = new ExtendaGo_WP_Connection_Logging();
     38            $logging->log_file_write( 'Manual | Manual triggered bulk synchronisation for ' .count($product_ids). ' products.');
     39
     40            $extendago_costs_field = get_option('extendago_costs_field');
     41
     42            foreach( $product_ids as $product_id ){
     43                $product = wc_get_product( $product_id );
     44
     45                $product_data = array();
     46
     47                if( $product->is_taxable() && empty($product->get_tax_class()) ){
     48                    $product_tax_class = 'null';
     49                }
     50                else{
     51                    $product_tax_class = $product->get_tax_class();
     52                }
     53
     54                $vat_rate_id = '';
     55                $Extendago = new Extendago_Web_Api();
     56                $VatRates = $Extendago->CurlRequest("/vat_rates", "GET");
     57                foreach ($VatRates as $VatRate){
     58                    $value = get_option($VatRate['id']);
     59                    if( $value == $product_tax_class ){
     60                        $vat_rate_id = $VatRate['id'];
     61                        break;
     62                    }
     63                }
     64
     65                // Get Product General Info
     66                $product_data['id'] = $product->get_id();
     67                $product_data['title'] = $product->get_name();
     68                $product_data['type'] = $product->get_type();
     69                $product_data['slug'] = $product->get_slug();
     70                $product_data['description'] = $product->get_description();
     71                $product_data['short_description'] = $product->get_short_description();
     72                $product_data['sku'] = $product->get_sku();
     73                $product_data['vat_rate_id'] = $vat_rate_id;
     74
     75                $woocommerce_prices_include_tax = get_option('woocommerce_prices_include_tax');
     76                $woocommerce_calc_taxes = get_option('woocommerce_calc_taxes');
     77                if( $woocommerce_prices_include_tax == 'no' && $woocommerce_calc_taxes == 'yes' ){
     78                    $default_tax = '0';
     79                    $default_tax_rates = WC_Tax::get_rates_for_tax_class( $product_tax_class );
     80                    foreach( $default_tax_rates as $default_tax_rate ){
     81                        if( $default_tax_rate->tax_rate > $default_tax ){
     82                            $default_tax = (int)$default_tax_rate->tax_rate;
     83                        }
     84                    }
     85
     86                    // Get Product Prices
     87                    $product_data['price'] = ( $product->get_price() / 100 ) * ( $default_tax + 100 );
     88
     89                    $product_regular_price = $product->get_regular_price();
     90                    if( isset($product_regular_price) && !empty($product_regular_price) ){
     91                        $product_data['regular_price'] = ( $product_regular_price / 100 ) * ( $default_tax + 100 );
     92                    }
     93                    else{
     94                        $product_data['regular_price'] = $product_data['price'];
     95                    }
     96
     97                    $product_sale_price = $product->get_sale_price();
     98                    if( isset($product_sale_price) && !empty($product_sale_price) ){
     99                        $product_data['sale_price'] = ( $product_sale_price / 100 ) * ( $default_tax + 100 );
     100                    }
     101                    else {
     102                        $product_data['sale_price'] = '';
     103                    }
     104                }
     105                else{
     106                    // Get Product Prices
     107                    $product_data['price'] = $product->get_price();
     108                    $product_data['regular_price'] = $product->get_regular_price();
     109                    $product_data['sale_price'] = $product->get_sale_price();
     110                }
     111
     112                $product_attributes = $product->get_attributes();
     113                if( isset($product_attributes) && !empty($product_attributes) ){
     114                    foreach( $product_attributes as $product_attribute_name => $product_attribute ){
     115                        $product_data['attributes'][$product_attribute_name] = $product->get_attribute( $product_attribute_name );
     116                    }
     117                }
     118
     119                if( $product_data['type'] == 'variable' ){
     120                    $product_data['variations'] = array();
     121                    $product_variations = $product->get_children();
     122                    if( isset($product_variations) && !empty($product_variations) ){
     123                        foreach( $product_variations as $product_variation_id ){
     124                            $variation_meta = get_post_meta($product_variation_id);
     125                            $product_variant_name = array();
     126                            foreach ($product_data['attributes'] as $attribute => $attribute_value) {
     127                                if (isset($variation_meta['attribute_' . $attribute])) {
     128                                    $term_slug = $variation_meta['attribute_' . $attribute][0];
     129                                    $term_object = get_term_by('slug', $term_slug, $attribute);
     130                                    $product_variant_name[] = $term_object->name;
     131                                }
     132                            }
     133
     134                            if( isset($product_variant_name) ) {
     135
     136                                $product_variant_name = implode('_', $product_variant_name);
     137
     138                                // Get Product Prices
     139                                $variation_price = $variation_meta['_regular_price'][0];
     140                                $variation_regular_price = $variation_meta['_regular_price'][0];
     141                                $variation_sale_price = $variation_meta['_sale_price'][0];
     142
     143                                // Check for prices exclude vat
     144                                if( $woocommerce_prices_include_tax == 'no' && $woocommerce_calc_taxes == 'yes' ){
     145
     146                                    $variation_price = ( $variation_price / 100 ) * ( $default_tax + 100 );
     147
     148                                    if( isset($variation_regular_price) && !empty($variation_regular_price) ){
     149                                        $variation_regular_price = ( $variation_regular_price / 100 ) * ( $default_tax + 100 );
     150                                    }
     151
     152                                    if( isset($variation_sale_price) && !empty($variation_sale_price) ){
     153                                        $variation_sale_price = ( $variation_sale_price / 100 ) * ( $default_tax + 100 );
     154                                    }
     155                                }
     156
     157                                $variant_data = array(
     158                                    'id' => $product_variation_id,
     159                                    'product_variant_name' => $product_variant_name,
     160                                    'stock' => $variation_meta['_stock'][0],
     161                                    'sku' => $variation_meta['_sku'][0],
     162                                    'regular_price' => $variation_regular_price,
     163                                    'retail_price' => $variation_price,
     164                                    'cost_price' => 0,
     165                                    'sale_price' => $variation_sale_price,
     166                                    'vat_rate_id' => $vat_rate_id,
     167                                );
     168
     169                                // Costs of goods
     170                                if ( isset($extendago_costs_field) && !empty($extendago_costs_field) ) {
     171                                    $cost_price = get_post_meta($product_variation_id, $extendago_costs_field, true);
     172
     173                                    $cost_price = str_replace(',', '.', $cost_price);
     174                                    $cost_price = preg_replace("/[^0-9\.]/", "", $cost_price);
     175                                    $cost_price = str_replace('.', '',substr($cost_price, 0, -3)) . substr($cost_price, -3);
     176
     177                                    if( !empty($cost_price) ) {
     178                                        $variant_data['cost_price'] = number_format(((float)$cost_price * 100), 0, '.', ''); // price in cents
     179                                    }
     180                                }
     181
     182                                $product_data['variations'][] = $variant_data;
     183
     184                            }
     185                        }
     186                    }
     187
     188                }
     189                else{
     190                    $product_data['quantity'] = $product->get_stock_quantity();
     191                    $product_data['status'] = $product->get_stock_status();
     192                }
     193
     194                // Costs of goods
     195                if ( isset($extendago_costs_field) && !empty($extendago_costs_field) ) {
     196                    $cost_price = get_post_meta($product->get_id(), $extendago_costs_field, true);
     197
     198                    $cost_price = str_replace(',', '.', $cost_price);
     199                    $cost_price = preg_replace("/[^0-9\.]/", "", $cost_price);
     200                    $cost_price = str_replace('.', '',substr($cost_price, 0, -3)) . substr($cost_price, -3);
     201
     202                    if( !empty($cost_price) ) {
     203                        $product_data['cost_price'] = number_format(((float)$cost_price * 100), 0, '.', ''); // price in cents
     204                    }
     205                }
     206
     207                $product_categories = wp_get_object_terms($product_data['id'], 'product_cat');
     208                if( isset($product_categories) && !empty($product_categories) ){
     209                    foreach( $product_categories as $product_category ){
     210                        $product_data['categories'][$product_category->term_id] = array(
     211                            'name' => $product_category->name,
     212                            'slug' => $product_category->slug,
     213                            'description' => $product_category->description,
     214                            'parent' => $product_category->parent,
     215                        );
     216                    }
     217                }
     218
     219                // Get Product Images
     220                $thumbnail_id = $product->get_image_id();
     221                if( isset($thumbnail_id) && !empty($thumbnail_id) ){
     222                    $product_data['image'] = get_attached_file($thumbnail_id, true);
     223                }
     224                $gallery_image_ids = $product->get_gallery_image_ids();
     225                if( isset($gallery_image_ids) && !empty($gallery_image_ids) ){
     226                    foreach( $gallery_image_ids as $gallery_image_id ){
     227                        $product_data['gallery'][] = get_attached_file($gallery_image_id, true);
     228                    }
     229                }
     230                $products[] = $product_data;
     231            }
     232
     233            $batch = 0;
     234            $i = 0;
     235            $total_products = count($products);
     236            $products_array = array();
     237            foreach( $products as $product ) {
     238
     239                $products_array[ $product['id'] ] = $product;
     240
     241                // 50 producten per batch
     242                if ( ( ( $i + 1 ) % 50 == 0 ) || $i + 1 == $total_products ) {
     243
     244                    // Save and encode array to json
     245                    $products_array = json_encode($products_array);
     246
     247                    $success = file_put_contents($new_upload_dir . '/products_batch_' . $batch . '.json', $products_array);
     248                    if( $success === FALSE ){
     249                        $logging->log_file_write( 'ERROR | Product batch ' .$batch. 'not created!' );
     250                    }
     251
     252                    $products_array = array();
     253                    $batch++;
     254                }
     255
     256                $i++;
     257            }
     258
     259            $redirect_url = add_query_arg('extenda-export-started', count($product_ids), $redirect_url);
     260        }
     261        return $redirect_url;
     262    }
     263
     264    public function show_extenda_bulk_succes_message(){
     265        if (!empty($_REQUEST['extenda-export-started'])) {
     266            $num_changed = (int) $_REQUEST['extenda-export-started'];
     267            printf('<div id="message" class="updated notice is-dismissable"><p>Bulk synchronisation started for %d products.</p></div>', $num_changed);
     268        }
    19269    }
    20270
  • extendago-wp-connection/trunk/readme.txt

    r3176970 r3208968  
    44Tags: extendago, extenda, woocommerce, arture, POS,
    55Requires at least: 6.0
    6 Tested up to: 6.6.2
     6Tested up to: 6.7.1
    77Stable tag: 1.5.6
    88Requires PHP: 7.4
     
    3131== Changelog ==
    3232
     33= 1.5.7 =
     34* HPOS compatibility
     35* Fallback with Product Query
     36* Bulk export action
     37
    3338= 1.5.6 =
    3439* New feature for Extenda product filters as Woocommerce attributes
Note: See TracChangeset for help on using the changeset viewer.