Checkout logic
This commit is contained in:
@@ -26,7 +26,6 @@ class Wiaas_Cart {
|
||||
add_action( 'woocommerce_before_calculate_totals', array( __CLASS__, 'on_calculate_totals' ), 99, 1);
|
||||
|
||||
add_action( 'woocommerce_cart_loaded_from_session', array( __CLASS__, 'on_calculate_totals' ), 99, 1);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,29 +72,35 @@ class Wiaas_Cart {
|
||||
*/
|
||||
public static function upload_cart_document($doc_type, $package_item_key) {
|
||||
try {
|
||||
$version = Wiaas_Document_Upload::upload_document_version();
|
||||
$result = Wiaas_Document_Upload::upload_document_version();
|
||||
|
||||
if (is_wp_error($version)) {
|
||||
return $version;
|
||||
// File upload failed
|
||||
if (is_wp_error($result)) {
|
||||
$code = $result->get_error_code();
|
||||
|
||||
if ($code === 'wiaas_upload_error_missing_file' || $code === 'wiaas_upload_error_invalid_extension') {
|
||||
wc_add_notice($result->get_error_message(), 'error');
|
||||
} else {
|
||||
wc_add_notice('File upload failed!', 'error');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$cart_document = array(
|
||||
'version' => $version,
|
||||
// save uploaded cart documents info to cart item
|
||||
WC()->cart->cart_contents[ $package_item_key ]['_wiaas_documents'][$doc_type] = array(
|
||||
'version' => $result,
|
||||
'key' => wp_generate_uuid4(),
|
||||
);
|
||||
);;
|
||||
|
||||
// persist uploaded cart document in cart
|
||||
WC()->cart->cart_contents[ $package_item_key ]['_wiaas_documents'][$doc_type] = $cart_document;
|
||||
// persist changes
|
||||
WC()->cart->calculate_totals();
|
||||
|
||||
// persist changes to cart session
|
||||
// Note: TODO Refactor this logic of persisting updates made to cart
|
||||
$s = new WC_Cart_Session(WC()->cart);
|
||||
$s->set_session();
|
||||
|
||||
return $cart_document['key'];
|
||||
return true;
|
||||
|
||||
} catch( Exception $e) {
|
||||
return new WP_Error('wiass_cart_upload_error', 'Error while uploading cart file!');
|
||||
wc_add_notice('Could not upload cart document!', 'error');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,9 +122,16 @@ class Wiaas_Cart {
|
||||
public static function add_package_to_cart($package_id, $price_id, $addons_ids, $options_ids) {
|
||||
// try adding package to cart
|
||||
try {
|
||||
// Check if package is in cart
|
||||
if (self::_is_package_in_cart($package_id)) {
|
||||
wc_add_notice('Package already in cart!', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
//Check if package exists
|
||||
$package = wc_get_product( $package_id );
|
||||
if (!$package) {
|
||||
wc_add_notice('Package does not exists!', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -143,6 +155,7 @@ class Wiaas_Cart {
|
||||
$cart_item_key = WC()->cart->add_to_cart($package_id, 1, 0, array(), $wiaas_cart_item_data);
|
||||
|
||||
if (!$cart_item_key) {
|
||||
wc_add_notice('Package could not be added to cart!', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -697,6 +710,24 @@ class Wiaas_Cart {
|
||||
WC()->cart->cart_contents[ $package_cart_item_key ]['_wiaas_addon_items'] = $addon_items_keys;
|
||||
WC()->cart->cart_contents[ $package_cart_item_key ]['_wiaas_option_items'] = $option_items_keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if package is added to cart
|
||||
* @param int $package_id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function _is_package_in_cart($package_id) {
|
||||
$cart_item = WC()->cart->get_cart_contents();
|
||||
|
||||
foreach ($cart_item as $item) {
|
||||
if($item['product_id'] === $package_id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Cart::init();
|
||||
Reference in New Issue
Block a user