$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>populating extra field with woocommerce categories|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

populating extra field with woocommerce categories

matteradmin10PV0评论

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!!

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!!

Share Improve this question asked Feb 7, 2019 at 13:27 mrblademrblade 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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?

Post a comment

comment list (0)

  1. No comments so far