Save WooCommerce Order Product Name to User Meta Custom Field

烈酒焚心 提交于 2019-12-02 01:34:14

as I've commented out, you should use woocommerce_checkout_update_order_meta.

Something like this:

function wascc_woocommerce_checkout_update_user_meta_membership ( $order_id ) {

    $theorder = new WC_Order( $order_id );
    $items = $theorder->get_items();

    foreach ( $items as $item ) {
        $product_name = $item['name'];
    }

    if (!(empty($product_name))) {

        // Gets the customer/user ID associated with the order. Guests are 0.
        $customer_id = $theorder->get_user_id();

        update_user_meta( $customer_id, 'membership', $product_name );

    }   

}

add_action( 'woocommerce_checkout_update_order_meta', 'wascc_woocommerce_checkout_update_user_meta_membership' );

I have doubts with your foreach loop though... you are looping just to get the last item?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!