Created
November 6, 2025 16:02
-
-
Save TanvirHasan19/0d02b72ecc6b3a066a344b888d0703c9 to your computer and use it in GitHub Desktop.
Add Wholesale Prices to WooCommerce CSV Export
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function wwpp_add_wholesale_price_export_columns( $columns ) { | |
| // Check if wholesale prices plugin is active | |
| if ( ! class_exists( 'WWP_Wholesale_Prices' ) ) { | |
| return $columns; | |
| } | |
| // ============================================ | |
| // CUSTOMIZE THIS: Set your wholesale role key | |
| // ============================================ | |
| $wholesale_role = 'wholesale_customer'; // Change this to your wholesale role key | |
| // Get role display name for column header | |
| $role_display_name = 'Wholesale Customer'; // Default | |
| if ( class_exists( 'WWP_Wholesale_Roles' ) ) { | |
| $wholesale_roles = WWP_Wholesale_Roles::getInstance(); | |
| $all_roles = $wholesale_roles->getAllRegisteredWholesaleRoles(); | |
| if ( isset( $all_roles[ $wholesale_role ] ) ) { | |
| $role_display_name = $all_roles[ $wholesale_role ]['roleName']; | |
| } | |
| } | |
| // Add wholesale price columns | |
| $columns['wholesale_price'] = sprintf( __( 'Wholesale Price (%s)', 'woocommerce-wholesale-prices-premium' ), $role_display_name ); | |
| $columns['wholesale_price_source'] = sprintf( __( 'Wholesale Price Source (%s)', 'woocommerce-wholesale-prices-premium' ), $role_display_name ); | |
| $columns['wholesale_price_with_tax'] = sprintf( __( 'Wholesale Price with Tax (%s)', 'woocommerce-wholesale-prices-premium' ), $role_display_name ); | |
| $columns['wholesale_price_without_tax'] = sprintf( __( 'Wholesale Price without Tax (%s)', 'woocommerce-wholesale-prices-premium' ), $role_display_name ); | |
| return $columns; | |
| } | |
| /** | |
| * Add wholesale price data to CSV export rows | |
| * | |
| * @param array $row Row data | |
| * @param WC_Product $product Product object | |
| * @param WC_Product_CSV_Exporter $exporter Exporter instance | |
| * @return array Modified row data | |
| */ | |
| function wwpp_add_wholesale_price_to_csv_export( $row, $product, $exporter ) { | |
| // Check if wholesale prices plugin is active | |
| if ( ! class_exists( 'WWP_Wholesale_Prices' ) ) { | |
| return $row; | |
| } | |
| // ============================================ | |
| // CUSTOMIZE THIS: Set your wholesale role key (must match the one above) | |
| // ============================================ | |
| $wholesale_role = 'wholesale_customer'; // Change this to your wholesale role key | |
| // Get product ID (handle variations) | |
| $product_id = $product->get_id(); | |
| // Get wholesale price (this automatically includes category-level pricing) | |
| $wholesale_price_data = WWP_Wholesale_Prices::get_product_wholesale_price_on_shop_v3( | |
| $product_id, | |
| array( $wholesale_role ) | |
| ); | |
| // Add wholesale price data to row | |
| if ( ! empty( $wholesale_price_data['wholesale_price_raw'] ) ) { | |
| // Raw wholesale price (without tax) - Most commonly used | |
| $row['wholesale_price'] = $wholesale_price_data['wholesale_price_raw']; | |
| // Price source (per_product_level, product_category_level, etc.) | |
| $row['wholesale_price_source'] = isset( $wholesale_price_data['source'] ) | |
| ? $wholesale_price_data['source'] | |
| : 'unknown'; | |
| // Wholesale price with tax | |
| $row['wholesale_price_with_tax'] = isset( $wholesale_price_data['wholesale_price_with_tax'] ) | |
| ? $wholesale_price_data['wholesale_price_with_tax'] | |
| : $wholesale_price_data['wholesale_price_raw']; | |
| // Wholesale price without tax | |
| $row['wholesale_price_without_tax'] = isset( $wholesale_price_data['wholesale_price_with_no_tax'] ) | |
| ? $wholesale_price_data['wholesale_price_with_no_tax'] | |
| : $wholesale_price_data['wholesale_price_raw']; | |
| } else { | |
| // No wholesale price available - set empty values | |
| $row['wholesale_price'] = ''; | |
| $row['wholesale_price_source'] = ''; | |
| $row['wholesale_price_with_tax'] = ''; | |
| $row['wholesale_price_without_tax'] = ''; | |
| } | |
| return $row; | |
| } | |
| // Hook into CSV export column names | |
| add_filter( 'woocommerce_product_export_column_names', 'wwpp_add_wholesale_price_export_columns', 10, 1 ); | |
| // Hook into CSV export row data | |
| add_filter( 'woocommerce_product_export_row_data', 'wwpp_add_wholesale_price_to_csv_export', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment