Cannot get custom attribute to work in menu

I’ve been searching and using the Woocommerce documentation, but cannot get a custom attribute in my menu. I used the following code:

add_filter('woocommerce_attribute_show_in_nav_menus', 'wc_reg_for_menus', 1, 2);

function wc_reg_for_menus( $register, $name = '' ) {
     if ( $name == 'pa_merk' ) $register = true;
     return $register;
}

I am unable to select this custom attribute in my menu manager in WordPress. Can anyone help?

Related posts

Leave a Reply

1 comment

  1. I had similar issue.

    Problem:

    Created a custom field under the ‘General’ tab using the guide below

    http://www.remicorson.com/mastering-woocommerce-products-custom-fields/

    Couldn’t reference the custom field and make it a menu option. This was due to there being no associated archive template so output could not be rendered.

    Solution:

    The key to this working is creating an attribute with ‘Enable Archives?’ being checked. This associates the attribute with the archive template.

    The Archives page template displays all posts in chronological order
    in a compact, paginated format and cannot be separated by category

    http://kb.oboxthemes.com/themedocs/store-content-archives/

    WordPress archive index

    http://codex.wordpress.org/Creating_an_Archive_Index

    Its an explicit, multi-step process so bear with me.

    Overview:

    • Create an attribute and options
    • Add your attribute to you product
    • Add the necessary code to functions.php
    • Create a new menu
    • Add a menu item
    • Add attribute options to parent menu item
    • Create custom archive template

    To start with I removed my custom field as it’s not going to work as intended.

    Create an attribute and options

    • Go to Products > Attributes and create your attribute e.g. ‘Sizes’ with the slug ‘sizes’
    • Check ‘Enable Archives?’ (this is the key to making this work)
    • Add size attribute options ‘Large’, ‘Medium’ & ‘Small’

    Add the attribute to your product

    • Products > Attributes (tab) > Custom attribute (dropdown ‘Size’) > Add
    • Once added click on ‘Sizes’ which is a dropdown > click on the ‘Values’ field > select your option > click ‘Save attributes’

    Add the necessary code to functions.php

    In your (child) functions.php add your add_filter() method and function as above referencing your attribute slug ‘sizes’ with ‘pa_sizes’ (prefix with pa_)

    http://docs.woothemes.com/document/using-custom-attributes-in-menus/

    Create a new menu

    • Go to Appearance > Menus
    • Click on options. ‘Sizes’ should be in there and checked
    • Also make sure ‘Links’ option is also checked

    Add a menu item

    • In the ‘Menus’ sidebar click on ‘Links’.
    • In the URL field enter ‘#’
    • In ‘link text’ field enter add the label name ‘Sizes’. This gives you your menu link item

    Add attribute options

    • Click on ‘Sizes’ in the side bar
    • Click on the ‘view all’ > ‘select all’ > click ‘Add to menu’
    • In the ‘Menu’ panel click and drag each attribute option to the right under your menu link ‘Sizes’. They should ‘snap’ into place

    Now refresh your frontend and the menu link ‘Sizes’ should be there with a dropdown of the options.

    Create custom archive template

    Instructions to create a different template per attribute menu item can be found here:

    http://docs.woothemes.com/document/using-custom-attributes-in-menus/

    Hope this helps