【问题标题】:Get a custom taxonomy value and set it as Order meta data in Woocommerce获取自定义分类值并将其设置为 Woocommerce 中的订单元数据
【发布时间】:2018-08-17 12:18:46
【问题描述】:

我的产品具有一个名为“卖家”的自定义分类,用于存储卖家 ID。

我的问题:
购买产品时如何在后端结转订单价值?

到目前为止我得到了什么:

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 );
}

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy orders


    【解决方案1】:

    由于一个订单可以有许多不同的项目,我不确定这是一个好的解决方案。无论如何,您的代码中存在一些错误。

    如果您的自定义分类是“卖家”,为什么在wp_get_post_terms() 函数中使用“soldby”。 wp_get_post_terms() 函数将为您提供一个数组'seller'自定义分类法post terms

    试试这个:

    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 );
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。

    它应该可以工作

    【讨论】:

    • 工作完美,除了我不得不改变 $term_slug = $wp_term->slug; to $term_slug = $wp_term[0]->slug;
    • @Tsea 我的代码有一点小错误……查看我的更新……我应该像你一样工作。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多