Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.90 KB | None | 0 0
  1. <?php
  2. /**
  3. * Run using drush scr export.php
  4. */
  5.  
  6. // Require client for Shoporama
  7.  
  8. require_once __DIR__ . '/../shoporama-php-sdk/vendor/autoload.php';
  9.  
  10. use Shoporama\Client;
  11. use Shoporama\Request;
  12.  
  13. $request = new Request('apikey');
  14. $client = new Client($request);
  15.  
  16. //This function intelligently trims a body of text to a certain
  17. //number of words, but will not break a sentence.
  18. function excerpt($string, $truncation = 15) {
  19. $string = strip_tags($string);
  20. $matches = preg_split("/\s+/", $string);
  21. $count = count($matches);
  22.  
  23. if($count > $truncation) {
  24. //Grab the last word; we need to determine if
  25. //it is the end of the sentence or not
  26. $last_word = strip_tags($matches[$truncation-1]);
  27. $lw_count = strlen($last_word);
  28.  
  29. //The last word in our truncation has a sentence ender
  30. if($last_word[$lw_count-1] == "." || $last_word[$lw_count-1] == "?" || $last_word[$lw_count-1] == "!") {
  31. for($i=$truncation;$i<$count;$i++) {
  32. unset($matches[$i]);
  33. }
  34.  
  35. //The last word in our truncation doesn't have a sentence ender, find the next one
  36. } else {
  37. //Check each word following the last word until
  38. //we determine a sentence's ending
  39. for($i=($truncation);$i<$count;$i++) {
  40. if($ending_found != TRUE) {
  41. $len = strlen(strip_tags($matches[$i]));
  42. if($matches[$i][$len-1] == "." || $matches[$i][$len-1] == "?" || $matches[$i][$len-1] == "!") {
  43. //Test to see if the next word starts with a capital
  44. if($matches[$i+1][0] == strtoupper($matches[$i+1][0])) {
  45. $ending_found = TRUE;
  46. }
  47. }
  48. } else {
  49. unset($matches[$i]);
  50. }
  51. }
  52. }
  53.  
  54. //Check to make sure we still have a closing <p> tag at the end
  55. $body = implode(' ', $matches);
  56. if(substr($body, -4) != "</p>") {
  57. $body = $body;
  58. }
  59.  
  60. return $body;
  61. } else {
  62. return $string;
  63. }
  64. }
  65.  
  66. $suppliers_json = $client->get('/supplier');
  67. $suppliers = json_decode($suppliers_json->getBody(), true);
  68.  
  69. $plastic_profile_id = 16009;
  70.  
  71. $plastic_json = $client->get('/profile-attribute-value?attribute_id=' . $plastic_profile_id);
  72. $plastics = json_decode($plastic_json->getBody(), true);
  73.  
  74. $types_json = $client->get('/profile-attribute-value?attribute_id=16010');
  75. $types = json_decode($types_json->getBody(), true);
  76.  
  77. $levels_json = $client->get('/profile-attribute-value?attribute_id=16011');
  78. $levels = json_decode($levels_json->getBody(), true);
  79.  
  80. $variant_profile_id = 16008;
  81.  
  82. $variants_json = $client->get('/profile-attribute-value?attribute_id=' . $variant_profile_id);
  83. $variants = json_decode($variants_json->getBody(), true);
  84.  
  85. $categories_json = $client->get('/category');
  86. $categories = json_decode($categories_json->getBody(), true);
  87.  
  88. // Bootstrap Drupal.
  89. $drupal_path = $_SERVER['DOCUMENT_ROOT'];
  90. define('DRUPAL_ROOT', $drupal_path);
  91. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  92. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  93.  
  94. // Gettting to work
  95.  
  96. $query = new EntityFieldQuery();
  97.  
  98. $query->entityCondition('entity_type', 'node')
  99. ->entityCondition('bundle', 'discgolf')
  100. ->propertyCondition('status', 1);
  101.  
  102. $result = $query->execute();
  103.  
  104. if (!empty($result['node'])) {
  105. $nids = array_keys($result['node']);
  106.  
  107. foreach ($nids as $nid) {
  108. $data = array();
  109. $shoporama = array();
  110. $test_image_path = '';
  111. $category_ids = array();
  112. $discount = 0;
  113. $related = array();
  114. $reserve_images = array();
  115.  
  116. $node_id = $nid;
  117. $node = node_load($nid, NULL, TRUE);
  118.  
  119. $wrapper = entity_metadata_wrapper('node', $node);
  120.  
  121. $title = $node->title;
  122. $body = $wrapper->body->value()['safe_value'];
  123. $url = drupal_get_path_alias('node/' . $node->nid);
  124.  
  125. // field_collection;
  126. $collection = $wrapper->field_collection->value()->name;
  127. // field_category
  128. $category = $type = $wrapper->field_category->value()->name;
  129. // field_brand;
  130. $brand = $wrapper->field_brand->value()->name;
  131. $supplier = $brand;
  132.  
  133. //3484 - Sportsartikler > Friluftsliv > Udendørsspil > Discgolf
  134. $data["meta"] = "g:google_product_category=3484";
  135.  
  136. if (empty($brand)) {
  137. $brand = $supplier = 'Unknown';
  138. } else {
  139. $data["meta"] .= "\ng:brand=" . $brand;
  140. }
  141. $data["meta"] .= "\ng:product_type=" . $collection . " & " . $category;
  142.  
  143. // field_level
  144. $level = $wrapper->field_level->value()->name;
  145. // field_discount_group
  146. $discount_group = $wrapper->field_discount_group->value()->name;
  147.  
  148. // Shoporama values
  149. $data['name'] = $title;
  150. $data['profile_id'] = 12254;
  151. $data['rewrite_url'] = $url;
  152. $data['description'] = $body;
  153. $data['list_description'] = excerpt($body);
  154. $data['shipping_weight'] = 200;
  155. $data['auto_offline'] = 0;
  156. $data['meta_title'] = '[NAME] - ' . $category . ' fra [SUPPLIER] - kun [PRICE],-';
  157. $data['meta_description'] = '[NAME] fra [SUPPLIER] er en ' . strtolower($category) . ' golfdisc til en ' . strtolower($level) . '.';
  158. $data['canonical_id'] = "";
  159. $data['allow_negative_stock'] = 0;
  160. $data['is_online'] = 1;
  161. $data['own_id'] = $node_id;
  162.  
  163. print 'Importing ' . $node_id . "\n";
  164.  
  165. // Supplier
  166. $found_supplier = array_search($supplier, array_column($suppliers, 'name'));
  167. $supplier_id = $suppliers[$found_supplier]['supplier_id'];
  168. $data['supplier_id'] = $supplier_id;
  169.  
  170. // Categories
  171. $found_category = array_search($collection, array_column($categories, 'name'));
  172. $category_id = $categories[$found_category]['category_id'];
  173. $data['main_category_id'] = $category_id;
  174. $category_ids[] = $category_id;
  175.  
  176. $found_category = array_search($level, array_column($categories, 'name'));
  177. if ($found_category) {
  178. $category_ids[] = $categories[$found_category]['category_id'];
  179. }
  180.  
  181. $found_category = array_search($type, array_column($categories, 'name'));
  182. if ($found_category) {
  183. $category_ids[] = $categories[$found_category]['category_id'];
  184. } else {
  185. echo 'Did not find' . $type;
  186. }
  187.  
  188. $data['categories'] = $category_ids;
  189.  
  190. // Attributes: Levels
  191. $found_level = array_search($level, array_column($levels, 'val'));
  192. $level_id = $levels[$found_level]['attribute_value_id'];
  193.  
  194. // Attributes: types
  195. $found_type = array_search($category, array_column($types, 'val'));
  196. $type_id = $types[$found_type]['attribute_value_id'];
  197.  
  198. $data["attributes"] = array(
  199. // Type 16010
  200. array(
  201. "attribute_id" => 16010,
  202. "value" => $type_id
  203. ),
  204. // Niveau 16011
  205. array(
  206. "attribute_id" => 16011,
  207. "value" => $level_id
  208. ),
  209. );
  210.  
  211. // field_product;
  212. foreach ($wrapper->field_product->value() as $pid) {
  213.  
  214. $product_wrapper = entity_metadata_wrapper('commerce_product', $pid->product_id);
  215.  
  216. // field_plastic
  217. $plastic = $product_wrapper->field_plastic->value()->name;
  218.  
  219. if (empty($shoporama[$plastic])) {
  220. $shoporama[$plastic] = $data;
  221. $shoporama[$plastic]['name'] .= ' - ' . $plastic;
  222. }
  223.  
  224. $product_title = $product_wrapper->title_field->value();
  225. // billysbilling_product_id
  226. $billy_id = $product_wrapper->billysbilling_product_id->value();
  227. // commerce_price
  228. $price = $product_wrapper->commerce_price->value();
  229. $base_price = $price['data']['components'][0]['price']['amount'] / 100;
  230. // commerce_stock
  231. $stock = $product_wrapper->commerce_stock->value()[0];
  232. // field_color
  233. $color = $product_wrapper->field_color->value()->name;
  234. // field_weight
  235. $weight = $product_wrapper->field_weight->value()->name;
  236.  
  237. $shoporama[$plastic]['price'] = $base_price;
  238.  
  239. if ($shoporama[$plastic]['price'] == 0) {
  240. $shoporama[$plastic]['price'] = 100;
  241. }
  242.  
  243. $sell_price = commerce_product_calculate_sell_price($pid);
  244.  
  245. foreach ($sell_price as $pr) {
  246. foreach ($pr['components'] as $p) {
  247. if (substr($p['name'], 0, 9) == 'discount|') {
  248. $discount = abs($p['price']['amount']) / 100 * 0.8;
  249. }
  250. }
  251. }
  252.  
  253. if ($discount > 0) {
  254. $shoporama[$plastic]['sale_price'] = $shoporama[$plastic]['price'] - $discount;
  255. }
  256.  
  257. // Shoporama SKU
  258. $own_id = $nid . '-' . $pid->product_id;
  259.  
  260. // Variants
  261. $variant = $color . ', ' . $weight;
  262.  
  263. $found_variant = array_search($variant, array_column($variants, 'val'));
  264. if ($found_variant === false) {
  265. $new_data = array(
  266. "attribute_id" => $variant_profile_id,
  267. "val" => $variant,
  268. "tag" => ""
  269. );
  270. $response = $client->post('/profile-attribute-value', $new_data);
  271. $interpret = json_decode($response->getBody(), true);
  272. $variant_id = $interpret['attribute_value_id'];
  273. $variants_json = $client->get('/profile-attribute-value?attribute_id=' . $variant_profile_id);
  274. $variants = json_decode($variants_json->getBody(), true);
  275. }
  276.  
  277. $variant_id = $variants[$found_variant]['attribute_value_id'];
  278.  
  279. // Vægt og farve = 16008
  280. $shoporama[$plastic]["stock_settings"][] =
  281. array(
  282. "attribute_id" => 16008,
  283. "attribute_value_id" => $variant_id,
  284. "sec_own_id" => $billy_id,
  285. "own_id" => $own_id
  286. );
  287.  
  288. // Attributes: types
  289. $found_plastic = array_search($plastic, array_column($plastics, 'val'));
  290. if ($found_plastic === false) {
  291. $new_data = array(
  292. "attribute_id" => $plastic_profile_id,
  293. "val" => $plastic,
  294. "tag" => ""
  295. );
  296. $response = $client->post('/profile-attribute-value', $new_data);
  297. $interpret = json_decode($response->getBody(), true);
  298. $plastic_id = $interpret['attribute_value_id'];
  299. $plastic_json = $client->get('/profile-attribute-value?attribute_id=' . $plastic_profile_id);
  300. $plastics = json_decode($plastic_json->getBody(), true);
  301. }
  302.  
  303. $plastic_id = $types[$found_plastic]['attribute_value_id'];
  304.  
  305. // Plastik 16009
  306. $shoporama[$plastic]["attributes"][] =
  307. array(
  308. "attribute_id" => 16009,
  309. "value" => $plastic_id
  310. );
  311.  
  312. // field_images
  313. foreach ($pid->field_images[LANGUAGE_NONE] as $img) {
  314.  
  315. $image_path = drupal_realpath($img['uri']);
  316.  
  317. $base_encoded_image = base64_encode(file_get_contents($image_path));
  318. if ($base_encoded_image == $test_image_path) {
  319. continue;
  320. }
  321. $shoporama[$plastic]["images"][] =
  322. array(
  323. "data" => $base_encoded_image,
  324. "description" => $shoporama[$plastic]['name']
  325. );
  326.  
  327. $test_image_path = $base_encoded_image;
  328. }
  329.  
  330. if (empty($shoporama[$plastic]["images"])) {
  331. $shoporama[$plastic]["images"] = $reserve_images;
  332. }
  333.  
  334. $reserve_images = $shoporama[$plastic]["images"];
  335.  
  336. $stock_data[] =
  337. array(
  338. "SKU" => $own_id,
  339. "stock" => $stock,
  340. "price" => $shoporama[$plastic]['price'] * 0.6
  341. );
  342.  
  343. // Put some products on the front page
  344. if ($stock > 0 and rand(1, 10) == 4) {
  345. $shoporama[$plastic]['categories'][] = 56394;
  346. }
  347.  
  348. // TODO - Ville være ret awesome med automatisk indsættelse :)
  349. $shoporama[$plastic]['related'] = $related;
  350. }
  351.  
  352. foreach ($shoporama as $values) {
  353. $response = $client->post('/product', $values);
  354.  
  355. $created_product = json_decode($response->getBody(), true);
  356. $related[] = $created_product['product_id'];
  357. }
  358.  
  359. foreach ($related as $r) {
  360. $data_related = array('related' => $related);
  361. $client->patch('/product/' . $r, $data_related);
  362. }
  363.  
  364. $client->post('/stock', $stock_data);
  365.  
  366. }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement