90 lines
2.1 KiB
PHP
90 lines
2.1 KiB
PHP
<?php
|
|
|
|
class Wiaas_Checkout {
|
|
|
|
/**
|
|
* Process the order checkout.
|
|
*
|
|
* @param array $data Posted data.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function process_checkout($data) {
|
|
try {
|
|
|
|
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
|
|
wc_set_time_limit( 0 );
|
|
|
|
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 );
|
|
|
|
if ( is_wp_error( $order_id ) ) {
|
|
throw new Exception( $order_id->get_error_message() );
|
|
}
|
|
|
|
if ( ! $order ) {
|
|
throw new Exception( __( 'Unable to create order.', 'woocommerce' ) );
|
|
}
|
|
|
|
self::_add_wiaas_checkout_data($order, $data);
|
|
|
|
do_action( 'woocommerce_checkout_order_processed', $order_id, array(), $order );
|
|
|
|
$order->payment_complete();
|
|
|
|
WC()->cart->empty_cart( true );
|
|
|
|
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']);
|
|
}
|
|
}
|
|
}
|