Support

Account

Home Forums ACF PRO Dynamically grab the category of a product in a hidden field

Solving

Dynamically grab the category of a product in a hidden field

  • I’m building a set of ACF fields to define in wp-admin product add/edit page some product-related fields. I used some conditional logic to display one of two ACF fields based on the chosen value of another ACF field, but basically this “other” field needs to go as it’s more or less obsolete, in that it chooses between two values that will eventually be chosen (one of them) as the category of the product.

    So, the products will need to be assigned to one of two categories: Black or White. For Black I need to show one ACF select box with some choices, and for White, I need to show another select box with its own choices.

    So initially I thought to use an other select box with values Black/White, upon selection of which, the other two select boxes will appear/disappear conditionally. But then the e-shop will have to choose a value between Black/White in my ACF select box, and one of two tax categories again between Black/White terms for the product to get assigned to.

    Now, in ACF’s conditional logic, we have to use an ACF field that will decide on a field’s hide/show state. So I thought to use a hidden ACF field that will automatically be populated, not necessarily store its value, it can be on-the-fly calculated every time, so that it plays its role in the decision-making of which of the other two fields to show.

    So in product edit page, normally the category will already be chosen, so I need to somehow grab that, in order to determine which select box to display.

    Product add page is a bit trickier, as the hidden field will need to be populated (thus resulting in hiding/showing the appropriate other select box, once the admin chooses one of the two categories for the new product.

    I hope all this makes sense and somebody (@hube2 are you around?) can help me with this.

    TIA.

  • To actually make it a little harder, I need to add one more detail. The product_cat categories aren’t obviously Black/White. I used those as a minimum reproducible example. The actual situation is a bit harder, in that there are two top-level categories again let’s say that their values are Black/White, but the user will be selecting one level deeper than that. So the mechanism will need to check the selected category and go one level up to find whether the selected cat has Black or White as its parent…

  • The only way I can think of to do this is using a combination of things ACF fillters, JavaScript and CSS.

    See https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/

    To hide the field you would have to add CSS to the admin, this is the only way to hide a field in ACF and still have the field submitted. The best solution would be a text field. Unless you don’t want it submitted. If this is the case you could set some conditional logic that can never be met.

    You will need to choose a default value for this hidden field and add an acf/prepare_field to set the field value. In this filter you can get the global $post get the catetory of the post and then get the top level category. There are various ways that this can be done. Then use this to set the value of the field. If it is a new post then the category will be empty and you would set a default value.

    With that in place you would create your other fields that are conditional on the value of this hidden field.

    Then you need to add custom JavaScript that will detect when the category is changed and update the value of this hidden field so that it can cause ACF to show the correct fields. I would likely localize the script and include an array of categories with their associated top level categories

    
    array(
      'category' => 'top_level_category',
      'category' => 'top_level_category',
      'category' => 'top_level_category',
      'category' => 'top_level_category',
      'category' => 'top_level_category',
      'category' => 'top_level_category',
      'category' => 'top_level_category',
    )
    

    so that I can use the JS array to look up the value rather than trying to do an AJAX request to get the top level category when a change is made.

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

You must be logged in to reply to this topic.