Add a product category to a specific product via a button
I want to add a button to product page when those products are in the categoryexpire. This button has to add a category. I search on web a solution and I found a code. I modified this code in order to work in my website but it doesn't. Here is the code :
add_action( 'woocommerce_single_product_summary', 'extra_button_on_product_page', 22 );
function extra_button_on_product_page() {
global $post, $product;
if ( has_term( 'expire', 'product_cat' ) ) {
?
button type=submit name=in_metrology class=button value=yes?php
echo __( 'In Metrology', 'woocommerce' ); ?/button
?php
}
}
add_action( 'restrict_manage_posts', 'add_category_product' );
function add_category_product() {
global $post, $product;
if (isset($_GET['in-metrology']) $_GET['in_metrology'] === 'yes' ) {
add_category();
}
}
function add_category() {
global $post, $product;
$term = get_term_by('name', 'Metrology', 'product_cat');
wp_set_object_terms( $product_ID, $term-term_id, product_cat);
}
Thanks for any help
Topic woocommerce-offtopic buttons php categories Wordpress
Category Web