Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function read_cart(){
- if( !WC()->cart ) return; //Exit if Woocommerce cart has not been initialized
- //Retrieving cart total value and currency
- $admin = new CartBounty_Pro_Admin( CARTBOUNTY_PRO_PLUGIN_NAME_SLUG, CARTBOUNTY_PRO_VERSION_NUMBER );
- $translatepress = new CartBounty_Pro_TranslatePress();
- $cart_total = WC()->cart->total;
- $cart_currency = get_woocommerce_currency();
- $current_time = current_time( 'mysql', false ); //Retrieving current time
- $session_id = WC()->session->get( 'cartbounty_pro_session_id' ); //Check if the session is already set
- if( empty( $session_id ) ){ //If session value does not exist - set one now
- $session_id = WC()->session->get_customer_id(); //Retrieving customer ID from WooCommerce sessions variable
- }
- if( WC()->session->get( 'cartbounty_pro_from_link' ) && WC()->session->get( 'cartbounty_pro_session_id' ) ){ //In case users returns from abandoned cart email link
- $session_id = WC()->session->get( 'cartbounty_pro_session_id' );
- }
- //Retrieving cart
- $products = WC()->cart->get_cart_contents();
- $product_array = array();
- foreach( $products as $key => $product ){
- do_action( 'woocommerce/cart_loop/start', $product );
- $item = wc_get_product( $product['data']->get_id() );
- $product_title = strip_tags( $item->get_title() );
- $product_title = $translatepress->maybe_translate_trp( $product_title );
- $product_quantity = $product['quantity'];
- $product_variation_price = '';
- $product_tax = '';
- $variation_attributes = array();
- $product_variation_id = '';
- $product_attributes = '';
- if( isset( $product['line_total'] ) ){
- $product_variation_price = $product['line_total'];
- }
- if( isset( $product['line_tax'] ) ){ //If we have taxes, add them to the price
- $product_tax = $product['line_tax'];
- }
- //Handling product variations
- if( isset( $product['variation'] ) ){
- if( is_array( $product['variation'] ) ){
- foreach( $product['variation'] as $key => $variation ){
- $variation_attributes[$key] = $variation;
- }
- }
- }
- if( isset( $product['variation_id'] ) ){ //If user has chosen a variation
- $product_variation_id = $product['variation_id'];
- $product_attributes = $this->get_attribute_names( $variation_attributes, $product['product_id'] );
- }
- $product_data = array(
- 'product_title' => $product_title . $product_attributes,
- 'quantity' => $product_quantity,
- 'product_id' => $product['product_id'],
- 'product_variation_id' => $product_variation_id,
- 'product_variation_price' => $product_variation_price,
- 'product_variation_attributes' => $variation_attributes,
- 'product_tax' => $product_tax
- );
- //Handle WooCommerce Product Bundles plugin data
- if( class_exists( 'WC_Bundles' ) ){ //If WooCommerce Product Bundles plugin active
- if( !wc_pb_is_bundled_cart_item( $product ) ){ //If cart item is not part of a bundle (looking to find only bundle parent item)
- if( isset( $product['stamp'] ) ){ //If bundle parent has data about bundled items
- $bundled_item_data = $product['stamp'];
- $product_data['bundle_data'] = $bundled_item_data;
- $bundle_price = 0;
- $bundle_tax = 0;
- $bundled_cart_items = wc_pb_get_bundled_cart_items( $product );
- //Looping through bundle sub-items to get price of the main bundle item
- foreach ( $bundled_cart_items as $key => $bundled_cart_item ) {
- if( isset( $bundled_cart_item['line_total'] ) ){
- $bundled_cart_item_price = $bundled_cart_item['line_total'];
- }
- if( isset( $bundled_cart_item['line_tax'] ) ){ //If we have taxes, add them to the price
- $bundled_cart_item_tax = $bundled_cart_item['line_tax'];
- }
- $bundle_price = $bundle_price + $bundled_cart_item_price;
- $bundle_tax = $bundle_tax + $bundled_cart_item_tax;
- }
- $product_data['product_variation_price'] = $product_variation_price + $bundle_price;
- $product_data['product_tax'] = $product_tax + $bundle_tax;
- }
- }else{ //Skip adding individual bundle sub-items to the cart
- do_action( 'woocommerce/cart_loop/end', $product );
- continue;
- }
- }
- //Skip adding bundled sub-items.
- //Added for YITH Product Bundles because cart restoring creates incorrect pricing
- if( isset( $product['bundled_by'] ) ){
- do_action( 'woocommerce/cart_loop/end', $product );
- continue;
- }
- $product_array[] = $product_data;
- do_action( 'woocommerce/cart_loop/end', $product );
- }
- $cart_contents = array(
- 'products' => $product_array,
- 'cart_data' => WC()->cart->get_cart_contents()
- );
- return $results_array = array(
- 'cart_total' => $cart_total,
- 'cart_currency' => $cart_currency,
- 'current_time' => $current_time,
- 'session_id' => $session_id,
- 'cart_contents' => $cart_contents
- );
- }
Advertisement
Comments
-
- /woo-save-abandoned-carts-pro/public/class-cartbounty-pro-public.php
Add Comment
Please, Sign In to add comment
Advertisement