Files
old-new-wiaas/backend/app/plugins/wiaas/includes/class-wiaas-checkout.php
2018-09-24 21:51:55 +02:00

77 lines
2.2 KiB
PHP

<?php
class Wiaas_Checkout {
public static function init() {
}
/**
* Process the order checkout.
*
* @param array $data Posted data.
*
* @return bool
*/
public static function process_checkout($data) {
try {
$customer = wp_get_current_user();
$order_id = WC()->checkout()->create_order(array());
$order = wc_get_order( $order_id );
// set order currency
$line_items = $order->get_items();
foreach ($line_items as $line_item) {
if (isset($line_item['wiaas_currency'])) {
$order->set_currency($line_item['wiaas_currency']);
break;
}
}
$delivery_address = Wiaas_Customer::get_customer_profile_address($customer->ID, $data['delivery_address_id']);
$billing_address = Wiaas_Customer::get_customer_billing_address($customer->ID, $data['billing_address_id']);
if (isset($delivery_address)) {
$order->set_shipping_city($delivery_address['city']);
$order->set_shipping_country($delivery_address['country_name']);
$order->set_shipping_address_1($delivery_address['detailed_address']);
$order->set_shipping_postcode($delivery_address['zip_code']);
$order->set_shipping_first_name($delivery_address['first_name']);
$order->set_shipping_last_name($delivery_address['last_name']);
}
if (isset($billing_address)) {
$order->set_billing_city($billing_address['city']);
$order->set_billing_country($billing_address['country_name']);
$order->set_billing_address_1($billing_address['detailed_address']);
$order->set_billing_postcode($billing_address['zip_code']);
$order->set_billing_first_name($billing_address['first_name']);
$order->set_billing_last_name($billing_address['last_name']);
}
Wiaas_Order::set_order_vat($order_id, $data['vat']);
Wiaas_Order::set_order_company($order_id, $data['company_name']);
Wiaas_Order::set_order_reference($order_id, $data['reference']);
Wiaas_Order::set_order_tender($order_id, $data['tender']);
if (isset($data['project_id'])) {
Wiaas_Order_Project::set_project_for_order($order_id, $data['project_id']);
}
$order->payment_complete();
WC()->cart->empty_cart( true );
return true;
} catch (Exception $e) {
return false;
}
}
}
Wiaas_Checkout::init();