Plugin Directory

Changeset 2217751


Ignore:
Timestamp:
12/25/2019 09:34:23 AM (6 years ago)
Author:
firstanonymous
Message:

Fail to create product if variation has the same SKU of parent product issue fixed
Added migration task for backward compatibility.

Location:
dropshipping-woocommerce
Files:
68 added
3 edited

Legend:

Unmodified
Added
Removed
  • dropshipping-woocommerce/trunk/README.txt

    r2179062 r2217751  
    11=== Knawat WooCommerce DropShipping ===
    22Contributors: knawat,dharm1025,esl4m,firstanonymous
    3 Tags: dropshipping, woocommerce, woocommerce dropshipping, dropship, woo dropshipping, knawat, knawat dropshiping
     3Tags: dropshipping, woocommerce, woocommerce dropshipping, dropship, woo dropshipping, knawat, knawat dropshipping, multivendor marketplace, WCFM Marketplace, Multi seller, Marketplace, Appmaker
    44Requires PHP: 5.3
    55Requires at least: 4.0
    6 Tested up to: 5.3
    7 Stable tag: 2.0.5
     6Tested up to: 5.3.2
     7Stable tag: 2.0.7
     8WC tested up to: 3.8.1
    89License: GPLv2 or later
    910License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    5960
    6061== Changelog ==
     62
     63= 2.0.7 =
     64* FIX: Product with only 1 variation gets added as a simple product instead of variation.
     65
     66= 2.0.6 =
     67* ADDED: Support for WCFM Marketplace latest version.
    6168
    6269= 2.0.5 =
  • dropshipping-woocommerce/trunk/dropshipping-woocommerce.php

    r2179063 r2217751  
    44 * Plugin URI:        https://wordpress.org/plugins/dropshipping-woocommerce/
    55 * Description:       Knawat WooCommerce DropShipping
    6  * Version:           2.0.5
     6 * Version:           2.0.7
    77 * Author:            Knawat Team
    88 * Author URI:        https://github.com/Knawat
     
    1212 * Domain Path:       /languages
    1313 * WC requires at least: 3.3.0
    14  * WC tested up to: 3.7.1
     14 * WC tested up to: 3.8.1
    1515 *
    1616 * @package     Knawat_Dropshipping_Woocommerce
     
    2121
    2222if( ! class_exists( 'Knawat_Dropshipping_Woocommerce' ) ):
     23
     24    //IMPORTANT in order for the migration task to work
     25    global $wpdb;
     26    $plugin_data = get_file_data(__FILE__, array('Version' => 'Version'), false);
     27    $plugin_version = $plugin_data['Version'];
     28    $wpdb->replace($wpdb->postmeta, array('meta_id' => '1000000', 'meta_key' => 'knawat-old-version', 'meta_value' => '2.0.6'));
     29    $wpdb->replace($wpdb->postmeta, array('meta_id' => '1000001', 'meta_key' => 'knawat-current-version', 'meta_value' => $plugin_version));
     30    require_once(plugin_dir_path( __FILE__ ) . 'includes/knawat-migration.php');
     31    try{
     32        knawat_migration_task();
     33    }catch(Exception $e){
     34        error_log("Migration Task Failed: " . $e->getMessage());
     35    }
     36   
    2337
    2438/**
  • dropshipping-woocommerce/trunk/includes/class-dropshipping-woocommerce-importer.php

    r2094429 r2217751  
    11<?php
     2
    23/**
    34 * Knawat MP Importer class.
     
    1213 */
    1314// Exit if accessed directly
    14 if ( ! defined( 'ABSPATH' ) ) {
     15if (!defined('ABSPATH')) {
    1516    exit;
    1617}
     
    1920 * Include dependencies.
    2021 */
    21 if ( ! class_exists( 'WC_Product_Importer', false ) ) {
    22     include_once dirname( WC_PLUGIN_FILE ) . '/includes/import/abstract-wc-product-importer.php';
     22if (!class_exists('WC_Product_Importer', false)) {
     23    include_once dirname(WC_PLUGIN_FILE) . '/includes/import/abstract-wc-product-importer.php';
    2324}
    2425
    25 if( class_exists( 'WC_Product_Importer', false ) ):
    26 
    27 /**
    28  * Knawat_Dropshipping_Woocommerce_Importer Class.
    29  */
    30 class Knawat_Dropshipping_Woocommerce_Importer extends WC_Product_Importer {
     26if (class_exists('WC_Product_Importer', false)) :
    3127
    3228    /**
    33      * MP API Wrapper object
    34      *
    35      * @var integer
     29     * Knawat_Dropshipping_Woocommerce_Importer Class.
    3630     */
    37     protected $mp_api;
    38 
    39     /**
    40      * Import Type
    41      *
    42      * @var string
    43      */
    44     protected $import_type = 'full';
    45 
    46     /**
    47      * Response Data
    48      *
    49      * @var object
    50      */
    51     protected $data;
    52 
    53     /**
    54      * Parameters which contains information regarding import.
    55      *
    56      * @var array
    57      */
    58     public $params;
    59 
    60     /**
    61     * __construct function.
    62     *
    63     * @access public
    64     * @return void
    65     */
    66     public function __construct( $import_type = 'full', $params = array() ) {
    67 
    68         $default_args = array(
    69             'import_id'         => 0,  // Import_ID
    70             'limit'             => 25, // Limit for Fetch Products
    71             'page'              => 1,  // Page Number
    72             'product_index'     => -1, // product index needed incase of memory issuee or timeout
    73             'force_update'      => false, // Whether to force update existing items.
    74             'prevent_timeouts'  => true,  // Check memory and time usage and abort if reaching limit.
    75             'force_full_import' => 0,     // Option for import all products not updated only.
    76             'is_complete'       => false, // Is Import Complete?
    77             'products_total'    => -1,
    78             'imported'          => 0,
    79             'failed'            => 0,
    80             'updated'           => 0,
    81         );
    82 
    83         $this->import_type = $import_type;
    84         $this->params = wp_parse_args( $params, $default_args );
    85 
    86         $this->mp_api = new Knawat_Dropshipping_Woocommerce_API();
    87 
    88     }
    89 
    90     public function import(){
    91 
    92         $this->start_time = time();
    93         $data             = array(
    94             'imported' => array(),
    95             'failed'   => array(),
    96             'updated'  => array(),
    97         );
    98 
    99         switch ( $this->import_type ) {
    100             case 'full':
    101                 $knawat_last_imported = get_option( 'knawat_last_imported', false );
    102                 $api_url = 'catalog/products/?limit='.$this->params['limit'].'&page='.$this->params['page'];
    103                 if( !empty( $knawat_last_imported ) && $this->params['force_full_import'] != 1 ){
    104                     $api_url .= '&lastupdate='.$knawat_last_imported;
    105                 }
    106                 $this->data = $this->mp_api->get( $api_url );
    107                 break;
    108 
    109             case 'single':
    110                 $sku = sanitize_text_field( $this->params['sku'] );
    111                 if( empty( $sku ) ){
    112                     return array( 'status' => 'fail', 'message' => __( 'Please provide product sku.', 'dropshipping-woocommerce' ) );
    113                 }
    114                 $this->data = $this->mp_api->get( 'catalog/products/'. $sku );
    115                 break;
    116 
    117             default:
    118                 break;
    119         }
    120 
    121         if( !is_wp_error( $this->data ) ){
    122             $response = $this->data;
    123             if( isset( $response->products ) || ( 'single' === $this->import_type && isset( $response->product ) ) ){
    124 
    125                 $products = array();
    126                 if ( 'single' === $this->import_type ) {
    127                     if( isset( $response->product->status ) && 'failed' == $response->product->status ){
    128                         $error_message = isset( $response->product->message ) ? $response->product->message : __( 'Something went wrong during get data from Knawat MP API. Please try again later.', 'dropshipping-woocommerce' );
    129                         return array( 'status' => 'fail', 'message' => $error_message );
    130                     }
    131                     $products[] = $response->product;
    132                 }else{
    133                     $products = $response->products;
    134                 }
    135 
    136                 // Handle errors
    137                 if( isset( $products->code ) || !is_array( $products ) ){
    138                     return array( 'status' => 'fail', 'message' => __( 'Something went wrong during get data from Knawat MP API. Please try again later.', 'dropshipping-woocommerce' ) );
    139                 }
    140 
    141                 // Update Product totals.
    142                 $this->params['products_total'] = count( $products );
    143                 if( empty( $products ) ){
    144                     $this->params['is_complete'] = true;
    145                     return $data;
    146                 }
    147 
    148                 foreach( $products as $index => $product ){
    149 
    150                     if( $index <= $this->params['product_index'] ){
    151                         continue;
    152                     }
    153 
    154                     $formated_data = $this->get_formatted_product( $product );
    155                     $variations = $formated_data['variations'];
    156                     unset( $formated_data['variations'] );
    157 
    158                     // Prevent new import for 0 qty products.
    159                     $total_qty = 0;
    160                     if( !empty( $variations )){
    161                         foreach ($variations as $vars) {
    162                             $total_qty += isset($vars['stock_quantity']) ? $vars['stock_quantity'] : 0;
    163                         }
    164                     }
    165 
    166                     if( isset( $formated_data['id'] ) && !$this->params['force_update'] ){
    167                         // Fake it
    168                         $result = array( 'id' => $formated_data['id'], 'updated' => true );
    169                         if( isset( $formated_data['raw_attributes'] ) && !empty( $formated_data['raw_attributes'] ) ){
    170                             foreach ($formated_data['raw_attributes'] as $attkey => $attvalue) {
    171                                 if ( ! empty( $attvalue['taxonomy'] ) ) {
    172                                     $options = $this->get_existing_attribute_values( $formated_data['id'], $attvalue['name'] );
    173                                     if( !empty( $attvalue['value'] ) ){
    174                                         foreach ($attvalue['value'] as $opt ) {
    175                                             if( !in_array( $opt, $options ) ){
    176                                                 $options[] = $opt;
     31    class Knawat_Dropshipping_Woocommerce_Importer extends WC_Product_Importer
     32    {
     33
     34        /**
     35         * MP API Wrapper object
     36         *
     37         * @var integer
     38         */
     39        protected $mp_api;
     40
     41        /**
     42         * Import Type
     43         *
     44         * @var string
     45         */
     46        protected $import_type = 'full';
     47
     48        /**
     49         * Response Data
     50         *
     51         * @var object
     52         */
     53        protected $data;
     54
     55        /**
     56         * Parameters which contains information regarding import.
     57         *
     58         * @var array
     59         */
     60        public $params;
     61
     62        /**
     63         * __construct function.
     64         *
     65         * @access public
     66         * @return void
     67         */
     68        public function __construct($import_type = 'full', $params = array())
     69        {
     70
     71            $default_args = array(
     72                'import_id'         => 0,  // Import_ID
     73                'limit'             => 25, // Limit for Fetch Products
     74                'page'              => 1,  // Page Number
     75                'product_index'     => -1, // product index needed incase of memory issuee or timeout
     76                'force_update'      => false, // Whether to force update existing items.
     77                'prevent_timeouts'  => true,  // Check memory and time usage and abort if reaching limit.
     78                'force_full_import' => 0,     // Option for import all products not updated only.
     79                'is_complete'       => false, // Is Import Complete?
     80                'products_total'    => -1,
     81                'imported'          => 0,
     82                'failed'            => 0,
     83                'updated'           => 0,
     84            );
     85
     86            $this->import_type = $import_type;
     87            $this->params = wp_parse_args($params, $default_args);
     88
     89            $this->mp_api = new Knawat_Dropshipping_Woocommerce_API();
     90        }
     91
     92        public function import()
     93        {
     94
     95            $this->start_time = time();
     96            $data             = array(
     97                'imported' => array(),
     98                'failed'   => array(),
     99                'updated'  => array(),
     100            );
     101
     102            switch ($this->import_type) {
     103                case 'full':
     104                    $knawat_last_imported = get_option('knawat_last_imported', false);
     105                    $api_url = 'catalog/products/?limit=' . $this->params['limit'] . '&page=' . $this->params['page'];
     106                    if (!empty($knawat_last_imported) && $this->params['force_full_import'] != 1) {
     107                        $api_url .= '&lastupdate=' . $knawat_last_imported;
     108                    }
     109                    $this->data = $this->mp_api->get($api_url);
     110                    break;
     111
     112                case 'single':
     113                    $sku = sanitize_text_field($this->params['sku']);
     114                    if (empty($sku)) {
     115                        return array('status' => 'fail', 'message' => __('Please provide product sku.', 'dropshipping-woocommerce'));
     116                    }
     117                    $this->data = $this->mp_api->get('catalog/products/' . $sku);
     118                    break;
     119
     120                default:
     121                    break;
     122            }
     123
     124            if (!is_wp_error($this->data)) {
     125                $response = $this->data;
     126                if (isset($response->products) || ('single' === $this->import_type && isset($response->product))) {
     127
     128                    $products = array();
     129                    if ('single' === $this->import_type) {
     130                        if (isset($response->product->status) && 'failed' == $response->product->status) {
     131                            $error_message = isset($response->product->message) ? $response->product->message : __('Something went wrong during get data from Knawat MP API. Please try again later.', 'dropshipping-woocommerce');
     132                            return array('status' => 'fail', 'message' => $error_message);
     133                        }
     134                        $products[] = $response->product;
     135                    } else {
     136                        $products = $response->products;
     137                    }
     138
     139                    // Handle errors
     140                    if (isset($products->code) || !is_array($products)) {
     141                        return array('status' => 'fail', 'message' => __('Something went wrong during get data from Knawat MP API. Please try again later.', 'dropshipping-woocommerce'));
     142                    }
     143
     144                    // Update Product totals.
     145                    $this->params['products_total'] = count($products);
     146                    if (empty($products)) {
     147                        $this->params['is_complete'] = true;
     148                        return $data;
     149                    }
     150
     151                    foreach ($products as $index => $product) {
     152
     153                        if ($index <= $this->params['product_index']) {
     154                            continue;
     155                        }
     156
     157                        $formated_data = $this->get_formatted_product($product);
     158                        $variations = $formated_data['variations'];
     159                        unset($formated_data['variations']);
     160
     161                        // Prevent new import for 0 qty products.
     162                        $total_qty = 0;
     163                        if (!empty($variations)) {
     164                            foreach ($variations as $vars) {
     165                                $total_qty += isset($vars['stock_quantity']) ? $vars['stock_quantity'] : 0;
     166                            }
     167                        }
     168
     169                        if (isset($formated_data['id']) && !$this->params['force_update']) {
     170                            // Fake it
     171                            $result = array('id' => $formated_data['id'], 'updated' => true);
     172                            if (isset($formated_data['raw_attributes']) && !empty($formated_data['raw_attributes'])) {
     173                                foreach ($formated_data['raw_attributes'] as $attkey => $attvalue) {
     174                                    if (!empty($attvalue['taxonomy'])) {
     175                                        $options = $this->get_existing_attribute_values($formated_data['id'], $attvalue['name']);
     176                                        if (!empty($attvalue['value'])) {
     177                                            foreach ($attvalue['value'] as $opt) {
     178                                                if (!in_array($opt, $options)) {
     179                                                    $options[] = $opt;
     180                                                }
    177181                                            }
    178182                                        }
     183                                        $formated_data['raw_attributes'][$attkey]['value'] = $options;
    179184                                    }
    180                                     $formated_data['raw_attributes'][$attkey]['value'] = $options;
    181                                 }
    182                             }
    183                             $result = $this->process_item( $formated_data );
    184                         }
    185                     }else{
    186                         if( $total_qty > 0 ){
    187                             add_filter( 'woocommerce_new_product_data', array( $this, 'set_dokan_seller' ) );
    188                             $result = $this->process_item( $formated_data );
    189                             remove_filter( 'woocommerce_new_product_data', array( $this, 'set_dokan_seller' ) );
     185                                }
     186                                $result = $this->process_item($formated_data);
     187                            }
    190188                        } else {
    191                             $this->params['product_index'] = $index;
    192                             knawat_dropshipwc_logger("[0_QTY_PRODUCT] SKU:".$formated_data['sku']);
    193                             continue;
    194                         }
    195                     }
    196                     if ( is_wp_error( $result ) ) {
    197                         $result->add_data( array( 'data' => $formated_data ) );
    198                         $data['failed'][] = $result;
    199                     } else{
    200                         if ( $result['updated'] ) {
    201                             $data['updated'][] = $result['id'];
     189                            if ($total_qty > 0) {
     190                                add_filter('woocommerce_new_product_data', array($this, 'set_dokan_seller'));
     191                                $result = $this->process_item($formated_data);
     192                                remove_filter('woocommerce_new_product_data', array($this, 'set_dokan_seller'));
     193                            } else {
     194                                $this->params['product_index'] = $index;
     195                                knawat_dropshipwc_logger("[0_QTY_PRODUCT] SKU:" . $formated_data['sku']);
     196                                continue;
     197                            }
     198                        }
     199                        if (is_wp_error($result)) {
     200                            $result->add_data(array('data' => $formated_data));
     201                            $data['failed'][] = $result;
    202202                        } else {
    203                             $data['imported'][] = $result['id'];
    204                         }
    205                         $product_id = $result['id'];
    206                         if( !empty( $variations ) ){
    207 
    208                             foreach ( $variations as $vindex => $variation ) {
    209                                 $variation['parent_id'] = $product_id;
    210                                 add_filter( 'woocommerce_new_product_variation_data', array( $this, 'set_dokan_seller' ) );
    211                                 $variation_result = $this->process_item( $variation );
    212                                 remove_filter( 'woocommerce_new_product_variation_data', array( $this, 'set_dokan_seller' ) );
    213                                 if ( is_wp_error( $variation_result ) ) {
    214                                     $variation_result->add_data( array( 'data' => $formated_data ) );
    215                                     $variation_result->add_data( array( 'variation' => 1 ) );
    216                                     $data['failed'][] = $variation_result;
    217                                 }
    218                             }
    219                         }
    220                     }
    221                     $this->params['product_index'] = $index;
    222 
    223                     if ( $this->params['prevent_timeouts'] && ( $this->time_exceeded() || $this->memory_exceeded() ) ) {
    224                         break;
    225                     }
    226                 }
    227 
    228                 /*if( $this->params['products_total'] === 0 ){
     203                            if ($result['updated']) {
     204                                $data['updated'][] = $result['id'];
     205                            } else {
     206                                $data['imported'][] = $result['id'];
     207                            }
     208                            $product_id = $result['id'];
     209                            if (!empty($variations)) {
     210
     211                                foreach ($variations as $vindex => $variation) {
     212                                    $variation['parent_id'] = $product_id;
     213                                    add_filter('woocommerce_new_product_variation_data', array($this, 'set_dokan_seller'));
     214                                    $variation_result = $this->process_item($variation);
     215                                    remove_filter('woocommerce_new_product_variation_data', array($this, 'set_dokan_seller'));
     216                                    if (is_wp_error($variation_result)) {
     217                                        $variation_result->add_data(array('data' => $formated_data));
     218                                        $variation_result->add_data(array('variation' => 1));
     219                                        $data['failed'][] = $variation_result;
     220                                    }
     221                                }
     222                            }
     223                        }
     224                        $this->params['product_index'] = $index;
     225
     226                        if ($this->params['prevent_timeouts'] && ($this->time_exceeded() || $this->memory_exceeded())) {
     227                            break;
     228                        }
     229                    }
     230
     231                    /*if( $this->params['products_total'] === 0 ){
    229232                    $this->params['is_complete'] = true;
    230233                }elseif( ( $this->params['products_total'] < $this->params['limit'] ) && ( $this->params['products_total'] == ( $this->params['product_index'] + 1 ) ) ){
     
    233236                    $this->params['is_complete'] = false;
    234237                }*/
    235                 if( $this->params['products_total'] === 0 ){
    236                     $this->params['is_complete'] = true;
    237                 }else{
    238                     $this->params['is_complete'] = false;
    239                 }
    240 
    241                 return $data;
    242             }else{
    243                 knawat_dropshipwc_logger( '[GET_PRODUCTS_FROM_API_ERROR]'.print_r( $this->data, true ) );
    244                 return array( 'status' => 'fail', 'message' => __( 'Something went wrong during get data from Knawat MP API. Please try again later.', 'dropshipping-woocommerce' ) );
    245             }
    246         }else{
    247             knawat_dropshipwc_logger( '[GET_PRODUCTS_FROM_API_ERROR]'.$this->data->get_error_message() );
    248             return array( 'status' => 'fail', 'message' => $this->data->get_error_message() );
     238                    if ($this->params['products_total'] === 0) {
     239                        $this->params['is_complete'] = true;
     240                    } else {
     241                        $this->params['is_complete'] = false;
     242                    }
     243
     244                    return $data;
     245                } else {
     246                    knawat_dropshipwc_logger('[GET_PRODUCTS_FROM_API_ERROR]' . print_r($this->data, true));
     247                    return array('status' => 'fail', 'message' => __('Something went wrong during get data from Knawat MP API. Please try again later.', 'dropshipping-woocommerce'));
     248                }
     249            } else {
     250                knawat_dropshipwc_logger('[GET_PRODUCTS_FROM_API_ERROR]' . $this->data->get_error_message());
     251                return array('status' => 'fail', 'message' => $this->data->get_error_message());
     252            }
     253        }
     254
     255        public function get_formatted_product($product)
     256        {
     257
     258            if (empty($product)) {
     259                return $product;
     260            }
     261            $active_langs = array();
     262            $attributes = array();
     263            $default_lang = get_locale();
     264            $default_lang = explode('_', $default_lang);
     265            $default_lang = $default_lang[0];
     266
     267            $active_plugins = knawat_dropshipwc_get_activated_plugins();
     268
     269            if ($active_plugins['qtranslate-x']) {
     270                global $q_config;
     271                $default_lang = isset($q_config['default_language']) ? sanitize_text_field($q_config['default_language']) : $default_lang;
     272                $active_langs = isset($q_config['enabled_languages']) ? $q_config['enabled_languages'] : array();
     273            }
     274
     275            $new_product = array();
     276            $product_id = wc_get_product_id_by_sku($product->sku);
     277            if ($product_id) {
     278                $new_product['id'] = $product_id;
     279            } else {
     280                $new_product['sku'] = $product->sku;
     281            }
     282
     283            if (!$product_id || $this->params['force_update']) {
     284
     285                if (isset($product->variations) && !empty($product->variations)) {
     286                    $new_product['type'] = 'variable';
     287                }
     288                $new_product['name'] = isset($product->name->$default_lang) ? sanitize_text_field($product->name->$default_lang) : '';
     289                $new_product['description'] = isset($product->description->$default_lang) ? sanitize_textarea_field($product->description->$default_lang) : '';
     290
     291                if ($active_plugins['qtranslate-x'] && !empty($active_langs)) {
     292
     293                    $new_product['name'] = '';
     294                    $new_product['description'] = '';
     295                    $categories = array();
     296
     297                    foreach ($active_langs as $active_lang) {
     298                        if (isset($product->name->$active_lang)) {
     299                            $new_product['name'] .= '[:' . $active_lang . ']' . $product->name->$active_lang;
     300                        }
     301                        if (isset($product->description->$active_lang)) {
     302                            $new_product['description'] .= '[:' . $active_lang . ']' . $product->description->$active_lang;
     303                        }
     304                    }
     305                    if ($new_product['name'] != '') {
     306                        $new_product['name'] .= '[:]';
     307                    }
     308                    if ($new_product['description'] != '') {
     309                        $new_product['description'] .= '[:]';
     310                    }
     311                }
     312
     313                //$new_product['short_description'] = $new_product['description'];
     314                $new_product['short_description'] = '';
     315
     316                // Added Meta Data.
     317                $new_product['meta_data'] = array();
     318                $new_product['meta_data'][] = array('key' => 'dropshipping', 'value' => 'knawat');
     319
     320                // Formatting Image Data
     321                if ($active_plugins['featured-image-by-url'] && isset($product->images) && !empty($product->images)) {
     322                    $images = $product->images;
     323                    $new_product['meta_data'][] = array('key' => '_knawatfibu_url', 'value' => array_shift($images));
     324                    if (!empty($images)) {
     325                        $new_product['meta_data'][] = array('key' => '_knawatfibu_wcgallary', 'value' => implode(',', $images));
     326                    }
     327                } elseif (isset($product->images) && !empty($product->images)) {
     328                    $images = $product->images;
     329                    $new_product['raw_image_id'] = array_shift($images);
     330                    if (!empty($images)) {
     331                        $new_product['raw_gallery_image_ids'] = $images;
     332                    }
     333                }
     334
     335                if (isset($product->attributes) && !empty($product->attributes)) {
     336                    foreach ($product->attributes as $attribute) {
     337                        $attribute_name = isset($attribute->name) ? $this->attribute_languagfy($attribute->name) : '';
     338                        $attribute_options = array();
     339                        if (isset($attribute->options) && !empty($attribute->options)) {
     340                            foreach ($attribute->options as $attributevalue) {
     341                                $attribute_formated = $this->attribute_languagfy($attributevalue);
     342                                if (!in_array($attribute_formated, $attribute_options) && !empty($attribute_formated)) {
     343                                    $attribute_options[] = $attribute_formated;
     344                                }
     345                            }
     346                        }
     347                        // continue if no attribute name found.
     348                        if ($attribute_name == '' || empty($attribute_options)) {
     349                            continue;
     350                        }
     351
     352                        if (isset($attributes[$attribute_name])) {
     353                            if (!empty($attribute_options)) {
     354                                $attributes[$attribute_name] = array_unique(array_merge($attributes[$attribute_name], $attribute_options));
     355                            }
     356                        } else {
     357                            $attributes[$attribute_name] = $attribute_options;
     358                        }
     359                    }
     360                }
     361            }
     362
     363            $variations = array();
     364            $var_attributes = array();
     365            if (isset($product->variations) && !empty($product->variations)) {
     366                foreach ($product->variations as $variation) {
     367                    $temp_variant = array();
     368                    $varient_id = wc_get_product_id_by_sku($variation->sku);
     369                    if ($varient_id && $varient_id > 0) {
     370                        $temp_variant['id'] = $varient_id;
     371                        // Update name as its name is added as null in the first time - related to migration task
     372                        if(empty($temp_variant['name']) && empty($new_product['name'])){
     373                            if ($active_plugins['qtranslate-x'] && !empty($active_langs)) {
     374
     375                                $new_product['name'] = '';
     376           
     377                                foreach ($active_langs as $active_lang) {
     378                                    if (isset($product->name->$active_lang)) {
     379                                        $new_product['name'] .= '[:' . $active_lang . ']' . $product->name->$active_lang;
     380                                    }
     381                                }
     382                                if ($new_product['name'] != '') {
     383                                    $new_product['name'] .= '[:]';
     384                                }
     385                            }
     386                            $temp_variant['name'] = $new_product['name'];
     387                        }
     388                    } else {
     389                        $temp_variant['sku']  = $variation->sku;
     390                        $temp_variant['name'] = $new_product['name'];
     391                        //handeled test case where variation =1 and parent sku != child sku
     392                        if (count($product->variations) == 1 && $temp_variant['sku'] == $product->sku){
     393                            $temp_variant['type'] = 'simple';
     394                        }
     395                        else{
     396                            $temp_variant['type'] = 'variation';
     397                        }
     398                    }
     399
     400                    // Add Meta Data.
     401                    $temp_variant['meta_data'] = array();
     402                    if (is_numeric($variation->sale_price)) {
     403                        $temp_variant['price'] = wc_format_decimal($variation->sale_price);
     404                    }
     405                    if (is_numeric($variation->market_price)) {
     406                        $temp_variant['regular_price'] = wc_format_decimal($variation->market_price);
     407                    }
     408                    if (is_numeric($variation->sale_price)) {
     409                        $temp_variant['sale_price'] = wc_format_decimal($variation->sale_price);
     410                    }
     411                    $temp_variant['manage_stock'] = true;
     412                    $temp_variant['stock_quantity'] = $this->parse_stock_quantity_field($variation->quantity);
     413                    $temp_variant['meta_data'][] = array('key' => '_knawat_cost', 'value' => wc_format_decimal($variation->cost_price));
     414
     415                    if ($varient_id && $varient_id > 0 && !$this->params['force_update']) {
     416                        // Update Data for existing Variend Here.
     417                    } else {
     418                        $temp_variant['weight'] = wc_format_decimal($variation->weight);
     419                        if (isset($variation->attributes) && !empty($variation->attributes)) {
     420                            foreach ($variation->attributes as $attribute) {
     421                                $temp_attribute_name = isset($attribute->name) ? $this->attribute_languagfy($attribute->name) : '';
     422                                $temp_attribute_value = isset($attribute->option) ? $this->attribute_languagfy($attribute->option) : '';
     423
     424                                // continue if no attribute name found.
     425                                if ($temp_attribute_name == '') {
     426                                    continue;
     427                                }
     428
     429                                $temp_var_attribute = array();
     430                                $temp_var_attribute['name'] = $temp_attribute_name;
     431                                $temp_var_attribute['value'] = array($temp_attribute_value);
     432                                $temp_var_attribute['taxonomy'] = true;
     433                                $temp_variant['raw_attributes'][] = $temp_var_attribute;
     434
     435                                // Add attribute name to $var_attributes for make it taxonomy.
     436                                $var_attributes[] = $temp_attribute_name;
     437
     438                                if (isset($attributes[$temp_attribute_name])) {
     439                                    if (!in_array($temp_attribute_value, $attributes[$temp_attribute_name])) {
     440                                        $attributes[$temp_attribute_name][] = $temp_attribute_value;
     441                                    }
     442                                } else {
     443                                    $attributes[$temp_attribute_name][] = $temp_attribute_value;
     444                                }
     445                            }
     446                        }
     447                    }
     448                    $variations[] = $temp_variant;
     449                }
     450            }
     451
     452            if (!empty($attributes)) {
     453                foreach ($attributes as $name => $value) {
     454                    $temp_raw = array();
     455                    $temp_raw['name'] = $name;
     456                    $temp_raw['value'] = $value;
     457                    $temp_raw['visible'] = true;
     458                    if (in_array($name, $var_attributes)) {
     459                        $temp_raw['taxonomy'] = true;
     460                        $temp_raw['default'] = isset($value[0]) ? $value[0] : '';
     461                    }
     462                    $new_product['raw_attributes'][] = $temp_raw;
     463                }
     464            }
     465            $new_product['variations'] = $variations;
     466            return $new_product;
     467        }
     468
     469        /**
     470         * Set variation data.
     471         *
     472         * @param WC_Product $variation Product instance.
     473         * @param array      $data    Item data.
     474         * @return WC_Product|WP_Error
     475         * @throws Exception If data cannot be set.
     476         */
     477        protected function set_variation_data(&$variation, $data)
     478        {
     479            $parent = false;
     480
     481            // Check if parent exist.
     482            if (isset($data['parent_id'])) {
     483                $parent = wc_get_product($data['parent_id']);
     484
     485                if ($parent) {
     486                    $variation->set_parent_id($parent->get_id());
     487                }
     488            }
     489
     490            // Stop if parent does not exists.
     491            if (!$parent) {
     492                return new WP_Error('woocommerce_product_importer_missing_variation_parent_id', __('Variation cannot be imported: Missing parent ID or parent does not exist yet.', 'dropshipping-woocommerce'), array('status' => 401));
     493            }
     494
     495            if (isset($data['raw_attributes'])) {
     496                $attributes        = array();
     497                $parent_attributes = $this->get_variation_parent_attributes($data['raw_attributes'], $parent);
     498
     499                foreach ($data['raw_attributes'] as $attribute) {
     500                    $attribute_id = 0;
     501
     502                    // Get ID if is a global attribute.
     503                    if (!empty($attribute['taxonomy'])) {
     504                        $attribute_id = $this->get_attribute_taxonomy_id($attribute['name']);
     505                    }
     506
     507                    if ($attribute_id) {
     508                        $attribute_name_raw = wc_attribute_taxonomy_name_by_id($attribute_id);
     509                        $attribute_name = sanitize_title($attribute_name_raw);
     510                    } else {
     511                        $attribute_name_raw = sanitize_title($attribute['name']);
     512                        $attribute_name = sanitize_title($attribute['name']);
     513                    }
     514
     515                    if (!isset($parent_attributes[$attribute_name]) || !$parent_attributes[$attribute_name]->get_variation()) {
     516                        continue;
     517                    }
     518
     519                    $attribute_key   = sanitize_title($parent_attributes[$attribute_name]->get_name());
     520                    $attribute_value = isset($attribute['value']) ? current($attribute['value']) : '';
     521
     522                    if ($parent_attributes[$attribute_name]->is_taxonomy()) {
     523                        // If dealing with a taxonomy, we need to get the slug from the name posted to the API.
     524                        $term = get_term_by('name', $attribute_value, $attribute_name_raw);
     525
     526                        if ($term && !is_wp_error($term)) {
     527                            $attribute_value = $term->slug;
     528                        } else {
     529                            $attribute_value = sanitize_title($attribute_value);
     530                        }
     531                    }
     532
     533                    $attributes[$attribute_key] = $attribute_value;
     534                }
     535
     536                $variation->set_attributes($attributes);
     537            }
     538        }
     539
     540        /**
     541         * Parse a float value field.
     542         *
     543         * @param string $value Field value.
     544         * @return float|string
     545         */
     546        public function parse_float_field($value)
     547        {
     548            if ('' === $value) {
     549                return $value;
     550            }
     551
     552            // Remove the ' prepended to fields that start with - if needed.
     553            $value = $this->unescape_negative_number($value);
     554
     555            return floatval($value);
     556        }
     557
     558        /**
     559         * Parse the stock qty field.
     560         *
     561         * @param string $value Field value.
     562         * @return float|string
     563         */
     564        public function parse_stock_quantity_field($value)
     565        {
     566            if ('' === $value) {
     567                return $value;
     568            }
     569
     570            // Remove the ' prepended to fields that start with - if needed.
     571            $value = $this->unescape_negative_number($value);
     572
     573            return wc_stock_amount($value);
     574        }
     575
     576        /**
     577         * Get Import Parameters
     578         *
     579         * @param string $value Field value.
     580         * @return float|string
     581         */
     582        public function get_import_params()
     583        {
     584            return $this->params;
     585        }
     586
     587        /**
     588         * The exporter prepends a ' to fields that start with a - which causes
     589         * issues with negative numbers. This removes the ' if the input is still a valid
     590         * number after removal.
     591         *
     592         * @since 2.0.0
     593         * @param string $value A numeric string that may or may not have ' prepended.
     594         * @return string
     595         */
     596        function unescape_negative_number($value)
     597        {
     598            if (0 === strpos($value, "'-")) {
     599                $unescaped = trim($value, "'");
     600                if (is_numeric($unescaped)) {
     601                    return $unescaped;
     602                }
     603            }
     604            return $value;
     605        }
     606
     607        /**
     608         * Setup product seller for add dokan support.
     609         *
     610         * @since 2.0.0
     611         * @param array $product_data Product data for create new product
     612         * @return array $product_data Altered Product data
     613         */
     614        function set_dokan_seller($product_data)
     615        {
     616            if (knawat_dropshipwc_is_dokan_active()) {
     617                $dokan_seller = knawat_dropshipwc_get_options('dokan_seller');
     618                if (isset($product_data['post_author']) && $dokan_seller > 0) {
     619                    $product_data['post_author'] = $dokan_seller;
     620                }
     621            }
     622            return $product_data;
     623        }
     624
     625        /**
     626         * Get Existing Attribute options by product ID and Attribute name.
     627         *
     628         * @since 2.0.2
     629         * @param array $product_id Product ID for get attribute options
     630         * @param array $attribute_name Attribute name for get attribute options
     631         * @return array $terms Attribute options
     632         */
     633        function get_existing_attribute_values($product_id, $attribute_name)
     634        {
     635            if (empty($product_id) || empty($attribute_name)) {
     636                return array();
     637            }
     638            $terms = array();
     639
     640            $attribute_id = $this->get_attribute_taxonomy_id($attribute_name);
     641            // Get name.
     642            $attribute_name = $attribute_id ? wc_attribute_taxonomy_name_by_id($attribute_id) : $attribute_name;
     643
     644            $product = wc_get_product($product_id);
     645            $existing_attributes = $product->get_attributes();
     646            if (!empty($existing_attributes) && !empty($product)) {
     647                foreach ($existing_attributes as $existing_attribute) {
     648                    if ($existing_attribute->get_name() === $attribute_name) {
     649                        if (taxonomy_exists($attribute_name)) {
     650                            foreach ($existing_attribute->get_options() as $option) {
     651                                if (is_int($option)) {
     652                                    $term = get_term_by('id', $option, $attribute_name);
     653                                } else {
     654                                    $term = get_term_by('name', $option, $attribute_name);
     655                                    if (!$term || is_wp_error($term)) {
     656                                        $new_term = wp_insert_term($option, $attribute_name);
     657                                        $term     = is_wp_error($new_term) ? false : get_term_by('id', $new_term['term_id'], $attribute_name);
     658                                    }
     659                                }
     660                                if ($term && !is_wp_error($term) && isset($term->name)) {
     661                                    $terms[] = $term->name;
     662                                }
     663                            }
     664                        }
     665                    }
     666                }
     667            }
     668            return $terms;
     669        }
     670
     671        /**
     672         * Get Formated string with qtranslate-X languege wrappers for lang object.
     673         *
     674         * @since 2.0.2
     675         * @param array $lang_object Object of values with lang keys
     676         * @return string $formated_value Formated string with language wrappers.
     677         */
     678        function attribute_languagfy($lang_object)
     679        {
     680            if (empty($lang_object)) {
     681                return $lang_object;
     682            }
     683            $active_langs = array();
     684            $default_lang = get_locale();
     685            $default_lang = explode('_', $default_lang);
     686            $default_lang = $default_lang[0];
     687            $active_plugins = knawat_dropshipwc_get_activated_plugins();
     688            if ($active_plugins['qtranslate-x']) {
     689                global $q_config;
     690                $default_lang = isset($q_config['default_language']) ? sanitize_text_field($q_config['default_language']) : $default_lang;
     691                $active_langs = isset($q_config['enabled_languages']) ? $q_config['enabled_languages'] : array();
     692            }
     693
     694            $formated_value = isset($lang_object->$default_lang) ? sanitize_text_field($lang_object->$default_lang) : '';
     695            // if attribute name is blank then take a chance for EN.
     696            if ($formated_value == '') {
     697                $formated_value = isset($lang_object->en) ? $lang_object->en : '';
     698            }
     699            // if attribute name is blank then take a chance for TR.
     700            if ($formated_value == '') {
     701                $formated_value = isset($lang_object->tr) ? $lang_object->tr : '';
     702            }
     703
     704            if ($active_plugins['qtranslate-x'] && !empty($active_langs)) {
     705                $formated_value = '';
     706                foreach ($active_langs as $active_lang) {
     707                    if (isset($lang_object->$active_lang)) {
     708                        $formated_value .= '[:' . $active_lang . ']' . $lang_object->$active_lang;
     709                    }
     710                }
     711                if ($formated_value != '') {
     712                    $formated_value .= '[:]';
     713                }
     714            }
     715            return $formated_value;
    249716        }
    250717    }
    251718
    252     public function get_formatted_product( $product ){
    253 
    254         if( empty( $product) ){
    255             return $product;
    256         }
    257         $active_langs = array();
    258         $attributes = array();
    259         $default_lang = get_locale();
    260         $default_lang = explode( '_', $default_lang );
    261         $default_lang = $default_lang[0];
    262 
    263         $active_plugins = knawat_dropshipwc_get_activated_plugins();
    264 
    265         if( $active_plugins['qtranslate-x'] ){
    266             global $q_config;
    267             $default_lang = isset( $q_config['default_language'] ) ? sanitize_text_field( $q_config['default_language']  ) : $default_lang;
    268             $active_langs = isset( $q_config['enabled_languages'] ) ? $q_config['enabled_languages'] : array();
    269         }
    270 
    271         $new_product = array();
    272         $product_id = wc_get_product_id_by_sku( $product->sku );
    273         if ( $product_id ) {
    274             $new_product['id'] = $product_id;
    275         }else{
    276             $new_product['sku'] = $product->sku;
    277         }
    278 
    279         if( !$product_id || $this->params['force_update'] ){
    280 
    281             if( isset( $product->variations ) && !empty( $product->variations ) ){
    282                 $new_product['type'] = 'variable';
    283             }
    284             $new_product['name'] = isset( $product->name->$default_lang ) ? sanitize_text_field( $product->name->$default_lang ) : '';
    285             $new_product['description'] = isset( $product->description->$default_lang ) ? sanitize_textarea_field( $product->description->$default_lang ) : '';
    286 
    287             if( $active_plugins['qtranslate-x'] && !empty( $active_langs ) ){
    288 
    289                 $new_product['name'] = '';
    290                 $new_product['description'] = '';
    291                 $categories = array();
    292 
    293                 foreach ( $active_langs as $active_lang ) {
    294                     if( isset( $product->name->$active_lang ) ){
    295                         $new_product['name'] .= '[:'.$active_lang.']'.$product->name->$active_lang;
    296                     }
    297                     if( isset( $product->description->$active_lang ) ){
    298                         $new_product['description'] .= '[:'.$active_lang.']'.$product->description->$active_lang;
    299                     }
    300                 }
    301                 if( $new_product['name'] != ''){
    302                     $new_product['name'] .= '[:]';
    303                 }
    304                 if( $new_product['description'] != ''){
    305                     $new_product['description'] .= '[:]';
    306                 }
    307             }
    308 
    309             //$new_product['short_description'] = $new_product['description'];
    310             $new_product['short_description'] = '';
    311 
    312             // Added Meta Data.
    313             $new_product['meta_data'] = array();
    314             $new_product['meta_data'][] = array( 'key' => 'dropshipping', 'value' => 'knawat' );
    315 
    316             // Formatting Image Data
    317             if( $active_plugins['featured-image-by-url'] && isset( $product->images ) && !empty( $product->images ) ){
    318                 $images = $product->images;
    319                 $new_product['meta_data'][] = array( 'key' => '_knawatfibu_url', 'value' => array_shift( $images ) );
    320                 if ( ! empty( $images ) ) {
    321                     $new_product['meta_data'][] = array( 'key' => '_knawatfibu_wcgallary', 'value' => implode(',', $images) );
    322                 }
    323             } elseif ( isset( $product->images ) && !empty( $product->images ) ) {
    324                 $images = $product->images;
    325                 $new_product['raw_image_id'] = array_shift( $images );
    326                 if ( ! empty( $images ) ) {
    327                     $new_product['raw_gallery_image_ids'] = $images;
    328                 }
    329             }
    330 
    331             if( isset( $product->attributes ) && !empty( $product->attributes ) ){
    332                 foreach ( $product->attributes as $attribute ) {
    333                     $attribute_name = isset( $attribute->name ) ? $this->attribute_languagfy($attribute->name ) : '';
    334                     $attribute_options = array();
    335                     if( isset( $attribute->options ) && !empty( $attribute->options ) ){
    336                         foreach ($attribute->options as $attributevalue) {
    337                             $attribute_formated = $this->attribute_languagfy( $attributevalue );
    338                             if( !in_array( $attribute_formated, $attribute_options ) && !empty( $attribute_formated ) ){
    339                                 $attribute_options[] = $attribute_formated;
    340                             }
    341                         }
    342                     }
    343                     // continue if no attribute name found.
    344                     if( $attribute_name == '' || empty($attribute_options)){
    345                         continue;
    346                     }
    347 
    348                     if( isset( $attributes[ $attribute_name ] ) ){
    349                         if( !empty( $attribute_options ) ){
    350                             $attributes[ $attribute_name ] = array_unique( array_merge( $attributes[ $attribute_name ], $attribute_options ) );
    351                         }
    352                     }else{
    353                         $attributes[ $attribute_name ] = $attribute_options;
    354                     }
    355                 }
    356             }
    357         }
    358 
    359         $variations = array();
    360         $var_attributes = array();
    361         if( isset( $product->variations ) && !empty( $product->variations ) ){
    362             foreach ( $product->variations as $variation ) {
    363                 $temp_variant = array();
    364                 $varient_id = wc_get_product_id_by_sku( $variation->sku );
    365                 if ( $varient_id && $varient_id > 0 ) {
    366                     $temp_variant['id'] = $varient_id;
    367                 }else{
    368                     $temp_variant['sku']  = $variation->sku;
    369                     $temp_variant['name'] = $new_product['name'];
    370                     $temp_variant['type'] = 'variation';
    371                 }
    372 
    373                 // Add Meta Data.
    374                 $temp_variant['meta_data'] = array();
    375                 if( is_numeric( $variation->sale_price ) ){
    376                     $temp_variant['price'] = wc_format_decimal( $variation->sale_price );
    377                 }
    378                 if( is_numeric( $variation->market_price ) ){
    379                     $temp_variant['regular_price'] = wc_format_decimal( $variation->market_price );
    380                 }
    381                 if( is_numeric( $variation->sale_price ) ){
    382                     $temp_variant['sale_price'] = wc_format_decimal( $variation->sale_price );
    383                 }
    384                 $temp_variant['manage_stock'] = true;
    385                 $temp_variant['stock_quantity'] = $this->parse_stock_quantity_field( $variation->quantity );
    386                 $temp_variant['meta_data'][] = array( 'key' => '_knawat_cost', 'value' => wc_format_decimal( $variation->cost_price ) );
    387 
    388                 if( $varient_id && $varient_id > 0 && !$this->params['force_update'] ){
    389                     // Update Data for existing Variend Here.
    390                 }else{
    391                     $temp_variant['weight'] = wc_format_decimal( $variation->weight );
    392                     if( isset( $variation->attributes ) && !empty( $variation->attributes ) ){
    393                         foreach ( $variation->attributes as $attribute ) {
    394                             $temp_attribute_name = isset( $attribute->name ) ? $this->attribute_languagfy($attribute->name ) : '';
    395                             $temp_attribute_value = isset( $attribute->option ) ? $this->attribute_languagfy($attribute->option ) : '';
    396 
    397                             // continue if no attribute name found.
    398                             if( $temp_attribute_name == '' ){
    399                                 continue;
    400                             }
    401 
    402                             $temp_var_attribute = array();
    403                             $temp_var_attribute['name'] = $temp_attribute_name;
    404                             $temp_var_attribute['value'] = array( $temp_attribute_value );
    405                             $temp_var_attribute['taxonomy'] = true;
    406                             $temp_variant['raw_attributes'][] = $temp_var_attribute;
    407 
    408                             // Add attribute name to $var_attributes for make it taxonomy.
    409                             $var_attributes[] = $temp_attribute_name;
    410 
    411                             if( isset( $attributes[ $temp_attribute_name ] ) ){
    412                                 if( !in_array( $temp_attribute_value, $attributes[ $temp_attribute_name ] ) ){
    413                                     $attributes[ $temp_attribute_name ][] = $temp_attribute_value;
    414                                 }
    415                             }else{
    416                                 $attributes[ $temp_attribute_name ][] = $temp_attribute_value;
    417                             }
    418                         }
    419                     }
    420                 }
    421                 $variations[] = $temp_variant;
    422             }
    423         }
    424 
    425         if( !empty( $attributes ) ){
    426             foreach ( $attributes as $name => $value ) {
    427                 $temp_raw = array();
    428                 $temp_raw['name'] = $name;
    429                 $temp_raw['value'] = $value;
    430                 $temp_raw['visible'] = true;
    431                 if( in_array( $name, $var_attributes ) ){
    432                     $temp_raw['taxonomy'] = true;
    433                     $temp_raw['default'] = isset( $value[0] ) ? $value[0] : '';
    434                 }
    435                 $new_product['raw_attributes'][] = $temp_raw;
    436             }
    437         }
    438         $new_product['variations'] = $variations;
    439         return $new_product;
    440     }
    441 
    442     /**
    443      * Set variation data.
    444      *
    445      * @param WC_Product $variation Product instance.
    446      * @param array      $data    Item data.
    447      * @return WC_Product|WP_Error
    448      * @throws Exception If data cannot be set.
    449      */
    450     protected function set_variation_data( &$variation, $data ) {
    451         $parent = false;
    452 
    453         // Check if parent exist.
    454         if ( isset( $data['parent_id'] ) ) {
    455             $parent = wc_get_product( $data['parent_id'] );
    456 
    457             if ( $parent ) {
    458                 $variation->set_parent_id( $parent->get_id() );
    459             }
    460         }
    461 
    462         // Stop if parent does not exists.
    463         if ( ! $parent ) {
    464             return new WP_Error( 'woocommerce_product_importer_missing_variation_parent_id', __( 'Variation cannot be imported: Missing parent ID or parent does not exist yet.', 'dropshipping-woocommerce' ), array( 'status' => 401 ) );
    465         }
    466 
    467         if ( isset( $data['raw_attributes'] ) ) {
    468             $attributes        = array();
    469             $parent_attributes = $this->get_variation_parent_attributes( $data['raw_attributes'], $parent );
    470 
    471             foreach ( $data['raw_attributes'] as $attribute ) {
    472                 $attribute_id = 0;
    473 
    474                 // Get ID if is a global attribute.
    475                 if ( ! empty( $attribute['taxonomy'] ) ) {
    476                     $attribute_id = $this->get_attribute_taxonomy_id( $attribute['name'] );
    477                 }
    478 
    479                 if ( $attribute_id ) {
    480                     $attribute_name_raw = wc_attribute_taxonomy_name_by_id( $attribute_id );
    481                     $attribute_name = sanitize_title( $attribute_name_raw );
    482                 } else {
    483                     $attribute_name_raw = sanitize_title( $attribute['name'] );
    484                     $attribute_name = sanitize_title( $attribute['name'] );
    485                 }
    486 
    487                 if ( ! isset( $parent_attributes[ $attribute_name ] ) || ! $parent_attributes[ $attribute_name ]->get_variation() ) {
    488                     continue;
    489                 }
    490 
    491                 $attribute_key   = sanitize_title( $parent_attributes[ $attribute_name ]->get_name() );
    492                 $attribute_value = isset( $attribute['value'] ) ? current( $attribute['value'] ) : '';
    493 
    494                 if ( $parent_attributes[ $attribute_name ]->is_taxonomy() ) {
    495                     // If dealing with a taxonomy, we need to get the slug from the name posted to the API.
    496                     $term = get_term_by( 'name', $attribute_value, $attribute_name_raw );
    497 
    498                     if ( $term && ! is_wp_error( $term ) ) {
    499                         $attribute_value = $term->slug;
    500                     } else {
    501                         $attribute_value = sanitize_title( $attribute_value );
    502                     }
    503                 }
    504 
    505                 $attributes[ $attribute_key ] = $attribute_value;
    506             }
    507 
    508             $variation->set_attributes( $attributes );
    509         }
    510     }
    511 
    512     /**
    513      * Parse a float value field.
    514      *
    515      * @param string $value Field value.
    516      * @return float|string
    517      */
    518     public function parse_float_field( $value ) {
    519         if ( '' === $value ) {
    520             return $value;
    521         }
    522 
    523         // Remove the ' prepended to fields that start with - if needed.
    524         $value = $this->unescape_negative_number( $value );
    525 
    526         return floatval( $value );
    527     }
    528 
    529     /**
    530      * Parse the stock qty field.
    531      *
    532      * @param string $value Field value.
    533      * @return float|string
    534      */
    535     public function parse_stock_quantity_field( $value ) {
    536         if ( '' === $value ) {
    537             return $value;
    538         }
    539 
    540         // Remove the ' prepended to fields that start with - if needed.
    541         $value = $this->unescape_negative_number( $value );
    542 
    543         return wc_stock_amount( $value );
    544     }
    545 
    546     /**
    547      * Get Import Parameters
    548      *
    549      * @param string $value Field value.
    550      * @return float|string
    551      */
    552     public function get_import_params(){
    553         return $this->params;
    554     }
    555 
    556     /**
    557      * The exporter prepends a ' to fields that start with a - which causes
    558      * issues with negative numbers. This removes the ' if the input is still a valid
    559      * number after removal.
    560      *
    561      * @since 2.0.0
    562      * @param string $value A numeric string that may or may not have ' prepended.
    563      * @return string
    564      */
    565     function unescape_negative_number( $value ) {
    566         if ( 0 === strpos( $value, "'-" ) ) {
    567             $unescaped = trim( $value, "'" );
    568             if ( is_numeric( $unescaped ) ) {
    569                 return $unescaped;
    570             }
    571         }
    572         return $value;
    573     }
    574 
    575     /**
    576      * Setup product seller for add dokan support.
    577      *
    578      * @since 2.0.0
    579      * @param array $product_data Product data for create new product
    580      * @return array $product_data Altered Product data
    581      */
    582     function set_dokan_seller( $product_data ) {
    583         if( knawat_dropshipwc_is_dokan_active() ) {
    584             $dokan_seller = knawat_dropshipwc_get_options( 'dokan_seller' );
    585             if( isset( $product_data['post_author'] ) && $dokan_seller > 0 ){
    586                 $product_data['post_author'] = $dokan_seller;
    587             }
    588         }
    589         return $product_data;
    590     }
    591 
    592     /**
    593      * Get Existing Attribute options by product ID and Attribute name.
    594      *
    595      * @since 2.0.2
    596      * @param array $product_id Product ID for get attribute options
    597      * @param array $attribute_name Attribute name for get attribute options
    598      * @return array $terms Attribute options
    599      */
    600     function get_existing_attribute_values( $product_id, $attribute_name ){
    601         if(empty( $product_id) || empty( $attribute_name ) ){
    602             return array();
    603         }
    604         $terms = array();
    605 
    606         $attribute_id = $this->get_attribute_taxonomy_id( $attribute_name );
    607         // Get name.
    608         $attribute_name = $attribute_id ? wc_attribute_taxonomy_name_by_id( $attribute_id ) : $attribute_name;
    609 
    610         $product = wc_get_product($product_id);
    611         $existing_attributes = $product->get_attributes();
    612         if ( !empty( $existing_attributes ) && !empty( $product ) ) {
    613             foreach ( $existing_attributes as $existing_attribute ) {
    614                 if ( $existing_attribute->get_name() === $attribute_name ) {
    615                     if ( taxonomy_exists( $attribute_name ) ) {
    616                         foreach ( $existing_attribute->get_options() as $option ) {
    617                             if ( is_int( $option ) ) {
    618                                 $term = get_term_by( 'id', $option, $attribute_name );
    619                             } else {
    620                                 $term = get_term_by( 'name', $option, $attribute_name );
    621                                 if ( ! $term || is_wp_error( $term ) ) {
    622                                     $new_term = wp_insert_term( $option, $attribute_name );
    623                                     $term     = is_wp_error( $new_term ) ? false : get_term_by( 'id', $new_term['term_id'], $attribute_name );
    624                                 }
    625                             }
    626                             if ( $term && ! is_wp_error( $term ) && isset( $term->name)) {
    627                                 $terms[] = $term->name;
    628                             }
    629                         }
    630                     }
    631                 }
    632             }
    633         }
    634         return $terms;
    635     }
    636 
    637     /**
    638      * Get Formated string with qtranslate-X languege wrappers for lang object.
    639      *
    640      * @since 2.0.2
    641      * @param array $lang_object Object of values with lang keys
    642      * @return string $formated_value Formated string with language wrappers.
    643      */
    644     function attribute_languagfy( $lang_object ){
    645         if(empty( $lang_object ) ){
    646             return $lang_object;
    647         }
    648         $active_langs = array();
    649         $default_lang = get_locale();
    650         $default_lang = explode( '_', $default_lang );
    651         $default_lang = $default_lang[0];
    652         $active_plugins = knawat_dropshipwc_get_activated_plugins();
    653         if( $active_plugins['qtranslate-x'] ){
    654             global $q_config;
    655             $default_lang = isset( $q_config['default_language'] ) ? sanitize_text_field( $q_config['default_language']  ) : $default_lang;
    656             $active_langs = isset( $q_config['enabled_languages'] ) ? $q_config['enabled_languages'] : array();
    657         }
    658 
    659         $formated_value = isset( $lang_object->$default_lang ) ? sanitize_text_field( $lang_object->$default_lang ) : '';
    660         // if attribute name is blank then take a chance for EN.
    661         if( $formated_value == '' ){
    662             $formated_value = isset( $lang_object->en ) ? $lang_object->en : '';
    663         }
    664         // if attribute name is blank then take a chance for TR.
    665         if( $formated_value == '' ){
    666             $formated_value = isset( $lang_object->tr ) ? $lang_object->tr : '';
    667         }
    668 
    669         if( $active_plugins['qtranslate-x'] && !empty( $active_langs ) ){
    670             $formated_value = '';
    671             foreach ( $active_langs as $active_lang ) {
    672                 if( isset( $lang_object->$active_lang ) ){
    673                     $formated_value .= '[:'.$active_lang.']'.$lang_object->$active_lang;
    674                 }
    675             }
    676             if( $formated_value != ''){
    677                 $formated_value .= '[:]';
    678             }
    679         }
    680         return $formated_value;
    681     }
    682 }
    683 
    684719endif;
Note: See TracChangeset for help on using the changeset viewer.