Support » Plugin: WooCommerce » How to call code when adding WooCommerce menu items via woocommerce_account_menu

  • Resolved AndreSC

    (@andresc)


    I’m inserting menu items similar to the code example at https://wordpress.stackexchange.com/a/277080/137417, but I haven’t encountered endpoints before and the documentation and tutorial I’ve looked at seem a bit beyond… Presumably, there must be a way to specify a function or something to generate the output? Or somewhere to hook into. (Or does it have to be via a template file in the theme?)

    I thought maybe i should create it as a query var:

    add_filter('query_vars', 'add_query_vars');
    function add_query_vars($vars) {
        $vars[] = "2nd-item";
        return $vars;
    }

    but I’m still staring at a good old error404 when i click the link and call

    …://site.com/shop/my-account/2nd-item/

    Any direction much appreciated, t.i.a.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Thread Starter AndreSC

    (@andresc)

    ah,
    <strong>add_action('woocommerce_account_' . $endpoint . '_endpoint'</strong>, 'my_endpoint_content');
    (nice)

    <?php
    
    add_filter('woocommerce_account_menu_items', 'add_my_menu_items');
    
    function add_my_menu_items($items) {
        $my_items = array('2nd-item' => __('2nd Item', 'artithmeric'),);
        $my_items = array_slice($items, 0, 1, true) +
                $my_items +
                array_slice($items, 1, count($items), true);
        return $my_items;
    }
    
    //so, for...
    $endpoint = '2nd-item';
    
    add_action('init', 'my_custom_endpoint');
    
    function my_custom_endpoint() {
        add_rewrite_endpoint('2nd-item', EP_ROOT | EP_PAGES);
    }
    
    add_action('woocommerce_account_' . $endpoint . '_endpoint', 'my_endpoint_content');
    
    function my_endpoint_content() {
        //content goes here
        echo 'My content goes here';
    }
    
    add_filter('query_vars', 'my_custom_query_vars', 0);
    
    function my_custom_query_vars($vars) {
        $vars[] = '2nd-item';
        return $vars;
    }
    
    add_action('wp_loaded', 'my_custom_flush_rewrite_rules');
    
    function my_custom_flush_rewrite_rules() {
        flush_rewrite_rules();
    }
    
Viewing 1 replies (of 1 total)
  • The topic ‘How to call code when adding WooCommerce menu items via woocommerce_account_menu’ is closed to new replies.