· 6 years ago · Aug 16, 2019, 05:08 PM
1<?php
2
3include_once plugin_dir_path( __FILE__ ) . 'include/business_ru_api.php';
4
5function initial_api_settings()
6{
7 //params for api
8 $app_id = "943908";
9 $address = 'https://a93803.business.ru';
10 $secretKey = "cOwXG6CeDjjCad1N8cmIPNka8eBwbDpe"; // секретный ключ
11
12 $api = new Business_ru_api_lib( $app_id, "", $address );
13 $api->setSecret($secretKey); // устанавливаем секретный ключ
14 $api->repair(); //устанавливаем token
15
16 return $api;
17}
18
19function sync_api_product($product)
20{
21
22 if (empty($product))
23 return false;
24
25
26 $api = initial_api_settings();
27
28 $volume = get_post_meta($product->get_id(), "attribute_pa_volume");
29
30 if ($volume != null)
31 $volume = preg_replace("/[^0-9\.]/", '', $volume[0]); //убираем буквы, и получаем объем (тип int)
32
33 $image_path = explode("/", get_the_post_thumbnail_url($product->get_id()));
34
35 $current_product_params = [
36 "name" => $product->get_name(),
37 "cost" => get_post_meta($product->get_id(), "_price")[0],
38 'part' => $product->get_sku() ?: null, //Артикул
39 'volume' => $volume,
40 'images' => [
41 [
42 "name" => end($image_path),
43 "url" => get_the_post_thumbnail_url($product->get_id())
44 ]
45 ]
46 ];
47
48 if ($product->is_type('simple')) {
49 create_api_product($current_product_params, $api);
50
51 return true;
52
53 } elseif ($product->is_type('variable')) {
54
55 $table_api_modifications = "goodsmodifications";
56
57 $available_variations = $product->get_available_variations();
58
59 foreach ($available_variations as $variation) {
60 $goods_modifications = $api->request("get", $table_api_modifications, ["part" => $variation["sku"]]); //api
61
62 if (empty($goods_modifications["result"])) {
63
64 $productData = create_api_product($current_product_params, $api); //создаем товар
65
66 if (empty($productData)) {
67 $productData = get_api_product_by_sku($current_product_params["part"], $api); //присваиваем существующие данные
68 }
69
70 $variation_id = wc_get_product($variation["variation_id"]);
71
72 $variation_image_name = explode("/", $variation["image"]["url"]);
73
74 $goods_params = [
75 "name" => $variation_id->get_formatted_name(),
76 "part" => $variation["sku"],
77 "good_id" => $productData["id"], //pay attention
78 'images' => [
79 [
80 'name' => end($variation_image_name),
81 'url' => $variation["image"]["url"]
82 ]
83 ]
84 ];
85
86 $api->request("post", $table_api_modifications, $goods_params); //создаем модификацию
87 }
88 }
89 return true;
90 }
91
92}
93
94/*
95 * Создание продукта
96 *
97 * @params:
98 * array $current_product_params
99 *
100 * */
101function create_api_product(array $current_product_params, $api)
102{
103 $isCreateProduct = true;
104 $page = 1;
105 $table_api_posts = "goods"; //с какой таблицей в api работаем
106
107 do {
108 $goods = $api->request("get", $table_api_posts, ["page" => $page++]); //api
109
110 if ($goods["status"] == "ok") {
111
112 foreach ($goods["result"] as $good) {
113 if (!is_null($good["part"])) {
114 if ($good["part"] == $current_product_params["part"]) {
115 $isCreateProduct = false;
116 break;
117 }
118 }
119 }
120 } else {
121 return false;
122 }
123 } while (count($goods['result']) > 0); // делаем это до тех пор пока не проверим все продукты
124
125 if($isCreateProduct) {
126 $response = $api->request("post", $table_api_posts, $current_product_params); //создаем товар
127
128 return $response["result"];
129 }
130
131 return [];
132}
133
134/*
135 * Получаем данные первого продукта
136 *
137 * @params:
138 * string $sku
139 *
140 * */
141function get_api_product_by_sku($sku, $api)
142{
143 $table_api_posts = "goods";
144
145 $response = $api->request("get", $table_api_posts, ["part" => $sku]);
146
147 if($response["status"] == "ok")
148 return $response["result"][0];
149 else
150 return false;
151
152}