na5tyk
na5tyk

Reputation: 181

Save product custom field value as custom order item meta in WooCommerce

I have problem with custom field in woocommerce (latest version). I need your help.

My code

function completed_order($order_id) {
    $order = wc_get_order( $order_id );
    $customer_id = $order->get_customer_id();
    $user = get_user_by('id', $customer_id);

    foreach ( $order->get_items() as $item_id => $item ) {
        $validity = $item->get_meta( 'validity_field' );

        if($validity) {
            $a = get_post_meta($item->get_id(), 'validity_field', true);
        }        
    } 
}
add_action('woocommerce_order_status_completed', 'completed_order', 10, 1);


function create_custom_field() {
    $args = array(
        'id' => 'validity_field',
        'label' => __( 'Ważność konta', 'waznosc' ),
        'class' => 'waznosc-custom-field',
        'desc_tip' => true,
        'description' => __( 'Wprowadz ilosc dni waznosci konta', 'waznosc' ),
    );
    woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_general_product_data', 'create_custom_field' );

function save_custom_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $title = isset( $_POST['validity_field'] ) ? $_POST['validity_field'] : '';
    $product->update_meta_data( 'validity_field', sanitize_text_field( $title ) );
    $product->save();
}
add_action( 'woocommerce_process_product_meta', 'save_custom_field' );

function add_custom_data_to_order( $item, $cart_item_key, $values, $order ) {
    foreach( $item as $cart_item_key=>$values ) {
        if( isset( $values['title_field'] ) ) {
            $item->add_meta_data( __( 'Waznosc konta', 'waznosc' ), $values['validity_field'], true );
        }
    }
}
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_data_to_order', 10, 4 );

Now I have problem with get data from custom field validity_field. Field is saved to database because value is in field while I edit product but I can't get it in hook after completed order :( I tried use get_post_meta, get_meta and another methods but nothing working. Someone knows reason why?

Upvotes: 0

Views: 1806

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254362

There are some mistakes in your code. Try the following:

add_action( 'woocommerce_product_options_general_product_data', 'add_admin_product_custom_field' );
function add_admin_product_custom_field() {
    woocommerce_wp_text_input( array(
        'id' => 'validity_field',
        'label' => __( 'Ważność konta', 'waznosc' ),
        'class' => 'waznosc-custom-field',
        'desc_tip' => true,
        'description' => __( 'Wprowadz ilosc dni waznosci konta', 'waznosc' ),
    ) );
}

add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_field_value' );
function save_product_custom_field_value( $product ) {
    $value = isset( $_POST['validity_field'] ) ? sanitize_text_field($_POST['validity_field']) : '';
    $product->update_meta_data( 'validity_field', $value );
}

add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_data_to_order_item', 10, 4 );
function add_custom_data_to_order( $item, $cart_item_key, $values, $order ) {
    $product = wc_get_product( $item->get_product_id() ); // The WC_Product Object (and the parent variable product object for product variations)
    $value   = $product->get_meta('validity_field');
    
    if( ! empty( $value ) ) {
        $item->update_meta_data( __( 'Waznosc konta', 'waznosc' ), $value );
    }
}

add_action( 'woocommerce_order_status_completed', 'action_completed_order', 10, 2 );
function action_completed_order( $order_id, $order ) {
    $user_id = $order->get_customer_id();
    $user    = $order->get_user(); // The WP_User Object (if needed)

    foreach ( $order->get_items() as $item_id => $item ) {
        $validity = $item->get_meta( 'validity_field' );
        
        if ( ! empty($validity) ) {
            // Do something with $validity
        }
    } 
}

This time it should work.

Upvotes: 1

Related Questions