Skip to content

Instantly share code, notes, and snippets.

@lucasstark
Last active September 2, 2021 13:48
Show Gist options
  • Save lucasstark/c261df0c2051c9320bf2eb2fd79e7f49 to your computer and use it in GitHub Desktop.
Save lucasstark/c261df0c2051c9320bf2eb2fd79e7f49 to your computer and use it in GitHub Desktop.
Modify WooCommece Wishlist's Endpoint Slug
class WC_Wishlists_Account_Endpoint {
private static $instance;
public static function register($endpoint_slug, $endpoint_name) {
if ( self::$instance == null ) {
self::$instance = new WC_Wishlists_Account_Endpoint($endpoint_slug, $endpoint_name);
}
}
protected $endpoint_slug;
protected $endpoint_name;
protected function __construct($endpoint_slug, $endpoint_name) {
$this->endpoint_slug = $endpoint_slug;
$this->endpoint_name = $endpoint_name;
add_action( 'woocommerce_init', array( $this, 'on_init' ), 0 );
add_filter('woocommerce_my_account_my_wishlists_title', array($this, 'on_get_wishlists_title'));
}
public function on_init() {
add_action( 'init', array( $this, 'account_wishlists_endpoints' ) );
add_filter( 'query_vars', array( $this, 'account_wishlists_query_vars' ), 0 );
add_filter( 'woocommerce_account_menu_items', array( $this, 'account_menu_item' ), 99 );
add_action( 'woocommerce_account_' . $this->endpoint_slug . '_endpoint', array(
$this,
'add_lists_to_account_page'
), 99 );
}
/**
* Register new endpoint to use inside My Account page.
*
* @see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
*/
public function account_wishlists_endpoints() {
add_rewrite_endpoint( $this->endpoint_slug, EP_ROOT | EP_PAGES );
}
/**
* Add new query var.
*
* @param array $vars
*
* @return array
*/
function account_wishlists_query_vars( $vars ) {
$vars[] = $this->endpoint_slug;
return $vars;
}
public function account_menu_item( $items ) {
// Remove the logout menu item.
$logout = null;
if ( isset( $items['customer-logout'] ) ) {
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
}
// Insert your custom endpoint.
$items[$this->endpoint_slug] = $this->endpoint_name;
unset( $items['account-wishlists'] );
//$items['account-wishlists'] = apply_filters('woocommerce_wishlists_account_menu_label', __( 'Wishlists', 'wc_wishlist' ));
if ( $logout ) {
// Insert back the logout item.
$items['customer-logout'] = $logout;
}
return $items;
}
public function add_lists_to_account_page() {
woocommerce_wishlists_get_template( 'my-account-lists.php' );
}
public function on_get_wishlists_title($title){
$title = $this->endpoint_name;
return $title;
}
}
// Be sure to re-save permalinks after setting this up.
// WordPress -> Settings -> Permalinks -> Save Changes
WC_Wishlists_Account_Endpoint::register('account-registries', 'Registries');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment