Advertisement
nshelper

WP GlobalCart - woo-save-abandoned-carts-pro

Aug 30th, 2024 (edited)
93
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.95 KB | None | 0 0
  1.     function read_cart(){
  2.  
  3.         if( !WC()->cart ) return; //Exit if Woocommerce cart has not been initialized
  4.  
  5.         //Retrieving cart total value and currency
  6.         $admin = new CartBounty_Pro_Admin( CARTBOUNTY_PRO_PLUGIN_NAME_SLUG, CARTBOUNTY_PRO_VERSION_NUMBER );
  7.         $translatepress = new CartBounty_Pro_TranslatePress();
  8.         $cart_total = WC()->cart->total;
  9.         $cart_currency = get_woocommerce_currency();
  10.         $current_time = current_time( 'mysql', false ); //Retrieving current time
  11.         $session_id = WC()->session->get( 'cartbounty_pro_session_id' ); //Check if the session is already set
  12.  
  13.         if( empty( $session_id ) ){ //If session value does not exist - set one now
  14.             $session_id = WC()->session->get_customer_id(); //Retrieving customer ID from WooCommerce sessions variable
  15.         }
  16.  
  17.         if( WC()->session->get( 'cartbounty_pro_from_link' ) && WC()->session->get( 'cartbounty_pro_session_id' ) ){ //In case users returns from abandoned cart email link
  18.             $session_id = WC()->session->get( 'cartbounty_pro_session_id' );
  19.         }
  20.  
  21.         //Retrieving cart
  22.         $products = WC()->cart->get_cart_contents();
  23.         $product_array = array();
  24.                
  25.         foreach( $products as $key => $product ){
  26.             do_action( 'woocommerce/cart_loop/start', $product );
  27.             $item = wc_get_product( $product['data']->get_id() );
  28.             $product_title = strip_tags( $item->get_title() );
  29.             $product_title = $translatepress->maybe_translate_trp( $product_title );
  30.             $product_quantity = $product['quantity'];
  31.             $product_variation_price = '';
  32.             $product_tax = '';
  33.             $variation_attributes = array();
  34.             $product_variation_id = '';
  35.             $product_attributes = '';
  36.  
  37.             if( isset( $product['line_total'] ) ){
  38.                 $product_variation_price = $product['line_total'];
  39.             }
  40.  
  41.             if( isset( $product['line_tax'] ) ){ //If we have taxes, add them to the price
  42.                 $product_tax = $product['line_tax'];
  43.             }
  44.  
  45.             //Handling product variations
  46.             if( isset( $product['variation'] ) ){
  47.                
  48.                 if( is_array( $product['variation'] ) ){
  49.                     foreach( $product['variation'] as $key => $variation ){
  50.                         $variation_attributes[$key] = $variation;
  51.                     }
  52.                 }
  53.             }
  54.  
  55.             if( isset( $product['variation_id'] ) ){ //If user has chosen a variation
  56.                 $product_variation_id = $product['variation_id'];
  57.                 $product_attributes = $this->get_attribute_names( $variation_attributes, $product['product_id'] );
  58.             }
  59.  
  60.             $product_data = array(
  61.                 'product_title'                 => $product_title . $product_attributes,
  62.                 'quantity'                      => $product_quantity,
  63.                 'product_id'                    => $product['product_id'],
  64.                 'product_variation_id'          => $product_variation_id,
  65.                 'product_variation_price'       => $product_variation_price,
  66.                 'product_variation_attributes'  => $variation_attributes,
  67.                 'product_tax'                   => $product_tax
  68.             );
  69.  
  70.             //Handle WooCommerce Product Bundles plugin data
  71.             if( class_exists( 'WC_Bundles' ) ){ //If WooCommerce Product Bundles plugin active
  72.                
  73.                 if( !wc_pb_is_bundled_cart_item( $product ) ){ //If cart item is not part of a bundle (looking to find only bundle parent item)
  74.                    
  75.                     if( isset( $product['stamp'] ) ){ //If bundle parent has data about bundled items
  76.                         $bundled_item_data = $product['stamp'];
  77.                         $product_data['bundle_data'] = $bundled_item_data;
  78.                         $bundle_price = 0;
  79.                         $bundle_tax = 0;
  80.                         $bundled_cart_items = wc_pb_get_bundled_cart_items( $product );
  81.  
  82.                         //Looping through bundle sub-items to get price of the main bundle item
  83.                         foreach ( $bundled_cart_items as $key => $bundled_cart_item ) {
  84.  
  85.                             if( isset( $bundled_cart_item['line_total'] ) ){
  86.                                 $bundled_cart_item_price = $bundled_cart_item['line_total'];
  87.                             }
  88.  
  89.                             if( isset( $bundled_cart_item['line_tax'] ) ){ //If we have taxes, add them to the price
  90.                                 $bundled_cart_item_tax = $bundled_cart_item['line_tax'];
  91.                             }
  92.  
  93.                             $bundle_price = $bundle_price + $bundled_cart_item_price;
  94.                             $bundle_tax = $bundle_tax + $bundled_cart_item_tax;
  95.                         }
  96.  
  97.                         $product_data['product_variation_price'] = $product_variation_price + $bundle_price;
  98.                         $product_data['product_tax'] = $product_tax + $bundle_tax;
  99.                     }
  100.  
  101.                 }else{ //Skip adding individual bundle sub-items to the cart
  102.                     do_action( 'woocommerce/cart_loop/end', $product );
  103.                     continue;
  104.                 }
  105.             }
  106.  
  107.             //Skip adding bundled sub-items.
  108.             //Added for YITH Product Bundles because cart restoring creates incorrect pricing
  109.             if( isset( $product['bundled_by'] ) ){
  110.                 do_action( 'woocommerce/cart_loop/end', $product );
  111.                 continue;
  112.             }
  113.  
  114.             $product_array[] = $product_data;
  115.            
  116.             do_action( 'woocommerce/cart_loop/end', $product );
  117.         }
  118.  
  119.         $cart_contents = array(
  120.             'products' => $product_array,
  121.             'cart_data' => WC()->cart->get_cart_contents()
  122.         );
  123.  
  124.         return $results_array = array(
  125.             'cart_total'    => $cart_total,
  126.             'cart_currency' => $cart_currency,
  127.             'current_time'  => $current_time,
  128.             'session_id'    => $session_id,
  129.             'cart_contents' => $cart_contents
  130.         );
  131.     }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement