Home / eCommerce / RewardsWP — My Account Rewards Tab
Duplicate Snippet

Embed Snippet on Your Site

RewardsWP — My Account Rewards Tab

Andrew Munro PRO
<10
Code Preview
php
<?php
/**
 * Plugin Name: RewardsWP — My Account Rewards Tab
 * Description: Adds a "My Rewards" tab to WooCommerce My Account that opens the rewards widget.
 * Version: 1.0.0
 * Author: AffiliateWP
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
add_action( 'init', function () {
	if ( ! function_exists( 'am_rewardswp' ) || ! class_exists( 'WooCommerce' ) ) {
		return;
	}
	// Add "My Rewards" menu item before "Log out".
	add_filter( 'woocommerce_account_menu_items', function ( array $items ): array {
		$new = [];
		foreach ( $items as $key => $label ) {
			if ( 'customer-logout' === $key ) {
				$new['rewards'] = esc_html__( 'My Rewards', 'rewardswp' );
			}
			$new[ $key ] = $label;
		}
		return $new;
	} );
	// Point the "Rewards" link to #rewardswp-home so it opens the widget.
	add_filter( 'woocommerce_get_endpoint_url', function ( string $url, string $endpoint ) {
		if ( 'rewards' === $endpoint ) {
			return '#rewardswp-home';
		}
		return $url;
	}, 10, 2 );
}, 20 );

Comments

Add a Comment