Get the network Total Sales for a synchronized product
The WooCommerce Global Cart offers a powerful solution for businesses operating multiple online stores within a WordPress Multisite network. By synchronizing products across these shops, merchants can ensure consistency in product offerings while simplifying inventory management. However, one challenge that arises with this setup is the need to accurately track sales for synchronized products across the entire network.
To address this challenge, developers can implement custom functions to retrieve essential data, such as the total number of network sales for a specific product. The provided code snippet demonstrates how to achieve this functionality effectively.
/** * Return the total sales for a synchronized product. * That will count the total_sales meta field for paren/childred products across the network. * * @param mixed $product_id */ function woogc_get_synchronized_product_network_sales( $product_id ) { global $blog_id; $total_sales = 0; $switched_site = FALSE; $WooGC_PS = new WooGC_PS(); if ( $WooGC_PS->is_child_product( $product_id ) ) { list ( $main_product_id, $origin_blog_id ) = $WooGC_PS->child_get_main( $product_id ); switch_to_blog( $origin_blog_id ); $switched_site = TRUE; } else { $main_product_id = $product_id; $origin_blog_id = $blog_id; } $main_product = new WC_Product( $main_product_id ); $total_sales += $main_product->get_total_sales(); $child_products = $WooGC_PS->main_get_children( $main_product_id ); foreach ( $child_products as $child_blog_id ) { $found_product_ID = $WooGC_PS->get_product_synchronized_at_shop( $main_product->get_id(), $origin_blog_id, $child_blog_id ); if ( empty ( $found_product_ID ) ) continue; switch_to_blog( $child_blog_id ); $child_product = new WC_Product( $found_product_ID ); $total_sales += $child_product->get_total_sales(); restore_current_blog(); } if ( $switched_site ) restore_current_blog(); return $total_sales; }
The code can be included within the child theme functions.php or a custom file on /wp-content/mu-plugins/
This function efficiently calculates the total sales for a synchronized product across the entire WordPress Multisite network. It iterates through parent and child products, retrieving sales data from each corresponding shop and aggregating the results.
By integrating such custom functionalities into WooCommerce setups with Global Cart synchronization, businesses can gain valuable insights into their network-wide sales performance. This not only facilitates informed decision-making but also enhances the overall efficiency and effectiveness of e-commerce operations.