Checkout logic

This commit is contained in:
Almira Krdzic
2018-10-04 03:15:51 +02:00
18 changed files with 285 additions and 147 deletions

View File

@@ -12,49 +12,41 @@ class Wiaas_Checkout {
public static function process_checkout($data) {
try {
$customer = wp_get_current_user();
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
wc_set_time_limit( 0 );
$order_id = WC()->checkout()->create_order(array());
do_action( 'woocommerce_before_checkout_process' );
// Check if cart empty before proceeding
if (WC()->cart->is_empty()) {
return false;
}
do_action( 'woocommerce_checkout_process' );
// Validate cart items before proceeding
WC()->checkout()->check_cart_items();
// if something is wrong bail out
if(wc_notice_count( 'error' ) > 0) {
return false;
}
// try processing order
$order_id = WC()->checkout()->create_order($data);
$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;
}
if ( is_wp_error( $order_id ) ) {
throw new Exception( $order_id->get_error_message() );
}
$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 ( ! $order ) {
throw new Exception( __( 'Unable to create order.', 'woocommerce' ) );
}
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']);
}
self::_add_wiaas_checkout_data($order, $data);
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']);
}
do_action( 'woocommerce_checkout_order_processed', $order_id, array(), $order );
$order->payment_complete();
@@ -63,7 +55,35 @@ class Wiaas_Checkout {
return true;
} catch (Exception $e) {
wc_add_notice( $e->getMessage(), 'error' );
return false;
}
}
/**
* Add additional wiaas checkout info for order
* @param $order
* @param array $data
*/
private static function _add_wiaas_checkout_data($order, $data) {
// save 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;
}
}
// save additional wiaas order info
Wiaas_Order::set_order_vat($order->get_id(), $data['vat']);
Wiaas_Order::set_order_company($order->get_id(), $data['company_name']);
Wiaas_Order::set_order_reference($order->get_id(), $data['reference']);
Wiaas_Order::set_order_tender($order->get_id(), $data['tender']);
// add order to project
if (isset($data['project_id'])) {
Wiaas_Order_Project::set_project_for_order($order->get_id(), $data['project_id']);
}
}
}