public function get_cart_from_session()
{
// Load cart session data from session
foreach ($this->cart_session_data as $key => $default) {
$this->{$key} = WC()->session->get($key, $default);
}
$update_cart_session = false;
$this->removed_cart_contents = array_filter(WC()->session->get('removed_cart_contents', array()));
$this->applied_coupons = array_filter(WC()->session->get('applied_coupons', array()));
/**
* Load the cart object. This defaults to the persistent cart if null.
*/
$cart = WC()->session->get('cart', null);
if (is_null($cart) && ($saved_cart = get_user_meta(get_current_user_id(), '_woocommerce_persistent_cart', true))) {
$cart = $saved_cart['cart'];
$update_cart_session = true;
} elseif (is_null($cart)) {
$cart = array();
}
if (is_array($cart)) {
// Prime meta cache to reduce future queries
update_meta_cache('post', wp_list_pluck($cart, 'product_id'));
foreach ($cart as $key => $values) {
$product = wc_get_product($values['variation_id'] ? $values['variation_id'] : $values['product_id']);
if (!empty($product) && $product->exists() && $values['quantity'] > 0) {
if (!$product->is_purchasable()) {
// Flag to indicate the stored cart should be update
$update_cart_session = true;
/* translators: %s: product name */
wc_add_notice(sprintf(__('%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'woocommerce'), $product->get_name()), 'error');
do_action('woocommerce_remove_cart_item_from_session', $key, $values);
} else {
// Put session data into array. Run through filter so other plugins can load their own session data
$session_data = array_merge($values, array('data' => $product));
$this->cart_contents[$key] = apply_filters('woocommerce_get_cart_item_from_session', $session_data, $values, $key);
}
}
}
}
// Trigger action
do_action('woocommerce_cart_loaded_from_session', $this);
if ($update_cart_session) {
WC()->session->cart = $this->get_cart_for_session();
}
// Queue re-calc if subtotal is not set
if (!$this->subtotal && !$this->is_empty() || $update_cart_session) {
$this->calculate_totals();
}
}