Code works in functions.php, but not when using hooks?

Question

I’m trying to hook into the WooCoomerce Accounts page to add custom tabs with a shortcode.

The code works perfectly when placed in the fuctions.php file, creating the tab and loading the shortcode.

However, if I try and hook the code in the tab is generated but rather than loading the shortcode, it takes me to a "this page doesn’t exist" error page.

Am I missing something from the code, or should I be using a function child theme to achieve what I’d like done?

Thanks for your time.

<?php

function has_bought() {
    // Get all customer orders
    $customer_orders = get_posts( array(
        'numberposts' => 1, // one order is enough
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed', // Only orders with "completed" status
        'fields'      => 'ids', // Return Ids "completed"
    ) );

    // return "true" when customer has already at least one order (false if not)
   return count($customer_orders) > 0 ? true : false; 
}


/*
 * Add Link (Tab) to My Account menu
 */
add_filter ( 'woocommerce_account_menu_items', 'pixeljack_canine_support_link', 40 );
function pixeljack_canine_support_link( $menu_links ){
 
    $menu_links = array_slice( $menu_links, 0, 5, true ) 
    + array( 'canine-support' => 'Canine Support' )
    + array_slice( $menu_links, 5, NULL, true );
 
    return $menu_links;
 
}
/*
 * Register Permalink Endpoint
 */
add_action( 'init', 'pixeljack_add_endpoint' );
function pixeljack_add_endpoint() {
 

    add_rewrite_endpoint( 'canine-support', EP_PAGES );
 
}
/*
 * Content for the new page in My Account, woocommerce_account_canine-support_endpoint
 */

add_action( 'woocommerce_account_canine-support_endpoint', 'pixeljack_my_account_endpoint_content' );
function pixeljack_my_account_endpoint_content() {
    
    if ( !has_bought()) {
        echo "Please place your first order to gain access to Canine Nutritionist";
    } else {
    echo do_shortcode( '');
    }
    
}

?>
0
Pixeljack 2 years 2020-12-16T18:10:23-05:00 0 Answers 6 views 0

Leave an answer

Browse
Browse