Approach for saving a product attribute values with a custom UI in a woocommerce+dokan set up [closed]

Actually, this ended up being straight forward.

Displaying the drop down

I used this line to get the list of terms for my pa_condition taxonomy:

$attr_condition = get_terms(
array(
    'taxonomy' => 'pa_condition', //empty string(''), false, 0 don't work, and return empty array
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => false, //can be 1, '1' too
));

And I used this to get the condition value for the current product (aka post)

$post_condition = get_the_terms($post_id,'pa_condition');

Then it was a simple matter of using a for loop to echo out all the options and setting the appropriate select option for the condition.

Saving the drop down

I first deleted the custom attributes control by making a child theme of the file plugins\dokan-pro\templates\products\product-variations.php and then emptying it of all html content.

Then I made sure I named my select menu and echoed the appropriate html variables in a convention that allows WordPress to save values without any new backend code like this

   <select name="attribute_values[0][0]" class="dokan-form-control">
    <?php
        $val = count($post_condition) > 0 ? $post_condition[0]->slug : "";
        foreach($attr_condition as $item) {
            $selected = $val == $item->slug ? ' selected' : '';
            echo '<option value="'.$item->slug.'"'.$selected.'>'.$item->name.'</option>';
        }
    ?>
    </select>
    <input type="hidden" name="attribute_names[0]" value="pa_condition" />
    <input type="hidden" name="attribute_position[0]" value="0" />
    <input type="hidden" name="attribute_is_taxonomy[0]" value="1" />
    <input type="hidden" name="attribute_visibility[0]" value="1" />

These html variables will create the appropriate $_POST schema to save custom attribute values from my new drop down menu.

Leave a Comment