Okay
  Public Ticket #3421761
Saving Custom Taxonomy From Vendor Dashboard
Closed

Comments

  •  3
    Peter Jenkins started the conversation

    Hi there! 

    I have added a few custom taxonomies to the seller dashboard using the following code, added to the edit-product file : 

    <!-- CROSS CURRICULAR -->
    <div class="col-xxl-3 col-md-6 marketking_card_gal_cat_tags">
        <div class="code-block marketking_cattag_card">
            <h6 class="overline-title title">
                <?php esc_html_e('Does your resource include any cross curricular topics?','marketking-multivendor-marketplace-for-woocommerce');?>
            </h6>
            <div class="form-group">
                <div class="form-control-wrap">
                    <?php
                    // Modify the arguments as needed for your custom taxonomy 'cross_curricular'
                    $cross_curricular_args = array(
                        'taxonomy' => 'cross_curricular',
                        'name'     => 'marketking_select_cross_curricular[]', // Modify the name to allow multiple choices
                        'class'    => 'form-select',
                        'orderby'  => 'name',
                        'title_li' => '',
                        'multiple' => 'multiple', // Enable multiple choices
                    );

                    wp_dropdown_categories($cross_curricular_args);
                    ?>
                </div>
            </div>
        </div>
    </div>

    It works perfectly - showing a drop-down checkbox in the vendor dashboard. 

    The problem is it isn't saving these custom taxonomies to the related product. When they click "Save Product" it saves the category and featured image fine - but it doesn't save any custom taxonomies. 

    The custom taxonomies work fine in the back end though, it's just when they're saved through the edit-product page. 

    Thanks! 


  •  2,131
    WebWizards replied

    Hi Peter,


    When adding code there to display custom fields such as custom taxonomies, it is sometimes (depending on the code) also needed to save the data, by hooking into MarketKing's hooks after saving the product.

    Please try adding this PHP code to your site:

    add_action('marketking_after_save_product', function($product_id, $vendor_id){
        $values = $_POST['marketking_select_cross_curricular'];
        wp_set_object_terms( $product_id, $values, 'cross_curricular' );
    }, 10, 2);
    

    I think this may be able to do it, but if not it's just a matter of finding the right code to programmatically give those values to the product id.


    Let me know if that can solve it for you,


    Kind regards,

    Stefan

  •  3
    Peter Jenkins replied

    Hi Stefan, 

    Unfortunately it doesn't look like it will be that easy - when I try and save a product with that code in place it doesn't "save" at all - it just comes back with : 

    bundle.js?ver=6.2.2:1     POST https://schoolsmusicmarket.com/wp-admin/admin-ajax.php 500

    Error inside console. 

    Thanks, 

    Pete

  •  2,131
    WebWizards replied

    Would it be possible to share a backend login to the site or a staging clone site with us?

    I could look into it directly and try to get this working.

  •   Peter Jenkins replied privately
  •  2,131
    WebWizards replied

    I got that to work by making a change to the snippet and to the edit-product.php template.


    Now when I save the product, I can see the elements if I go to https://schoolsmusicmarket.com/wp-admin/post.php?post=232133&action=edit


    The only problem is that the code you have on edit-product.php does not show the "selected" ones. 

    I think you either need to add the "selected" parameter and set it to an array of values ( see https://wordpress.stackexchange.com/questions/326424/wp-dropdown-categories-not-showing-option-as-selected ) or build the category dropdown in a different way.

  •  3
    Peter Jenkins replied

    Thanks so much for looking into this further - Unfortunately I can't recreate this today for some reason. It still appears to not be saving the cross_curricular taxonomy to the backend. I have tried to recreate this with other custom taxonomies my site uses today as well and can't get it to work for them either (I have just tried duplicating the Code Snippet and changing the taxonomy though - there may be more to it). 

    Would you mind just check the backend again for me and seeing why it doesn't appear to be saving it anymore like you suggested yesterday? 

    That is annoying about the selected paramenter - are you saying that if I changed the drop down's to something else it would work straight away? 

    Thanks so much for your help - I know this is stretching the limits of customer service for this plugin but I really do appreciate the help :) 

    Pete

  •  2,131
    WebWizards replied

    I think I see where the issue is. If you check the HTML of the select dropdown:

    5156297692.png

    You see that the name is marketking_select_cross_curricular[][] - however, it should be marketking_select_cross_curricular[]


    (it should have [] only once to specify it's an array)


    To fix it, I delete your [] in edit-product.php here:

    4583250680.png

    It seems the dropdown function there adds it automatically so it's no longer needed.

    After deleting [] from the name in edit-product.php I believe now it's working.


    I enter

    4484548813.png

    and in the backend I see:

    3985162448.png


  •  3
    Peter Jenkins replied

    Thanks so much! That worked, and I've managed to recreate it for all my other taxonomies in a snippet, so all is good!