WC_Cart_Session::set_sessionpublicWC 1.0

Sets the php session data for the cart and coupons.

Method of the class: WC_Cart_Session{}

Hooks from the method

Returns

null. Nothing (null).

Usage

$WC_Cart_Session = new WC_Cart_Session();
$WC_Cart_Session->set_session();

WC_Cart_Session::set_session() code WC 10.5.0

public function set_session() {
	$wc_session = WC()->session;

	$cart                       = $this->get_cart_for_session();
	$applied_coupons            = $this->cart->get_applied_coupons();
	$coupon_discount_totals     = $this->cart->get_coupon_discount_totals();
	$coupon_discount_tax_totals = $this->cart->get_coupon_discount_tax_totals();
	$removed_cart_contents      = $this->cart->get_removed_cart_contents();

	/*
	 * We want to clear out any empty/default data from the session that have no value in being stored so the session
	 * can be forgotten if empty.
	 */
	$wc_session->set( 'cart_totals', empty( $cart ) ? null : $this->cart->get_totals() );
	$wc_session->set( 'cart', empty( $cart ) ? null : $cart );
	$wc_session->set( 'applied_coupons', empty( $applied_coupons ) ? null : $applied_coupons );
	$wc_session->set( 'coupon_discount_totals', empty( $coupon_discount_totals ) ? null : $coupon_discount_totals );
	$wc_session->set( 'coupon_discount_tax_totals', empty( $coupon_discount_tax_totals ) ? null : $coupon_discount_tax_totals );
	$wc_session->set( 'removed_cart_contents', empty( $removed_cart_contents ) ? null : $removed_cart_contents );
	if ( ! $this->cart_has_shippable_products() ) {
		$wc_session->set( 'shipping_method_counts', null );
		$wc_session->set( 'previous_shipping_methods', null );
		$wc_session->set( 'chosen_shipping_methods', null );
		$this->remove_shipping_for_package_from_session();
	}
	if ( empty( $cart ) ) {
		$wc_session->set( 'store_api_draft_order', null );
	}

	/**
	 * Fires when cart is updated.
	 *
	 * @since 3.2.0
	 */
	do_action( 'woocommerce_cart_updated' );
}