Last active
July 19, 2021 15:10
-
-
Save mydropcrm/340c2598389241870e556b4f6ac63175 to your computer and use it in GitHub Desktop.
Код для интеграции получения заказа с корзины сайта WooCommerce (версия для дропшипперов)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_checkout_order_processed', 'action_function_mydropppua' ); | |
function action_function_mydropppua( $order_id ){ | |
$order = wc_get_order($order_id); | |
$order_data = $order->get_data(); | |
$orderUrl = $order_id; // Номер заказа | |
$phone = $order_data['billing']['phone']; // Телефон | |
$first_name = $order_data['billing']['first_name']; // Имя | |
$last_name = $order_data['billing']['last_name']; // Фамилия | |
$payment_method = $order_data['payment_method'] == 'cod' ? 'Наложенный платеж' : 'Предоплата'; | |
// способ оплаты | |
$city = str_replace('город ','',$order_data['shipping']['city']); // Город | |
$address = $order_data['shipping']['address_1']; // Склад | |
foreach( $order->get_items( 'shipping' ) as $item_id => $shipping_item_obj ){ | |
$order_item_name = $shipping_item_obj->get_name(); | |
} | |
//----- Формируем товар -------------------------- | |
$products = array(); | |
foreach($order->get_items() as $item) { | |
$product = $item->get_data(); | |
$product_meta = $product['meta_data'][0]; | |
$product_not_exist_vendor_not_exist = array( | |
'product_title' => $product['name'], // название нового товара | |
'price' => wc_get_product($product['product_id'])->get_price(), // цена продажи товара | |
'product_sku' => wc_get_product($product['product_id'])->get_sku(), // артикул | |
'amount' => $product['quantity'], // количество товара | |
'size_title' => mb_strtoupper($product_meta->value) // размер товара (необязательно) | |
); | |
array_push($products, $product_not_exist_vendor_not_exist); | |
} | |
// параметры запроса | |
$data = array( | |
'name' => $first_name . ' ' . $last_name, // имя и фамилия покупателя | |
'phone' => $phone, // телефон | |
'city' => $city, // город | |
'warehouse_number'=> $address, // склад Новой Почты | |
'products' => $products, // массив с товарами заказа | |
'order_source' => 'Сайт', // источник заказа (необязательно) | |
'traffic_source' => 'AdWords', // источник трафика (необязательно) | |
'description' => $payment_method // способ оплаты передаём в описание (необязательно) | |
); | |
// запрос | |
$curl = curl_init(); | |
$production_url = 'https://backend.mydrop.com.ua/dropshipper/api/orders'; | |
curl_setopt($curl, CURLOPT_URL, $production_url); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'X-API-KEY: ваш_API_ключ', // замените на ваш API-ключ | |
'Content-Type: application/json' | |
)); | |
$out = curl_exec($curl); | |
curl_close($curl); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment