0

i have wordpress with woocommerce + dokan marketplace plugin. in dokan shop settings i would add a category dropdown field. in my function.php:

<?php
add_filter( 'dokan_settings_form_bottom', 'extra_fields_cat', 10, 2);
function extra_fields_cat( $current_user, $profile_info ){
$cat_shop= isset( $profile_info['cat_shop'] ) ? $profile_info['cat_shop'] : ''; ?>
<div>
<?php (isset($_POST["category1"])) ? $company = $_POST["category1"] : $company=1; ?>
<select name="cat_shop" id="reg_cat_shop" class="dokan-form-control input-md valid" >
<option <?php if ($cat_shop == 'category1' ) echo 'selected' ; ?> value="category1">category1</option>
<option <?php if ($cat_shop == 'category2' ) echo 'selected' ; ?> value="category2">category2</option>
</select></div>
<?php }
//save the field value
add_action( 'dokan_store_profile_saved', 'save_extra_fields_cat', 15 );
function save_extra_fields_cat( $store_id ) {
$dokan_settings = dokan_get_store_info($store_id);
if ( isset( $_POST['cat_shop'] ) ) {
$dokan_settings['cat_shop'] = $_POST['cat_shop'];
}
update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}

this example works correctly, but i want populate this field with a woocommerce categories. is it possible in some way? thanks!!

0

ok, solution:

$args = array(
    'order'      => 'ASC',
    'hide_empty' => $hide_empty,
    'include'    => $ids,
    'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
 (isset($_POST["Cianfrusaglie"])) ? $company = $_POST["Cianfrusaglie"] : $company=1;

echo "<select name='cat_shop' id='reg_cat_shop' class='dokan-form-control input-md valid'>";
foreach( $product_categories as $category ){
    echo "<option "; ?><?php if ($cat_shop == esc_html( $category->name ) ) echo 'selected' ; ?>
    <?php echo "value = '" . esc_attr( $category->name ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";

Now, in my woocommerce new product page, how i can select my category?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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