Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('woocommerce_new_order', 'send_order_to_master_eshop', 10, 1);
- function send_order_to_master_eshop($order_id) {
- $order = wc_get_order($order_id);
- $masterEshopUrl = 'https://www.originalnetricka.sk'; // URL hlavného eshopu
- $consumerKey = 'ck_40ebe542520e1db41446390780840db79f36da9a'; // Kľúč spotrebiteľa WooCommerce API
- $consumerSecret = 'cs_aec9b7c4b500ba9af7acf4131709394ee4e156b8'; // Tajný kľúč spotrebiteľa WooCommerce API
- $data = [
- 'status' => 'processing', // Status objednávky
- 'payment_method' => $order->get_payment_method(), // Metóda platby
- 'payment_method_title' => $order->get_payment_method_title(), // Názov metódy platby
- 'set_paid' => true, // Nastaviť objednávku ako zaplatenú
- // Zvyšok údajov objednávky (meno, adresa, položky objednávky atď.)
- 'billing' => [
- 'first_name' => $order->get_billing_first_name(),
- 'last_name' => $order->get_billing_last_name(),
- 'address_1' => $order->get_billing_address_1(),
- 'address_2' => $order->get_billing_address_2(),
- // Ďalšie fakturačné údaje
- ],
- 'shipping' => [
- 'first_name' => $order->get_shipping_first_name(),
- 'last_name' => $order->get_shipping_last_name(),
- 'address_1' => $order->get_shipping_address_1(),
- 'address_2' => $order->get_shipping_address_2(),
- // Ďalšie dodacie údaje
- ],
- 'line_items' => [],
- ];
- // Získanie položiek objednávky
- foreach ($order->get_items() as $item_id => $item) {
- $product = $item->get_product();
- $product_data = [
- 'product_id' => $product->get_id(),
- 'quantity' => $item->get_quantity(),
- 'price' => $product->get_price(),
- ];
- array_push($data['line_items'], $product_data);
- }
- $args = [
- 'headers' => [
- 'Authorization' => 'Basic ' . base64_encode($consumerKey . ':' . $consumerSecret),
- 'Content-Type' => 'application/json',
- ],
- 'body' => json_encode($data),
- 'timeout' => 60,
- ];
- $response = wp_remote_post($masterEshopUrl . '/wp-json/wc/v3/orders', $args);
- if (is_wp_error($response)) {
- $error_message = $response->get_error_message();
- error_log("Nastala chyba pri odosielaní objednávky na Master eshop: $error_message");
- } else {
- echo 'Objednávka bola úspešne odoslaná na Master eshop.';
- }
- }
Add Comment
Please, Sign In to add comment