Skip to content

Instantly share code, notes, and snippets.

@digitalchild
Created July 23, 2021 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalchild/09e15649425845fef0b8d3af75c79dd1 to your computer and use it in GitHub Desktop.
Save digitalchild/09e15649425845fef0b8d3af75c79dd1 to your computer and use it in GitHub Desktop.
Use WC Vendors Pro form helper to output a custom taxonomy checklist
<?php
// WC Vendors Pro 1.7.10 or above required.
/**
* Taxonomy: location.
*/
function wcv_register_my_location_taxonomy() {
$labels = [
'name' => __( 'Location', 'wcvendors-pro' ),
'singular_name' => __( 'location', 'wcvendors-pro' ),
];
$args = [
'label' => __( 'Location', 'wcvendors-pro' ),
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'location', 'with_front' => true, ],
'show_admin_column' => false,
'show_in_rest' => true,
'rest_base' => 'location',
'rest_controller_class' => 'WP_REST_Terms_Controller',
'show_in_quick_edit' => true,
'show_in_graphql' => false,
'capabilities' => [ 'assign_product_terms' ]
];
register_taxonomy( 'location', [ 'product' ], $args );
}
add_action( 'init', 'wcv_register_my_location_taxonomy' );
// Output the location checklist
function form_location( $object_id ) {
$args = array(
'taxonomy' => 'location',
);
$field = array(
'id' => 'product_loc_list',
'label' => __( 'Location', 'wcvendors-pro' ),
'class' => 'product_cat_checklist',
);
WCVendors_Pro_Form_Helper::wcv_terms_checklist( $object_id, $args, $field );
}
add_action( 'wcv_after_product_details', 'form_location' );
// Save the terms
function save_location( $post_id ){
if ( isset( $_POST[ 'location' ] ) && is_array( $_POST[ 'location' ] ) ) {
$location = array_map( 'intval', $_POST[ 'location' ] );
$location = array_unique( $location );
wp_set_post_terms( $post_id, $location, 'location' );
}
}
add_action( 'wcv_save_product', 'save_location' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment