Advertisement
Ostap34PHP

Untitled

May 10th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1. <?php
  2.  
  3. use Automattic\WooCommerce\Client;
  4.  
  5. class ImportController {
  6.  
  7.     //Database-like structure (config)
  8.     const DOORS = [
  9.         [
  10.             'id' => 7,
  11.             'name' => '1 Дверь'
  12.         ],
  13.         [
  14.             'id' => 8,
  15.             'name' => '2 Дверь'
  16.         ],
  17.         [
  18.             'id' => 9,
  19.             'name' => '3 Дверь'
  20.         ],
  21.         [
  22.             'id' => 10,
  23.             'name' => '4 Дверь'
  24.         ],
  25.     ];
  26.  
  27.     const MATERIALS = [
  28.         'МДФ',
  29.         'Зеркало',
  30.         'Стекло'
  31.     ];
  32.  
  33.     const COLORS = [
  34.         1 => 'Бежевый',
  35.         2 => 'Белый'
  36.     ];
  37.  
  38.     const MILLING = [
  39.         1 => 'Верона',
  40.         2 =>'Ламель'
  41.     ];
  42.  
  43.     const DOORKNOBS = [
  44.         'Л' => 'Слева',
  45.         'П' => 'Справа'
  46.     ];
  47.  
  48.     /**
  49.      * Show form for creation new import
  50.      * @return mixed
  51.      */
  52.     static function show()
  53.     {
  54.         return view('form');
  55.     }
  56.  
  57.     /**
  58.      * Import
  59.      */
  60.     static function store() {
  61.         global $wpdb;
  62.         $image  = $_FILES['image'];
  63.         $params = explode( '-', $image['name'] );
  64.  
  65.         //Build attributes array
  66.         $attributes = [];
  67.  
  68.         //Doors
  69.         $doors = str_split( $params[1] );
  70.         foreach ( $doors as $iteration => $value ) {
  71.             $door  = self::DOORS[ $iteration ];
  72.             $value = self::MATERIALS[ $value ];
  73.             array_push( $attributes, [
  74.                 'id'     => $door['id'],
  75.                 'name'   => $door['name'],
  76.                 'option' => $value
  77.             ] );
  78.         }
  79.         //Color
  80.         $color = self::COLORS[ $params[2][0] ];
  81.         array_push( $attributes, [
  82.             'id'     => 4,
  83.             'name'   => 'Цвет',
  84.             'option' => $color
  85.         ] );
  86.         //Milling
  87.         $milling = self::MILLING[ $params[2][1] ];
  88.         array_push( $attributes, [
  89.             'id'     => 5,
  90.             'name'   => 'Фрезеровка',
  91.             'option' => $milling
  92.         ] );
  93.  
  94.         //Get product id
  95.         $productTitle = $params[0];
  96.  
  97.         $productID  = $wpdb->get_var( "SELECT ID from $wpdb->posts WHERE post_title LIKE \"%$productTitle%\"" );
  98.  
  99.         try {
  100.             if ( empty( $productID ) ) {
  101.                 throw new Exception(
  102.                     "Product not found, please create a product that contains the title '$productTitle'"
  103.                 );
  104.             }
  105.  
  106.             //Set-up woocommerce api
  107.             $woocommerce = new Client(
  108.                 'http://promebel.local', // Your store URL
  109.                 'ck_29f81bf934d967f7525c257d74bd82fcff01ba1f', // Your consumer key
  110.                 'cs_dc0f8b192a3f84d82b48a0f4cf79bfbcf02d7fb7', // Your consumer secret
  111.                 [
  112.                     'wp_api'  => true, // Enable the WP REST API integration
  113.                     'version' => 'wc/v3' // WooCommerce WP REST API version
  114.                 ]
  115.             );
  116.             //Get product vatiations
  117.             $variations = $woocommerce->get( "products/$productID/variations");
  118.  
  119.             //Check if variation exits
  120.             $exits = false;
  121.             foreach ( $variations as $variation ) {
  122.                 if ( $variation->attributes === $attributes ) {
  123.                     $exits = true;
  124.                     break;
  125.                 }
  126.             }
  127.  
  128.             if (!$exits){
  129.                 $photoId = upload_photo($image);
  130.  
  131.                 $woocommerce->post("products/$productID/variations" , [
  132.                     'image' => [
  133.                         'id' => $photoId
  134.                     ],
  135.                     'attributes' => $attributes
  136.                 ]);
  137.             }
  138.         } catch(Exception $e) {
  139.             echo 'Error: ' .$e->getMessage();
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement