在 WooCommerce 中将产品自定义字段值保存为自定义订单项元

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

我在 woocommerce(最新版本)中的自定义字段有问题。我需要你的帮助。

我的代码

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

现在我在从自定义字段 validity_field 获取数据时遇到问题。 字段已保存到数据库中,因为在我编辑产品时值在字段中,但在完成订单后我无法将其挂钩 :( 我尝试使用 get_post_meta、get_meta 和其他方法,但没有任何效果。 有人知道原因吗?

您的代码中有一些错误。请尝试以下操作:

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

这次应该可以了。