laciikee

kodik

May 22nd, 2023
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. add_action('woocommerce_new_order', 'send_order_to_master_eshop', 10, 1);
  2. function send_order_to_master_eshop($order_id) {
  3.     $order = wc_get_order($order_id);
  4.  
  5.     $masterEshopUrl = 'https://www.originalnetricka.sk'; // URL hlavného eshopu
  6.     $consumerKey = 'ck_40ebe542520e1db41446390780840db79f36da9a'; // Kľúč spotrebiteľa WooCommerce API
  7.     $consumerSecret = 'cs_aec9b7c4b500ba9af7acf4131709394ee4e156b8'; // Tajný kľúč spotrebiteľa WooCommerce API
  8.    
  9.     $data = [
  10.         'status' => 'processing', // Status objednávky
  11.         'payment_method' => $order->get_payment_method(), // Metóda platby
  12.         'payment_method_title' => $order->get_payment_method_title(), // Názov metódy platby
  13.         'set_paid' => true, // Nastaviť objednávku ako zaplatenú
  14.        
  15.         // Zvyšok údajov objednávky (meno, adresa, položky objednávky atď.)
  16.         'billing' => [
  17.             'first_name' => $order->get_billing_first_name(),
  18.             'last_name' => $order->get_billing_last_name(),
  19.             'address_1' => $order->get_billing_address_1(),
  20.             'address_2' => $order->get_billing_address_2(),
  21.             // Ďalšie fakturačné údaje
  22.         ],
  23.         'shipping' => [
  24.             'first_name' => $order->get_shipping_first_name(),
  25.             'last_name' => $order->get_shipping_last_name(),
  26.             'address_1' => $order->get_shipping_address_1(),
  27.             'address_2' => $order->get_shipping_address_2(),
  28.             // Ďalšie dodacie údaje
  29.         ],
  30.         'line_items' => [],
  31.     ];
  32.  
  33.     // Získanie položiek objednávky
  34.     foreach ($order->get_items() as $item_id => $item) {
  35.         $product = $item->get_product();
  36.         $product_data = [
  37.             'product_id' => $product->get_id(),
  38.             'quantity' => $item->get_quantity(),
  39.             'price' => $product->get_price(),
  40.         ];
  41.         array_push($data['line_items'], $product_data);
  42.     }
  43.  
  44.     $args = [
  45.         'headers' => [
  46.             'Authorization' => 'Basic ' . base64_encode($consumerKey . ':' . $consumerSecret),
  47.             'Content-Type' => 'application/json',
  48.         ],
  49.         'body' => json_encode($data),
  50.         'timeout' => 60,
  51.     ];
  52.  
  53.     $response = wp_remote_post($masterEshopUrl . '/wp-json/wc/v3/orders', $args);
  54.  
  55.     if (is_wp_error($response)) {
  56.         $error_message = $response->get_error_message();
  57.         error_log("Nastala chyba pri odosielaní objednávky na Master eshop: $error_message");
  58.     } else {
  59.         echo 'Objednávka bola úspešne odoslaná na Master eshop.';
  60.     }
  61. }
  62.  
Add Comment
Please, Sign In to add comment