· 6 years ago · Aug 14, 2019, 11:18 AM
1<?php
2include_once plugin_dir_path( __FILE__ ) . 'include/business_ru_api.php';
3
4function initial_api_settings()
5{
6 //params for api
7 $app_id = "943908";
8 $address = 'https://a93803.business.ru';
9 $secretKey = "cOwXG6CeDjjCad1N8cmIPNka8eBwbDpe"; // секретный ключ
10
11 $api = new Business_ru_api_lib( $app_id, "", $address );
12 $api->setSecret($secretKey); // устанавливаем секретный ключ
13 $api->repair(); //устанавливаем token
14
15 return $api;
16}
17//615908426687
18function get_api_product_data($sku = "615908426687") //4734 id //
19{
20 global $wpdb;
21
22 $table_name = "goods";
23
24 $api = initial_api_settings();
25 $goods = $api->request("get", $table_name, ["part" => $sku]);
26
27
28 if ($goods["status"] == "ok") {
29 //get post id
30 $post_id = $wpdb->get_results("select post_id from $wpdb->postmeta where meta_value = '{$sku}'", ARRAY_A);
31
32 if(empty($post_id)){
33 echo "Артикул: {$sku} - не существует в базе данных\n";
34 exit;
35 }
36
37 $post_id = $post_id[0]["post_id"];
38
39 $name = get_post($post_id)->post_title;
40 $cost = get_post_meta($post_id, "_price")[0];
41 $image = get_the_post_thumbnail_url($post_id);
42
43 $params = [
44 "name" => $name,
45 "full_name" => $name,
46 "cost" => $cost,
47 "store_code" => $post_id,
48 'part' => $sku, //Артикул
49 // 'images' => $image
50 ];
51
52 if (empty($goods["result"])) {
53// $response = $api->request("post", $table_name, $params); //добавляем данные
54//
55// if ($response["status"] == "error") { // ошибка
56// echo "Ошибка! Артикул: {$sku}; ". $response["error_text"] . "\n";
57// exit;
58// } else { //успех
59// echo "Добавили! Имя товара: {$name}; Артикул: {$sku} ". "\n";
60// }
61
62 return true;
63 }
64 }
65}
66
67
68
69
70get_api_product_data();
71exit;