Created
October 23, 2025 04:12
-
-
Save TanvirHasan19/ce93a2579ac23970d27cee18181a1248 to your computer and use it in GitHub Desktop.
Add vendor header on Elementor single product template.
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
| /** | |
| * Add vendor header on Elementor single product template. | |
| */ | |
| add_action( 'wp_footer', 'wcv_elementor_js_vendor_header' ); | |
| function wcv_elementor_js_vendor_header() { | |
| if ( ! function_exists( 'is_plugin_active' ) ) { | |
| include_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| } | |
| if ( is_product() && is_plugin_active( 'elementor/elementor.php' ) ) { | |
| global $post; | |
| if ( ! $post ) { | |
| return; | |
| } | |
| // WC Vendors vendor id from product | |
| if ( ! class_exists( 'WCV_Vendors' ) ) { | |
| return; | |
| } | |
| $vendor_id = WCV_Vendors::get_vendor_from_product( $post->ID ); | |
| if ( $vendor_id && WCV_Vendors::is_vendor( $vendor_id ) ) { | |
| ?> | |
| <script> | |
| jQuery(document).ready(function($) { | |
| // Give Elementor a moment to finish rendering | |
| setTimeout(function() { | |
| var vendorHeader = <?php echo wp_json_encode( wcv_get_vendor_header_html( $vendor_id ) ); ?>; | |
| if (vendorHeader && $('.elementor-widget-woocommerce-product-data').length) { | |
| $('.elementor-widget-woocommerce-product-data').first().before(vendorHeader); | |
| } else if (vendorHeader && $('.product').length) { | |
| // Fallback: insert near the product container if the widget selector changes | |
| $('.product').first().prepend(vendorHeader); | |
| } | |
| }, 800); | |
| }); | |
| </script> | |
| <?php | |
| } | |
| } | |
| } | |
| /** | |
| * Render the WC Vendors store header HTML using the active store header template. | |
| */ | |
| function wcv_get_vendor_header_html( $vendor_id ) { | |
| if ( ! $vendor_id || ! class_exists( 'WCV_Vendors' ) || ! WCV_Vendors::is_vendor( $vendor_id ) ) { | |
| return ''; | |
| } | |
| // Collect vendor meta | |
| $vendor_meta = array_map( | |
| function( $a ) { return is_array( $a ) && isset( $a[0] ) ? $a[0] : $a; }, | |
| get_user_meta( $vendor_id ) | |
| ); | |
| // Choose the header template (modern or default depending on your setup) | |
| $template = function_exists( 'wcv_get_store_header_template' ) | |
| ? wcv_get_store_header_template() | |
| : 'store/store-header.php'; | |
| ob_start(); | |
| wc_get_template( | |
| $template, | |
| array( | |
| 'vendor_id' => $vendor_id, | |
| 'vendor_meta' => $vendor_meta, | |
| 'product' => wc_get_product( get_the_ID() ), | |
| 'post' => get_post(), | |
| ), | |
| 'wc-vendors/', | |
| defined( 'WCV_PRO_ABSPATH_TEMPLATES' ) ? WCV_PRO_ABSPATH_TEMPLATES : WP_PLUGIN_DIR . '/wc-vendors-pro/templates/' | |
| ); | |
| return ob_get_clean(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment