Home / eCommerce / Show and Save Custom Taxonomy On the Vendor Product Edit Form
Duplicate Snippet

Embed Snippet on Your Site

Show and Save Custom Taxonomy On the Vendor Product Edit Form

This code snippet is for displaying and saving the custom product taxonomy (e.g., "product_brand" or "sizes") on the vendor product edit form. You can use a plugin like the Advanced Coupons Field to create the product taxonomy.

Code Preview
php
<?php
/* WCV - Show and save product brand taxonomy on the product edit form */
add_action( 'wcv_after_product_details', 'wcv_product_brand_select2' );
/**
 * Add the Brands taxonomy to the vendor product edit form.
 *
 * @param int $object_id The post ID.
 */
function wcv_product_brand_select2( $object_id ) {
	$multiple         = true;
	$exclude          = array(); // Add term IDs to exclude if needed.
	$show_option_none = __( 'Select a brand', 'wcvendors-pro' );
	$post_terms       = get_the_terms( $object_id, 'product_brand' );
	$values           = is_array( $post_terms ) ? wp_list_pluck( $post_terms, 'term_id' ) : array();
	WCVendors_Pro_Form_Helper::select( array(
		'post_id'          => $object_id,
		'id'               => 'product_brand',
		'name'             => 'product_brand[]',
		'taxonomy'         => 'product_brand',
		'class'            => 'category-select2',
		'show_option_none' => $show_option_none,
		'taxonomy_args'    => array(
			'hide_empty' => 0,
			'orderby'    => 'name',
			'exclude'    => $exclude,
		),
		'value'            => $values,
		'label'            => __( 'Brands', 'wcvendors-pro' ),
		'multiple'         => $multiple,
	) );
}
add_action( 'wcv_save_product', 'save_product_brand' );
/**
 * Save the Sizes taxonomy from the vendor product edit form.
 *
 * @param int $post_id The post ID.
 */
function save_product_brand( $post_id ) {
	if ( ! is_user_logged_in() ) return;
	$product_brand = isset( $_POST['product_brand'] ) ? array_map( 'intval', $_POST['product_brand'] ) : array();
	wp_set_post_terms( $post_id, $product_brand, 'product_brand' );
}
/**************************/
/* WCV - Show and save product size taxonomy on the product edit form */
/**
 * Add the Sizes taxonomy to the vendor product edit form.
 */
add_action( 'wcv_after_product_details', 'wcv_product_sizes_select2' );
function wcv_product_sizes_select2( $object_id ) {
	$multiple         = true;
	$exclude          = array(); // You can exclude terms here if needed.
	$show_option_none = __( 'Select a size', 'wcvendors-pro' );
	$post_terms       = get_the_terms( $object_id, 'sizes' );
	$values           = is_array( $post_terms ) ? wp_list_pluck( $post_terms, 'term_id' ) : array();
	WCVendors_Pro_Form_Helper::select( array(
		'post_id'          => $object_id,
		'id'               => 'sizes',
		'name'             => 'sizes[]',
		'taxonomy'         => 'sizes',
		'class'            => 'category-select2',
		'show_option_none' => $show_option_none,
		'taxonomy_args'    => array(
			'hide_empty' => 0,
			'orderby'    => 'name',
			'exclude'    => $exclude,
		),
		'value'            => $values,
		'label'            => __( 'Sizes', 'wcvendors-pro' ),
		'multiple'         => $multiple,
	) );
}
/**
 * Save the Sizes taxonomy from the vendor product edit form.
 */
add_action( 'wcv_save_product', 'save_product_sizes' );
function save_product_sizes( $post_id ) {
	if ( ! is_user_logged_in() ) return;
	$sizes = isset( $_POST['sizes'] ) ? array_map( 'intval', $_POST['sizes'] ) : array();
	wp_set_post_terms( $post_id, $sizes, 'sizes' );
}

Comments

Add a Comment