How to use update_post_meta with Metabox?

Support General How to use update_post_meta with Metabox?Resolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #36283
    kadotakadota
    Participant

    I'm trying to assign an order's product SKU to a custom value, like this:

    add_action( 'woocommerce_thankyou', 'change_value' );
     
    function change_value( $order_id ) {
       $item_sku = '';
       $order = wc_get_order( $order_id );
       foreach ($order->get_items() as $item) {
         $product = wc_get_product($item->get_product_id());
         $item_sku = $product->get_sku();
       }
       update_post_meta( $order_id, 'custom_value', $item_sku );   
    }

    However, I can't make it work and I can't find anything about how to update a custom value from Metabox in a similar way to this. Can you help me? Thanks!

    #36291
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Can you please share the code that creates the custom fields on your site? If the field is associated with the order, it will be updated with the WP function update_post_meta().

    #36298
    kadotakadota
    Participant

    Sorry, my bad. The problem was with using product variations—$product->get_sku() wasn't working. In case someone needs it, this is how you can get it:

    get_post_meta( $item['variation_id'], '_sku', true );

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.