订单状态完成后更改产品类别
Change product category once the order status get completed
我想在 WooCommerce 中的订单状态达到 "completed" 后将新类别应用于产品。假设产品在(类别 A)中,我想申请(类别 B)订单状态 "completed"。
有什么办法吗?
我找到了几个教程,但不知道如何组合它们:
https://wordpress.org/support/topic/automatically-add-posts-to-a-category-conditionally
我怎样才能做到这一点?
谢谢!
如果您想在订单完成后执行任何操作,则必须使用 woocommerce_order_status_completed
操作,而要添加类别则必须使用 wp_set_object_terms。所以在你的情况下,这个功能应该可以工作。
function add_cat_product($order_id) {
$post_categories = array(); //Array of category IDs.
$append = FALSE; // If true, categories will be appended to the post. If false, categories will replace existing categories.
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
//$product_name = $item['name'];
$product_id = $item['product_id'];
$term_taxonomy_ids = wp_set_object_terms( $product_id, $post_categories, 'product_cat', $append);
if ( is_wp_error( $term_taxonomy_ids ) ) {
// There was an error somewhere and the terms couldn't be set.
} else {
// Success! The post's categories were set.
}
}
}
add_action( 'woocommerce_order_status_completed', 'add_cat_product' );
已更新
As you want to change the woocommerce category for a product, you should use wp_set_object_terms()
native WordPress function that accept either the category ID or slug with 'product_cat'
taxonomy parameter and NOT 'category'
.
woocommerce_order_status_completed
挂钩通常用于在订单状态更改完成时触发回调函数。
这是代码:
add_action('woocommerce_order_status_completed', 'add_category_to_order_items_on_competed_status' 10, 1);
function add_category_to_order_items_on_competed_status( $order_id ) {
// set your category ID or slug
$your_category = 'my-category-slug'; // or $your_category = 123;
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item_id => $product_item ) {
$product_id = $product_item->get_product_id();
wp_set_object_terms( $product_id, $your_category, 'product_cat' );
}
}
或者您也可以使用 woocommerce_order_status_changed
挂钩,其条件函数将过滤订单“已完成”状态:
add_action('woocommerce_order_status_changed', 'add_category_to_order_items_on_competed_status' 10, 1);
function add_category_to_order_items_on_competed_status( $order_id ) {
// set your category ID or slug
$your_category = 'my-category-slug'; // or $your_category = 123;
$order = wc_get_order( $order_id );
if ( $order->has_status( 'completed' ) ) {
foreach ( $order->get_items() as $item_id => $product_item ) {
$product_id = $product_item->get_product_id();
wp_set_object_terms( $product_id, $your_category, 'product_cat' );
}
}
}
此代码在您的活动子主题或主题的 function.php 文件中。
此代码已经过测试且功能齐全。
我想在 WooCommerce 中的订单状态达到 "completed" 后将新类别应用于产品。假设产品在(类别 A)中,我想申请(类别 B)订单状态 "completed"。
有什么办法吗?
我找到了几个教程,但不知道如何组合它们:
https://wordpress.org/support/topic/automatically-add-posts-to-a-category-conditionally
我怎样才能做到这一点?
谢谢!
如果您想在订单完成后执行任何操作,则必须使用 woocommerce_order_status_completed
操作,而要添加类别则必须使用 wp_set_object_terms。所以在你的情况下,这个功能应该可以工作。
function add_cat_product($order_id) {
$post_categories = array(); //Array of category IDs.
$append = FALSE; // If true, categories will be appended to the post. If false, categories will replace existing categories.
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
//$product_name = $item['name'];
$product_id = $item['product_id'];
$term_taxonomy_ids = wp_set_object_terms( $product_id, $post_categories, 'product_cat', $append);
if ( is_wp_error( $term_taxonomy_ids ) ) {
// There was an error somewhere and the terms couldn't be set.
} else {
// Success! The post's categories were set.
}
}
}
add_action( 'woocommerce_order_status_completed', 'add_cat_product' );
已更新
As you want to change the woocommerce category for a product, you should use
wp_set_object_terms()
native WordPress function that accept either the category ID or slug with'product_cat'
taxonomy parameter and NOT'category'
.
woocommerce_order_status_completed
挂钩通常用于在订单状态更改完成时触发回调函数。
这是代码:
add_action('woocommerce_order_status_completed', 'add_category_to_order_items_on_competed_status' 10, 1);
function add_category_to_order_items_on_competed_status( $order_id ) {
// set your category ID or slug
$your_category = 'my-category-slug'; // or $your_category = 123;
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item_id => $product_item ) {
$product_id = $product_item->get_product_id();
wp_set_object_terms( $product_id, $your_category, 'product_cat' );
}
}
或者您也可以使用 woocommerce_order_status_changed
挂钩,其条件函数将过滤订单“已完成”状态:
add_action('woocommerce_order_status_changed', 'add_category_to_order_items_on_competed_status' 10, 1);
function add_category_to_order_items_on_competed_status( $order_id ) {
// set your category ID or slug
$your_category = 'my-category-slug'; // or $your_category = 123;
$order = wc_get_order( $order_id );
if ( $order->has_status( 'completed' ) ) {
foreach ( $order->get_items() as $item_id => $product_item ) {
$product_id = $product_item->get_product_id();
wp_set_object_terms( $product_id, $your_category, 'product_cat' );
}
}
}
此代码在您的活动子主题或主题的 function.php 文件中。
此代码已经过测试且功能齐全。