【问题标题】:Change product category once the order status get completed订单状态完成后更改产品类别
【发布时间】:2017-01-05 02:15:11
【问题描述】:

一旦订单状态在 WooCommerce 中“完成”,我想将新类别应用于产品。假设产品在(A 类)中,我想申请(B 类)订单状态为“已完成”。

有什么办法吗?

我找到了几个教程,但不知道如何组合它们:

https://wordpress.org/support/topic/automatically-add-posts-to-a-category-conditionally

https://wordpress.org/support/topic/woocommerce-on-order-complete-insert-quantity-data-into-custom-database-table

我怎样才能做到这一点?

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce product orders


    【解决方案1】:

    更新

    当您想更改产品的 woocommerce 类别时,您应该使用 wp_set_object_terms() 原生 WordPress 函数,该函数接受类别 ID 或 slug 带有 'product_cat' 分类参数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 文件中。

    此代码经过测试且功能齐全。

    【讨论】:

    • 嗨,当我将代码粘贴到 functions.pho 中时,它会在代码的第一行和第二行出现错误。我认为第二行中缺少(函数)这个词,但不知道为什么它会在代码的第一行出现错误.. add_action( 'woocommerce_order_status_completed', 'add_category_to_order_items_on_competed_status' 10, 1);
    • 另外我使用的是分组产品,我们可以检查一下组列表中的第一个产品是否完成然后更改类别?
    • @user3814097 抱歉,由于我尚未使用此功能,因此无法使用分组产品进行测试。最好的方法是询问其他问题,包括此答案的代码。您将有更好的机会解决分组产品的问题。
    • 我的意思是您提供的代码可以正常工作,但我也想应用一个条件,所以如果另一个产品为真,那么它应该应用该类别。这是 ID 为“3334”的硬编码产品。
    • 我认为我们需要在代码的这一行添加一些内容以检查两种产品... $item_id = $item['product_id']
    【解决方案2】:

    如果您想在订单完成后做任何事情,那么您必须使用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' );
    

    【讨论】:

    • 嗨 Raunak - 感谢您的回答,但我在上面的代码中在哪里定义 [类别 B]?
    • 您必须在$post_categories 变量中添加类别ID。例如,如果您的 B 类 ID 为 12,那么您必须写 $post_categories = array(12);
    • 我是否还需要在这一行定义产品ID $product_id = $item['product_id']; ?
    • 不,它将是动态的。如果使用购买产品,那么在订单完成后,该产品将被添加到猫 B。
    • 嗨 Raunak - 当订单在后端标记为已完成时,您提供的代码没有任何作用?你能建议别的吗。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-04-16
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2016-02-29
    相关资源
    最近更新 更多