Implement shop search and refactor
This commit is contained in:
@@ -26,6 +26,8 @@ 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);
|
||||
|
||||
add_action('woocommerce_checkout_create_order', array(__CLASS__, 'add_additional_order_data'), 99);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,13 +116,13 @@ class Wiaas_Cart {
|
||||
*
|
||||
* @param int $package_id Package ID of selected package
|
||||
* @param string $price_id Price ID of selected package payment
|
||||
* @param int $commercial_lead_id Shop owner commercial lead ID
|
||||
* @param int $shop_owner_id Shop owner commercial lead ID
|
||||
* @param array $addons_ids Array of selected additional packages IDs
|
||||
* @param array $options_ids Array of selected option packages IDs
|
||||
*
|
||||
* @return bool TRUE if all packages are succesfully added to cart, FALSE otherwise
|
||||
*/
|
||||
public static function add_package_to_cart($package_id, $price_id, $commercial_lead_id, $addons_ids, $options_ids) {
|
||||
public static function add_package_to_cart($package_id, $price_id, $shop_owner_id, $addons_ids, $options_ids) {
|
||||
// try adding package to cart
|
||||
try {
|
||||
// Check if package is in cart
|
||||
@@ -138,17 +140,28 @@ class Wiaas_Cart {
|
||||
|
||||
//Check if package is available for adding to cart
|
||||
if (Wiaas_Package_Status::get_package_status($package_id) !== Wiaas_Package_Status::AVAILABLE){
|
||||
wc_add_notice('Package cannot be purchased at the moment', 'error');
|
||||
wc_add_notice('Package cannot be purchased at the moment!', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Retrieve package country
|
||||
$country = Wiaas_Countries::get_package_country($package);
|
||||
|
||||
if (empty($country)) {
|
||||
wc_add_notice('Package cannot be added do cart!', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Add validation that only packages from the same country can be added to cart at the same time
|
||||
update_user_meta( get_current_user_id(), '_wiaas_cart_items_country', $country);
|
||||
|
||||
// TODO: Add validation that only packages from the same shop can be added to cart at the same time
|
||||
update_user_meta( get_current_user_id(), '_wiaas_cart_shop_owner_id', $shop_owner_id);
|
||||
|
||||
$customer_id = wiaas_get_current_user_organization_id();
|
||||
|
||||
// Retrieve package price
|
||||
$package_prices = Wiaas_Pricing::get_standard_package_customer_prices($package, $customer_id, $commercial_lead_id);
|
||||
$package_prices = Wiaas_Pricing::get_standard_package_customer_prices($package, $customer_id, $shop_owner_id);
|
||||
|
||||
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
|
||||
|
||||
@@ -157,9 +170,7 @@ class Wiaas_Cart {
|
||||
'_wiaas_standard_package' => true,
|
||||
'_wiaas_addon_items' => array(),
|
||||
'_wiaas_option_items' => array(),
|
||||
'_wiaas_currency' => isset($country) ? $country['currency'] : get_woocommerce_currency(),
|
||||
'_wiaas_payment' => $package_prices[$selected_price_index] ? $package_prices[$selected_price_index] : null,
|
||||
'_wiaas_commercial_lead_id' => $commercial_lead_id,
|
||||
'_wiaas_documents' => array()
|
||||
);
|
||||
|
||||
@@ -171,7 +182,7 @@ class Wiaas_Cart {
|
||||
}
|
||||
|
||||
// Add selected additional packages and options
|
||||
self::_add_additional_packages_to_cart($cart_item_key, $price_id, $commercial_lead_id, $addons_ids, $options_ids);
|
||||
self::_add_additional_packages_to_cart($cart_item_key, $price_id, $shop_owner_id, $addons_ids, $options_ids);
|
||||
|
||||
// Trigger calculation of total prices after additional packages are added
|
||||
WC()->cart->calculate_totals();
|
||||
@@ -300,12 +311,6 @@ class Wiaas_Cart {
|
||||
if (isset($cart_item['_wiaas_standard_package'])) {
|
||||
$order_item->add_meta_data( '_wiaas_standard_package', $cart_item['_wiaas_standard_package'], true );
|
||||
}
|
||||
if (isset($cart_item['_wiaas_currency'])) {
|
||||
$order_item->add_meta_data( '_wiaas_currency', $cart_item['_wiaas_currency'], true );
|
||||
}
|
||||
if (isset($cart_item['_wiaas_commercial_lead_id'])) {
|
||||
$order_item->add_meta_data( '_wiaas_commercial_lead_id', $cart_item['_wiaas_commercial_lead_id'], true );
|
||||
}
|
||||
|
||||
// add options metadata
|
||||
if (isset($cart_item['_wiaas_option_items'])) {
|
||||
@@ -383,12 +388,33 @@ class Wiaas_Cart {
|
||||
'_wiaas_option_for',
|
||||
'_wiaas_option_group_name',
|
||||
'_wiaas_standard_package',
|
||||
'_wiaas_currency',
|
||||
'_wiaas_documents',
|
||||
'_wiaas_commercial_lead_id'
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets additional order data form cart after order is successfully created
|
||||
*
|
||||
* @param WC_Order $order
|
||||
*
|
||||
* @throws WC_Data_Exception
|
||||
*
|
||||
*/
|
||||
public static function add_additional_order_data($order) {
|
||||
|
||||
// set order currency
|
||||
$country = get_user_meta(get_current_user_id(), '_wiaas_cart_items_country', true);
|
||||
$currency = empty($country) ? get_woocommerce_currency() : $country['currency'];
|
||||
|
||||
$order->set_currency($currency);
|
||||
|
||||
// set order commercial lead
|
||||
$shop_owner_id = get_user_meta(get_current_user_id(), '_wiaas_cart_shop_owner_id', true);
|
||||
$shop_owner_id = absint($shop_owner_id);
|
||||
|
||||
$order->add_meta_data('_wiaas_commercial_lead_id', $shop_owner_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate total cost for cart item
|
||||
*
|
||||
@@ -456,6 +482,9 @@ class Wiaas_Cart {
|
||||
public static function get_cart_packages() {
|
||||
$items = WC()->cart->get_cart_contents();
|
||||
|
||||
$shop_owner_id = get_user_meta(get_current_user_id(), '_wiaas_cart_shop_owner_id', true);
|
||||
$shop_owner_id = absint($shop_owner_id);
|
||||
|
||||
$package_items = array();
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
@@ -505,8 +534,8 @@ class Wiaas_Cart {
|
||||
'package_name' => $package->get_title(),
|
||||
'quantity' => $item['quantity'],
|
||||
|
||||
'commercial_lead_id' => 14,
|
||||
'commercial_lead' => 'Coor Service Management',
|
||||
'commercial_lead_id' => $shop_owner_id,
|
||||
'commercial_lead' => wiaas_get_organization_name($shop_owner_id),
|
||||
'country' => Wiaas_Countries::get_package_country($package),
|
||||
|
||||
'are_additional_available' => true,
|
||||
@@ -641,13 +670,13 @@ class Wiaas_Cart {
|
||||
*
|
||||
* @param string $package_cart_item_key
|
||||
* @param int $price_id
|
||||
* @param int $commercial_lead_id
|
||||
* @param int $shop_owner_id
|
||||
* @param array $addons_ids
|
||||
* @param array $options_ids
|
||||
*
|
||||
* @throws Exception if any of the addons or options cannot be added to cart
|
||||
*/
|
||||
private static function _add_additional_packages_to_cart($package_cart_item_key, $price_id, $commercial_lead_id, $addons_ids, $options_ids) {
|
||||
private static function _add_additional_packages_to_cart($package_cart_item_key, $price_id, $shop_owner_id, $addons_ids, $options_ids) {
|
||||
|
||||
$parent_item = WC()->cart->get_cart_item($package_cart_item_key);
|
||||
|
||||
@@ -669,7 +698,7 @@ class Wiaas_Cart {
|
||||
$addon_package,
|
||||
$parent_item['data'],
|
||||
$customer_id,
|
||||
$commercial_lead_id
|
||||
$shop_owner_id
|
||||
);
|
||||
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
|
||||
|
||||
@@ -706,7 +735,7 @@ class Wiaas_Cart {
|
||||
$option_package,
|
||||
$parent_item['data'],
|
||||
$customer_id,
|
||||
$commercial_lead_id);
|
||||
$shop_owner_id);
|
||||
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
|
||||
|
||||
// Retrieve option package group name
|
||||
|
||||
Reference in New Issue
Block a user