SHARE
TWEET

Untitled

a guest May 16th, 2018 247 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. products.json
  2. [
  3.   {
  4.     "product_id": 109,
  5.     "type": "simple",
  6.     "sku": "109",
  7.     "name": "Нов продукт 16",
  8.     "description": "Опис за новиот продукт 16",
  9.     "regular_price": 150,
  10.     "sale_price": "",
  11.     "categories": [
  12.               "Активна Мрежна опрема",
  13.               "Безжична опрема / WiFi",
  14.               "WiFi"
  15.             ],
  16.    
  17.     "manage_stock": 1,
  18.     "stock": 10,
  19.     "has_variations": 0,
  20.     "image": "http://localhost/photo/TL-SF1008D-03.jpg"
  21.   }
  22. ]
  23. import.php
  24. <?php
  25.  
  26. define( 'FILE_TO_IMPORT', 'products.json' );
  27.  
  28. require __DIR__ . '/vendor/autoload.php';
  29.  
  30. use Automattic\WooCommerce\Client;
  31. use Automattic\WooCommerce\HttpClient\HttpClientException;
  32.  
  33. if ( ! file_exists( FILE_TO_IMPORT ) ) :
  34.     die( 'Unable to find ' . FILE_TO_IMPORT );
  35. endif; 
  36.  
  37. $woocommerce = new Client(
  38.     'http://localhost/trinity-prodavnica',
  39.     'ck_02221db3d0fb049678344979bfa8b4733c020a52',
  40.     'cs_6a865eb19be1d6ed5fd758ebab5b998486cd6d60',
  41.     [
  42.         'wp_api' => true,
  43.         'version' => 'wc/v2',
  44.        // 'query_string_auth' => true
  45.     ]
  46. );
  47.  
  48. try {
  49.  
  50.     $json = parse_json( FILE_TO_IMPORT );
  51.  
  52.     // Import Attributes
  53.     foreach ( get_attributes_from_json( $json ) as $product_attribute_name => $product_attribute ) :
  54.  
  55.         $attribute_data = array(
  56.             'name' => $product_attribute_name,
  57.             'slug' => 'pa_' . strtolower( $product_attribute_name ),
  58.             'type' => 'select',
  59.             'order_by' => 'menu_order',
  60.             'has_archives' => true
  61.         );
  62.  
  63.         $wc_attribute = $woocommerce->post( 'products/attributes', $attribute_data );
  64.  
  65.         if ( $wc_attribute ) :
  66.             status_message( 'Attribute added. ID: '. $wc_attribute['id'] );
  67.  
  68.             // store attribute ID so that we can use it later for creating products and variations
  69.             $added_attributes[$product_attribute_name]['id'] = $wc_attribute['id'];
  70.            
  71.             // Import: Attribute terms
  72.             foreach ( $product_attribute['terms'] as $term ) :
  73.  
  74.                 $attribute_term_data = array(
  75.                     'name' => $term
  76.                 );
  77.  
  78.                 $wc_attribute_term = $woocommerce->post( 'products/attributes/'. $wc_attribute['id'] .'/terms', $attribute_term_data );
  79.  
  80.                 if ( $wc_attribute_term ) :
  81.                     status_message( 'Attribute term added. ID: '. $wc_attribute['id'] );
  82.  
  83.                     // store attribute terms so that we can use it later for creating products
  84.                     $added_attributes[$product_attribute_name]['terms'][] = $term;
  85.                 endif; 
  86.                
  87.             endforeach;
  88.  
  89.         endif;     
  90.  
  91.     endforeach;
  92.  
  93.  
  94.     $data = get_products_and_variations_from_json( $json, $added_attributes );
  95.  
  96.     // Merge products and product variations so that we can loop through products, then its variations
  97.     $product_data = merge_products_and_variations( $data['products'], $data['product_variations'] );
  98.  
  99.     // Import: Products
  100.     foreach ( $product_data as $k => $product ) :
  101.  
  102.         if ( isset( $product['variations'] ) ) :
  103.             $_product_variations = $product['variations']; // temporary store variations array
  104.  
  105.             // Unset and make the $product data correct for importing the product.
  106.             unset($product['variations']);
  107.         endif;     
  108.  
  109.             $wc_product = $woocommerce->post( 'products', $product );
  110.  
  111.             //if ( $wc_product ) :
  112.                 //status_message( 'Product added. ID: '. $wc_product['id'] );
  113.             //endif;
  114.  
  115.         if ( isset( $_product_variations ) ) :
  116.             // Import: Product variations
  117.  
  118.             // Loop through our temporary stored product variations array and add them
  119.             foreach ( $_product_variations as $variation ) :
  120.                 $wc_variation = $woocommerce->post( 'products/'. $wc_product['id'] .'/variations', $variation );
  121.  
  122.                 if ( $wc_variation ) :
  123.                     status_message( 'Product variation added. ID: '. $wc_variation['id'] . ' for product ID: ' . $wc_product['id'] );
  124.                 endif; 
  125.             endforeach;
  126.  
  127.             // Don't need it anymore
  128.             unset($_product_variations);
  129.         endif;
  130.  
  131.     endforeach;
  132.    
  133.  
  134. } catch ( HttpClientException $e ) {
  135.     echo $e->getMessage(); // Error message
  136. }
  137.  
  138. /**
  139.  * Merge products and variations together.
  140.  * Used to loop through products, then loop through product variations.
  141.  *
  142.  * @param  array $product_data
  143.  * @param  array $product_variations_data
  144.  * @return array
  145. */
  146. function merge_products_and_variations( $product_data = array(), $product_variations_data = array() ) {
  147.     foreach ( $product_data as $k => $product ) :
  148.         foreach ( $product_variations_data as $k2 => $product_variation ) :
  149.             if ( $product_variation['_parent_product_id'] == $product['_product_id'] ) :
  150.  
  151.                 // Unset merge key. Don't need it anymore
  152.                 unset($product_variation['_parent_product_id']);
  153.  
  154.                 $product_data[$k]['variations'][] = $product_variation;
  155.  
  156.             endif;
  157.         endforeach;
  158.  
  159.         // Unset merge key. Don't need it anymore
  160.         unset($product_data[$k]['_product_id']);
  161.     endforeach;
  162.  
  163.     return $product_data;
  164. }
  165.  
  166. /**
  167.  * Get products from JSON and make them ready to import according WooCommerce API properties.
  168.  *
  169.  * @param  array $json
  170.  * @param  array $added_attributes
  171.  * @return array
  172. */
  173. function get_products_and_variations_from_json( $json, $added_attributes ) {
  174.  
  175.     $product = array();
  176.     $product_variations = array();
  177.  
  178.     foreach ( $json as $key => $pre_product ) :
  179.  
  180.         if ( $pre_product['type'] == 'simple' ) :
  181.         if ( $pre_product['image'] ) {
  182.             $product[$key]['images'][] = array(
  183.               'src' => (string) $pre_product['image'],
  184.               'position' => 0
  185.             );
  186.         }
  187.        
  188.  
  189.             $product[$key]['_product_id'] = (string) $pre_product['product_id'];
  190.  
  191.             $product[$key]['name'] = (string) $pre_product['name'];
  192.             $product[$key]['sku'] = (string) $pre_product['sku'];
  193.             $product[$key]['description'] = (string) $pre_product['description'];
  194.             $product[$key]['regular_price'] = (string) $pre_product['regular_price'];
  195.             $product[$key]['sale_price'] = (string) $pre_product['sale_price'];
  196.  
  197.             // Adding selected categories
  198.             function get_product_categories_from_json ($json){
  199.             $product_categories = array();
  200.             $cats = array();
  201.  
  202.             foreach($product_categories as $c){
  203.                 $cat = get_category( $c );
  204.                 $cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
  205.             }
  206. }
  207.             $product_categories = get_product_categories_from_json( $product[$key] );
  208.            
  209.             $product[$key]['cats'] = (array) $pre_product['categories'];
  210.  
  211.    
  212.  
  213.             // Stock
  214.             $product[$key]['manage_stock'] = (bool) $pre_product['manage_stock'];
  215.  
  216.             if ( $pre_product['stock'] > 0 ) :
  217.                 $product[$key]['in_stock'] = (bool) true;
  218.                 $product[$key]['stock_quantity'] = (int) $pre_product['stock'];
  219.             else :
  220.                 $product[$key]['in_stock'] = (bool) false;
  221.                 $product[$key]['stock_quantity'] = (int) 0;
  222.             endif; 
  223.  
  224.         elseif ( $pre_product['type'] == 'variable' ) :
  225.             $product[$key]['_product_id'] = (string) $pre_product['product_id'];
  226.  
  227.             $product[$key]['type'] = 'variable';
  228.             $product[$key]['name'] = (string) $pre_product['name'];
  229.             $product[$key]['description'] = (string) $pre_product['description'];
  230.             $product[$key]['regular_price'] = (string) $pre_product['regular_price'];
  231.  
  232.             // Stock
  233.             $product[$key]['manage_stock'] = (bool) $pre_product['manage_stock'];
  234.  
  235.             if ( $pre_product['stock'] > 0 ) :
  236.                 $product[$key]['in_stock'] = (bool) true;
  237.                 $product[$key]['stock_quantity'] = (int) $pre_product['stock'];
  238.             else :
  239.                 $product[$key]['in_stock'] = (bool) false;
  240.                 $product[$key]['stock_quantity'] = (int) 0;
  241.             endif; 
  242.  
  243.             $attribute_name = $pre_product['attribute_name'];
  244.  
  245.             $product[$key]['attributes'][] = array(
  246.                     'id' => (int) $added_attributes[$attribute_name]['id'],
  247.                     'name' => (string) $attribute_name,
  248.                     'position' => (int) 0,
  249.                     'visible' => true,
  250.                     'variation' => true,
  251.                     'options' => $added_attributes[$attribute_name]['terms']
  252.             );
  253.  
  254.         elseif ( $pre_product['type'] == 'product_variation' ) :   
  255.  
  256.             $product_variations[$key]['_parent_product_id'] = (string) $pre_product['parent_product_id'];
  257.  
  258.             $product_variations[$key]['description'] = (string) $pre_product['description'];
  259.             $product_variations[$key]['regular_price'] = (string) $pre_product['regular_price'];
  260.  
  261.             // Stock
  262.             $product_variations[$key]['manage_stock'] = (bool) $pre_product['manage_stock'];
  263.  
  264.             if ( $pre_product['stock'] > 0 ) :
  265.                 $product_variations[$key]['in_stock'] = (bool) true;
  266.                 $product_variations[$key]['stock_quantity'] = (int) $pre_product['stock'];
  267.             else :
  268.                 $product_variations[$key]['in_stock'] = (bool) false;
  269.                 $product_variations[$key]['stock_quantity'] = (int) 0;
  270.             endif;
  271.  
  272.             $attribute_name = $pre_product['attribute_name'];
  273.             $attribute_value = $pre_product['attribute_value'];
  274.  
  275.             $product_variations[$key]['attributes'][] = array(
  276.                 'id' => (int) $added_attributes[$attribute_name]['id'],
  277.                 'name' => (string) $attribute_name,
  278.                 'option' => (string) $attribute_value
  279.             );
  280.  
  281.         endif;     
  282.     endforeach;    
  283.  
  284.     $data['products'] = $product;
  285.     $data['product_variations'] = $product_variations;
  286.  
  287.     return $data;
  288. }  
  289.  
  290. /**
  291.  * Get attributes and terms from JSON.
  292.  * Used to import product attributes.
  293.  *
  294.  * @param  array $json
  295.  * @return array
  296. */
  297.  
  298.  
  299. function get_attributes_from_json( $json ) {
  300.     $product_attributes = array();
  301.  
  302.     foreach( $json as $key => $pre_product ) :
  303.         if ( !empty( $pre_product['attribute_name'] ) && !empty( $pre_product['attribute_value'] ) ) :
  304.             $product_attributes[$pre_product['attribute_name']]['terms'][] = $pre_product['attribute_value'];
  305.         endif;
  306.     endforeach;    
  307.  
  308.     return $product_attributes;
  309.  
  310. }
  311.  
  312.  
  313. /**
  314.  * Parse JSON file.
  315.  *
  316.  * @param  string $file
  317.  * @return array
  318. */
  319. function parse_json( $file ) {
  320.     $json = json_decode( file_get_contents( $file ), true );
  321.  
  322.     if ( is_array( $json ) && !empty( $json ) ) :
  323.         return $json;  
  324.     else :
  325.         die( 'An error occurred while parsing ' . $file . ' file.' );
  326.  
  327.     endif;
  328. }
  329.  
  330. /**
  331.  * Print status message.
  332.  *
  333.  * @param  string $message
  334.  * @return string
  335. */
  336. function status_message( $message ) {
  337.     echo $message . "\r\n";
  338. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top