I have products with a custom taxonomy called "seller" that stores the sellers ID.
My question:
How do I get the value to carry over to order in the backend when product is purchased?
Here what I got so far:
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_seller_meta' );
function my_custom_seller_meta( $order_id ) {
$order = wc_get_order( $order_id );
$prod_id_for_seller_id = $order->get_items();
$prod_id = $prod_id_for_seller_id['484']['product_id']);
$seller_id = wp_get_post_terms( $prod_id, 'soldby' );
update_post_meta( $order_id, '_seller_id_order', $seller_id );
}
Any help is appreciated.
As an order can have many different items, I am not sure that this is a good solution. Anyway, there is some errors in your code.
If you custom taxonomy is "seller", why are you using "soldby" in wp_get_post_terms()
function. The wp_get_post_terms()
function will give you an array of post terms for the 'seller' custom taxonomy…
Try this instead:
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_seller_meta', 30, 1 );
function my_custom_seller_meta( $order_id ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$order_items = $order->get_items(); // Get order items
$item = current( $order_items ); // Get the first item
// Get custom taxonomy 'seller' array of WP_Term objects
$terms = wp_get_post_terms( $item->get_product_id(), 'seller' );
$wp_term = reset($terms); // Get the first WP_Term
$term_id = $wp_term->term_id;
$term_slug = $wp_term->slug;
$term_name = $wp_term->name;
update_post_meta( $order_id, '_seller_id_order', $term_slug );
}
Code goes in function.php file of your active child theme (or active theme).
It should work