php - i want add conditional for add category in wordpress

admin2025-06-03  8

i have marketplace plugin when the seller want add product first he will choose category i want change this code to for example i have 2 main category ( A - B ) and i have ( C - D - E ) under A and i have ( F - G - H ) under B this my code

    <td>

                                        <label for="mp_seller_product_categories">Produkt kategorier</label>

                                </td>

                                <td>

                                        <?php

                                        $allowed_cat = get_user_meta(get_current_user_id(), 'wkmp_seller_allowed_categories', true);

                                        if (! $allowed_cat) {
                                                $allowed_categories = get_option('wkmp_seller_allowed_categories');
                                        } else {
                                                $allowed_categories = $allowed_cat;
                                        }

                                        require 'class-taxonomy-filter.php';

                                        $product_categories = wp_dropdown_categories(array(
                                                'show_option_none' => __( '', 'marketplace' ),
                                                'hierarchical'     => 1,
                                                'hide_empty'       => 0,
                                                'name'             => 'product_cate[]',
                                                'id'               => 'mp_seller_product_categories',
                                                'taxonomy'         => 'product_cat',
                                                'title_li'         => '',
                                                'orderby'                  => 'name',
                                                'order'                      => 'ASC',
                                                'class'            => '',
                                                'exclude'          => '',
                                                'selected'         => array(),
                                                'echo'             => 0,
                                                'value_field'        => 'slug',
                                                'walker'                     => new MpProductCategoryTree($allowed_categories)
                                        ) );

                                        echo str_replace( '<select', '<select style="width: 50%;" data-placeholder="'.__( 'Choose category(s)', 'marketplace' ).'" multiple="multiple" ', $product_categories );

                                        ?>

                                </td>

i want change to

first seller will choose A or B if choose A he can choose ( C or D or E ) else he choose B he can Choose ( F or G or H )

and i don't want the code take category from wordpress system i want write the category in code

i have marketplace plugin when the seller want add product first he will choose category i want change this code to for example i have 2 main category ( A - B ) and i have ( C - D - E ) under A and i have ( F - G - H ) under B this my code

    <td>

                                        <label for="mp_seller_product_categories">Produkt kategorier</label>

                                </td>

                                <td>

                                        <?php

                                        $allowed_cat = get_user_meta(get_current_user_id(), 'wkmp_seller_allowed_categories', true);

                                        if (! $allowed_cat) {
                                                $allowed_categories = get_option('wkmp_seller_allowed_categories');
                                        } else {
                                                $allowed_categories = $allowed_cat;
                                        }

                                        require 'class-taxonomy-filter.php';

                                        $product_categories = wp_dropdown_categories(array(
                                                'show_option_none' => __( '', 'marketplace' ),
                                                'hierarchical'     => 1,
                                                'hide_empty'       => 0,
                                                'name'             => 'product_cate[]',
                                                'id'               => 'mp_seller_product_categories',
                                                'taxonomy'         => 'product_cat',
                                                'title_li'         => '',
                                                'orderby'                  => 'name',
                                                'order'                      => 'ASC',
                                                'class'            => '',
                                                'exclude'          => '',
                                                'selected'         => array(),
                                                'echo'             => 0,
                                                'value_field'        => 'slug',
                                                'walker'                     => new MpProductCategoryTree($allowed_categories)
                                        ) );

                                        echo str_replace( '<select', '<select style="width: 50%;" data-placeholder="'.__( 'Choose category(s)', 'marketplace' ).'" multiple="multiple" ', $product_categories );

                                        ?>

                                </td>

i want change to

first seller will choose A or B if choose A he can choose ( C or D or E ) else he choose B he can Choose ( F or G or H )

and i don't want the code take category from wordpress system i want write the category in code

Share Improve this question edited Feb 15, 2019 at 11:33 user141080 7984 silver badges11 bronze badges asked Feb 15, 2019 at 10:07 Sam GhaliSam Ghali 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

If I understood your question correctly, then something like this might do the trick.

// html markup for the selects
<select name="main_cat">
  <option value="A">A</option>
  <option value="B">B</option>
</select>

<select name="sub_cat_for_A" class="sub-cat-select hidden">
    <option value="C">C</option>
    <option value="D">D</option>
    <option value="E">E</option>
</select>

<select name="sub_cat_for_B" class="sub-cat-select hidden">
    <option value="F">F</option>
    <option value="G">G</option>
    <option value="H">H</option>
</select>

If you change your mind and want to use a taxonomy from WordPress, you can change the markup to this,

<?php
$main_categories = array();
$sub_categories = array();
$cats = get_terms(array(
  'taxonomy' => 'your_taxonomy',
));

if ( $cats && ! is_wp_error( $cats ) ) {
  foreach ( $cats as $cat ) {
    if ( $cat->parent ) {
      $sub_categories[$cat->parent][$cat->term_id] = $cat->name;
    } else {
      $main_categories[$cat->term_id] = $cat->name;
    }
  }
}
?>

<select name="main_cat">
  <?php foreach ( $main_categories as $id => $name ) : ?>
    <option value="<?php echo $id; ?>"><?php echo $name; ?></option>
  <?php endforeach; ?>
</select>

<?php foreach ( $sub_categories as $main_cat_id => $sub_category_options ) : ?>
  <select name="<?php echo "sub_cat_for_{$main_cat_id}" ?>" class="sub-cat-select hidden">
    <?php foreach ( $sub_category_options as $id => $name ) : ?>
      <option value="<?php echo $id; ?>"><?php echo $name; ?></option>
    <?php endforeach; ?>
  </select>
<?php endforeach; ?>

Then you just need some jQuery (vanilla javascript could be used also) to watch changes on the main select and to show the related sub-select.

jQuery(document).on('ready',function($){
  $('select[name="main_cat"]').on('change',function(event){
    var selectedMainCatId = $(this).value();
    // hide other sub cat selects
    $('.sub-cat-select').addClass('hidden');
    // show correct sub cat select
    $('select[name="sub_cat_for_'+selectedMainCatId+'"]').removeClass('hidden');
  });  
});

There might be room for improvement on the jQuery example, but I'm sure you'll get the basic principle from it.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748901182a314665.html

最新回复(0)