Support

Account

Home Forums ACF PRO Get woocommerce attribute with ACF

Solved

Get woocommerce attribute with ACF

  • Hello,
    I’ve created attributes on a woocommerce website and added custom fields to them : 1 main custom field by attribute.
    For example :

    array (
                    'key' => 'field_56e072dcc4abf',
                    'label' => 'Brand',
                    'name' => 'brand',
                    'type' => 'taxonomy',
                    'instructions' => '',
                    'required' => 1,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'taxonomy' => 'pa_brand',
                    'field_type' => 'select',
                    'allow_null' => 0,
                    'add_term' => 0,
                    'save_terms' => 1,
                    'load_terms' => 1,
                    'return_format' => 'object',
                    'multiple' => 0,
                ),

    and same for pa_year-> year, pa_color-> color…

    I thought this was the right way to allow my client to enter product attributes without the woocommerce attribute tab interface.

    And it seems to work: I select the year, the color, the brand of my product (bikes) and nothing in the woocommerce attribute tab. In my archive pages, products appear with their attributes. The code I use :
    $product_brand = get_the_terms(get_the_ID(), 'pa_brand');
    get the right data.
    And I have as many templates as attributes : taxonomy-pa_brand.php, taxonomy-pa_color.php …

    I thought it was good… until I saw that on product page, Additional information tab doesn’t show because in code, if($product->has_attributes()) get back nothing.

    But I can’t understand something : what do I record ? Not my attributes ? Only custom field linked to the attribute ? And especially, why does it work on archive pages, and what can I do to make it work everywhere ? Is there a method to record attribute (pa_brand) just by selecting my custom field (brand) ?

    Thanks a lot for any answer

  • Hi @wax

    I believe WooCommerce does more than assigning the attributes as taxonomy like what ACF does. They use this attribute to create variations too, so it’s possible that they save the attribute data somewhere else. For further support, could you please ask WooCommerce support instead?

    I hope this makes sense 🙂

  • I’m not exactly sure of the specifics on how WooCommerce stores product attibutes, but I do know that it stores them in a serialized array in a single postmeta field. You’re going to need to figure out how this array is built and you’re going to need to create an acf/save_post filter (https://www.advancedcustomfields.com/resources/acfsave_post/) to take all the ACF fields and build that array and then store it where WooCommerce expects to find it.

    I would start by creating some products with all of the attributes you want to be able to set with ACF and then looking into the database to see where it’s stored and how it is constructed. You’ll be looking for the value with a postmeta key of _product_attributes

  • Ok. And thank you both for answering.
    As I am at 80% of the project, and as I am as good developper as english spoker 😉 I gonna try not to go further but to return to a conventional use of woocommerce attribute. Their interface for attributes is a nonsense, but never mind.

  • A last question before returning back :
    With my method, which is wrong bug, I can assign a brand, a color, a year to my products. I can see them on archive pages. I can see them in admin array (I’ve added column for attributes), I can even see them on custom template archives pages like taxonomy-pa_brand.php.

    So if I override additional-information.php to get my fields instead of attributes list, I got everything I need ?

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Get woocommerce attribute with ACF’ is closed to new replies.