Adding Custom Field to Product Category

Add Custom field to woocommerce product for an specific category

Use instead the dedicated Wordpress conditional function has_term() as follow:

add_action('woocommerce_before_add_to_cart_button', 'before_add_to_cart_button_custom_fields');
function before_add_to_cart_button_custom_fields() {
global $post, $product;

// Here define your terms (can be terms Ids, slugs or names)
$terms = array('ballons');

if ( has_term( $terms, 'product_cat' ) ) {
?>
<div class="wdm-custom-fields">
<label><?php _e("Color", "woocommerce"); ?></label>
<select type="text" name="wdm_name">
<option value="blue"><?php _e("Blue", "woocommerce"); ?></option>
<option value="red"><?php _e("Rojo", "woocommerce"); ?></option>
<option value="green"><?php _e("Green", "woocommerce"); ?></option>
</select>
</div>
<div class="clear"></div>
<br>
<?php
}
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

You should replace wdm_name simply by color everywhere in your code

Display custom field from product category after WooCommerce loop product title

To display your new field rather than the default category name in the product loop, you can use:

function action_woocommerce_shop_loop_item_title() {
global $product;

// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Retrieves product term ids for a taxonomy
$product_cats_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' );

echo '<div class="categories">';

// Iterate over
foreach( $product_cats_ids as $product_cats_id ) {
echo '<span class="category">' . get_term_meta( $product_cats_id, 'product_cat_singular', true ) . '</span>';
}

echo '</div>';
}
}
add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_shop_loop_item_title', 15 );

Note: Since WooCommerce 3 use $product->get_id() instead of $product->id

How do I add custom fields to the categories in Woocommerce?

Add below code in your themes function.php to create custom field on your product categories taxonomy.

// Add term page
function custom_product_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Postalcode', 'my-text-domain' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','my-text-domain' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_product_taxonomy_edit_meta_field($term) {

// put the term ID into a variable
$t_id = $term->term_id;

// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Postalcode', 'my-text-domain' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','my-text-domain' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );

you can save this custom field along with categories. you can used this custom field for your filtration.

Add custom checkout field based on product category in Woocommerce

Try the following to handle product categories (handling parent terms too):

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;

if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}

// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}

// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

// Custom conditional function that check for product categories
function is_in_cart() {
// HERE your product categories
$categories = array( 'clothing' );

foreach( WC()->cart->get_cart() as $cart_item ){
if( has_product_categories( $cart_item['product_id'], $categories ) )
return true;
}
return false;
}

// Add a checkout field before order notes
add_action( 'woocommerce_before_order_notes', 'custom_checkout_field', 20 );
function custom_checkout_field( $checkout ) {
if( ! is_in_cart() ){
woocommerce_form_field( 'special_field', array(
'type' => 'textarea',
'class' => array('form-row-wide'),
'label' => pll__('Special text'),
'required' => false,
'label_class' => array('specialfieldclass'),
'clear' => false,
), $checkout->get_value( 'special_field' ));
}
}

Code goes in functions.php file of your active child theme (or active theme). It should works.

Adding 2 custom fields to product category and tags in WooCommerce but code giving notice

Remove this code

$metaArray = get_option('taxonomy_' . $term_id);
echo $productCatMetaTitle = $metaArray['wh_meta_title'];
echo $productCatMetaDesc = $metaArray['wh_meta_desc'];

From two places in your code



Related Topics



Leave a reply



Submit