Last active
December 7, 2023 09:10
-
-
Save SirDarcanos/19908ee6dd56c75d263d6b1dded4b9e9 to your computer and use it in GitHub Desktop.
Add an Enquiry Form to The My Account Page in WooCommerce
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
<?php | |
class WC_Custom_My_Account_Tabs extends WC_Query { | |
/** | |
* Adds main filters and actions and inits the endpoints. | |
*/ | |
public function __construct() { | |
add_action( 'init', array( $this, 'add_endpoints' ) ); | |
if ( ! is_admin() ) { | |
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 ); | |
add_filter( 'woocommerce_account_menu_items', array( $this, 'edit_navigation' ) ); | |
add_action( 'woocommerce_account_contact-us_endpoint', array( $this, 'add_custom_tab_content' ) ); | |
} | |
$this->init_query_vars(); | |
} | |
/** | |
* Inits the query vars for WooCommerce | |
*/ | |
public function init_query_vars() { | |
$this->query_vars = array( | |
'contact-us' => 'contact-us', | |
); | |
} | |
/** | |
* Edits the navigation in the page My Account adding a new Contact Us tab. | |
* | |
* @param array $items The existing tab items. | |
* @return array | |
*/ | |
public function edit_navigation( $items ) { | |
if ( ! isset( $items['contact-us'] ) ) { | |
$last_item = array_splice( $items, -1, 1 ); | |
$contact = array( | |
'contact-us' => esc_html( 'Contact Us' ), | |
); | |
$items = array_merge( $items, $contact, $last_item ); | |
} | |
return $items; | |
} | |
/** | |
* Prints the contact tab content from a template in theme-name/woocommerce/myaccount/ | |
*/ | |
public function add_custom_tab_content() { | |
wc_get_template( 'myaccount/contact.php', array(), '', get_stylesheet() . 'woocommerce/' ); | |
} | |
} | |
new WC_Custom_My_Account_Tabs(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment