Advertisement
Ostap34PHP

Untitled

May 10th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 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.         ////////////////////////
  66.         //Build attributes array
  67.         ////////////////////////
  68.         $attributes = [];
  69.  
  70.         //Doors
  71.         $doors = str_split( $params[1] );
  72.         foreach ( $doors as $iteration => $value ) {
  73.             $door  = self::DOORS[ $iteration ];
  74.             $value = self::MATERIALS[ $value ];
  75.             array_push( $attributes, [
  76.                 'id'     => $door['id'],
  77.                 'name'   => $door['name'],
  78.                 'option' => $value
  79.             ] );
  80.         }
  81.         //Color
  82.         $color = self::COLORS[ $params[2][0] ];
  83.         array_push( $attributes, [
  84.             'id'     => 4,
  85.             'name'   => 'Цвет',
  86.             'option' => $color
  87.         ] );
  88.         //Milling
  89.         $milling = self::MILLING[ $params[2][1] ];
  90.         array_push( $attributes, [
  91.             'id'     => 5,
  92.             'name'   => 'Фрезеровка',
  93.             'option' => $milling
  94.         ] );
  95.  
  96.         //Get product id
  97.         $productTitle = $params[0];
  98.         $productTitle = "217";
  99.         $productID    = $wpdb->get_var( "SELECT ID from $wpdb->posts WHERE post_title LIKE \"%$productTitle%\"" );
  100.  
  101.         //Set-up woocommerce api
  102.         $woocommerce = new Client(
  103.             'http://promebel.local', // Your store URL
  104.             'ck_29f81bf934d967f7525c257d74bd82fcff01ba1f', // Your consumer key
  105.             'cs_dc0f8b192a3f84d82b48a0f4cf79bfbcf02d7fb7', // Your consumer secret
  106.             [
  107.                 'wp_api'  => true, // Enable the WP REST API integration
  108.                 'version' => 'wc/v3' // WooCommerce WP REST API version
  109.             ]
  110.         );
  111.         //Get product vatiations
  112.         $variations = $woocommerce->get( "products/$productID/variations" );
  113.  
  114.         //Check if variation exits
  115.         foreach ($variations as $variation){
  116.             if($variation->attributes === $attributes){
  117.                 var_dump(true);
  118.                 break;
  119.             }
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement