0

I have create a custom taxonomy called regions with acf. and have create also a select option on dokan multivendor dashboard where they can create new product

<?php
$regions = get_terms([
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'taxonomy' => 'region'
]);
?>
    <label for="regions"><?php esc_html_e('Region', 'your-text-domain'); ?></label><br>
    <input type="hidden" name="region_id" id="region_id" value="">
    <select name="regions" class="dokan-form-control">
        <option value=""><?php esc_html_e('Select Region', 'your-text-domain'); ?></option>
        <?php foreach ($regions as $region) {
        $parent_categories = get_terms([
            'taxonomy' => 'region',
            'hide_empty' => false,
            'parent' => $region->term_id
        ]);
        if (!empty($parent_categories)) { // check if there are any child categories
            ?>
            <?php foreach ($parent_categories as $parent_category) {
                $child_categories = get_terms([
                'taxonomy' => 'region',
                'hide_empty' => false,
                'parent' => $parent_category->term_id
                ]);
                if (!empty($child_categories)) { // check if there are any child categories
                ?>
                <optgroup label="<?php echo $parent_category->name; ?>">
                    <?php foreach ($child_categories as $child_category) { ?>
                    <option value="<?php echo $child_category->term_taxonomy_id; ?>" id="in-region-<?php echo $child_category->term_taxonomy_id; ?>"> <?php echo $child_category->name; ?> </option>
                    <?php } ?>
                </optgroup>
                <?php } ?>
            <?php } ?>
        <?php } ?>
        <?php } ?>
    </select>

and now i try to assign this post id when its created to regions like this

//path: dokan-lit/includes/Dashboard/Templates/Products.php

public function handle_product_add() {
    if ( isset( $postdata['add_product'] ) ) {
            $post_title     = sanitize_text_field( $postdata['post_title'] );
            $post_content   = wp_kses_post( $postdata['post_content'] );
            $post_excerpt   = wp_kses_post( $postdata['post_excerpt'] );
            $featured_image = absint( sanitize_text_field( $postdata['feat_image_id'] ) );
            <!-- i have added this line here to get regions id  -->
            $regionId       = sanitize_text_field($postdata['regions']);
            
            $product_id = wp_insert_post( $post_data );

                
                if ( $product_id ) {
                    
                    // and trying to assign this product id to region id like this.
                    wp_set_object_terms($product_id, $regionId, 'region');

}

But i see this is not working. Any idea how to fix this problem?

3
  • have you confirmed that wp_set_object_terms is running and if so how? Can you confirm that wp_insert_post worked and that the $product_id variable is not empty/null/false? Where does $postdata come from? It seems to pop out of thing air, my initial expectation is that if ( isset( $postdata['add_product'] ) ) { will never be true because $postdata doesn't exist
    – Tom J Nowell
    May 15, 2023 at 15:56
  • Thanks for replying. $postdata = wp_unslash( $_POST ); this row is above my code. I am not posting all code couse is a long function. But also i cant confirm if wp_insert_post or $product_id couse i am just editing this plugin but wihle trying to understand code, i thinks is okay couse inside if ($product_id) have also another functions who use ` wp_set_object_terms( $product_id, $cat_ids, 'product_cat' );`
    – tilamap365
    May 16, 2023 at 7:47
  • if there are missing parts from handle_product_add can you ensure that enough of it is present that we don't have mystery variables, and that there are comments stating that code is missing? a .... on its own line is usually enough to indicate parts are missing, and in this case it's important to know, especially since people can and do ask questions like this were $postdata is undefined
    – Tom J Nowell
    May 16, 2023 at 8:40

1 Answer 1

0

I can see few potential issues here:

  1. wp_insert_post returns post_id or 0 or WP_Error, if you will have WP_Error it will still pass the if ( $porduct_id ). So here you have one potential issue.

  2. In description you wrote, that you create fields called "regions", and in code you have wp_set_object_terms($product_id, $regionId, 'region'); so here we have "region" no "regions". Not sure if you misspelled in the code or in the description.

  3. Last thing is here: <option value="<?php echo $child_category->term_taxonomy_id; ?>" probably it should be $child_category->term_id

Hope any of this will point you to the issue.

3
  • Thanks for replying. i just changed the$child_category->term_id but no difference. Also i try both names region and regions but still no difference
    – tilamap365
    May 16, 2023 at 7:54
  • wp_insert_post returns post_id or 0 or WP_Error, if you will have WP_Error it will still pass the if ( $porduct_id ). So here you have one potential issue. i does not understand very good what this mean. I also am beginer on wp development
    – tilamap365
    May 16, 2023 at 7:59
  • WP_Error is an error object with an error message inside, if there is a problem then the $product_id variable will not contain the product ID, it will contain an error object, which you do not check for. Since if ( $product_id ) is checking " is this a thing or is it false/null?" WP_Error objects are a thing so it succeeds when it should not. You should look up WP_Error and how to handle it, how to grab the message inside it, etc
    – Tom J Nowell
    May 16, 2023 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.