Skip to content

Instantly share code, notes, and snippets.

@nayemDevs
Created January 24, 2017 04:35
Show Gist options
  • Save nayemDevs/56a04b3d72bb4b23c3ae5976352dc841 to your computer and use it in GitHub Desktop.
Save nayemDevs/56a04b3d72bb4b23c3ae5976352dc841 to your computer and use it in GitHub Desktop.
Load category in ajax
add_action( 'wp_ajax_dokan_fetch_product_child_category', 'dokan_fetch_product_child_category' );
function dokan_fetch_product_child_category() {
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'marketica-child-theme-nonce' ) ) {
wp_send_json_error( __( 'Nonce Verfication faild', 'dokan' ) );
}
$category_id = ( isset( $_POST['category'] ) && ! empty( $_POST['category'] ) ) ? $_POST['category'] : '';
if ( empty( $category_id ) ) {
wp_send_json_error( 'No Category Selected', 'dokan' );
}
$categories = get_terms( 'product_cat', array(
'orderby' => 'count',
'hide_empty' => 0,
'parent' => $category_id,
) );
if ( empty( $categories ) ) {
wp_send_json_error(__( 'No Category Found', 'dokan' ) );
}
ob_start();
?>
<div class="dokan-category-wrapper" style="margin-top:20px">
<select class="dokan_product_category dokan-form-control chosen" name="product_cat[]" id="product_cat" style="height: 26px; margin-top: 20px;" data-placeholder="<?php _e('Select a category', 'dokan' ); ?>">
<option value=""></option>
<?php foreach ( $categories as $key => $category ) : ?>
<option value="<?php echo $category->term_id ?>"><?php echo $category->name; ?></option>
<?php endforeach ?>
</select>
</div>
<?php
$content = ob_get_clean();
wp_send_json_success( $content );
}
// add_action( 'dokan_product_updated', 'dokan_map_category_ids_save', 59 );
add_action( 'dokan_process_product_meta', 'dokan_map_category_ids_save', 59 );
add_action( 'dokan_new_product_added', 'dokan_map_category_ids_save', 59 );
function dokan_map_category_ids_save( $product_id ) {
if ( ! is_user_logged_in() ) {
return;
}
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( isset( $_POST['product_cat'] ) ) {
update_post_meta( $product_id, '_categroy_hirerchy_ids', $_POST['product_cat'] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment