Display WooCommerce custom product field meta data in Cart and on Checkout

南笙酒味 提交于 2021-02-10 23:39:52

问题


I've created a custom product field (admin). The field works fine and it saves the data just fine.

What I am having problems with it is adding the meta data to the cart and checkout.

I get no errors (debug turned on in wp-config), no notices -- nothing. In the cart and on checkout, no data is shown.

This is my code:

add_action( 'woocommerce_product_options_general_product_data', 'product_delivery_time' );
function product_delivery_time(){

    global $woocommerce, $post;

    echo '<div class="product_custom_field">';
        woocommerce_wp_text_input(
        array(
        'id' => '_shipping_field',
        'placeholder' => 'shipping time in days',
        'label' => __('Shipping Time', 'woocommerce'),
        'desc_tip' => 'true'
        )
    );

    echo '</div>';
}

add_action( 'woocommerce_process_product_meta', 'product_delivery_time_save' );
function product_delivery_time_save( $post_id ) {

    if (isset($_POST['_shipping_field'] ) )
    update_post_meta( $post_id, '_shipping_field', esc_attr( $_POST['_shipping_field'] ) );
}

add_filter( 'woocommerce_add_cart_item_data', 'delivery_time_in_cart', 10, 3 ); 
function delivery_time_in_cart( $cart_item_data, $product_id, $variation_id ) {

    if (! empty( $_POST['_shipping_field'] ) ) {

        $cart_item_data['_shipping_field'] = sanitize_text_field( $_POST['_shipping_field'] );
    }
    
    return $cart_item_data;
}

add_filter( 'woocommerce_get_item_data', 'delivery_time_in_cart_display', 10, 2 ); 
function delivery_time_in_cart_display( $item_data, $cart_item ) {

    if (! empty($cart_item['_shipping_field'] ) ) {

        $item_data[] = array(
        'key' => '_shipping_field',
        'value' => $cart_item['_shipping_field'],
        'display' => '',
        );
    }
    
    return $item_data;
}

add_action( 'woocommerce_checkout_create_order_line_item', 'delivery_time_in_order_display', 10, 4 ); 
function delivery_time_in_order_display( $item, $cart_item_key, $values, $order ) {

    if (! empty($values['_shipping_field'] ) ) {

        $item->add_meta_data( 'Delivery time:', $values['_shipping_field'] );
    }
}

回答1:


You have some minor mistakes, this should suffice

  • Note the use of woocommerce_admin_process_product_object to save instead of the outdated woocommerce_process_product_meta hook

So you get

// Add field
function product_delivery_time() {
    echo '<div class="product_custom_field">';
        woocommerce_wp_text_input( array(
            'id' => '_shipping_field',
            'placeholder' => 'shipping time in days',
            'label' => __('Shipping Time', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );

    echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'product_delivery_time', 10, 0 );

// Save Field
function woo_add_custom_general_fields_supplier_save( $product ) {
    if( isset($_POST['_shipping_field']) ) {
        $product->update_meta_data( '_shipping_field', esc_html( $_POST['_shipping_field'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'woo_add_custom_general_fields_supplier_save', 10, 1 );

// Display on cart & checkout pages
function delivery_time_in_cart_display( $item_data, $cart_item ) {
    $shipping_field = $cart_item['data']->get_meta('_shipping_field');
    
    if ( ! empty( $shipping_field ) ) {
        $item_data[] = array(
            'name' => __('Shipping field', 'woocommerce'),
            'value' => $shipping_field,
        );
    }
    
    return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'delivery_time_in_cart_display', 10, 2 );

// Display item data everywhere on orders and email notifications 
function delivery_time_in_order_display( $item, $cart_item_key, $values, $order ) {
    $shipping_field = $values['data']->get_meta('_shipping_field');

    if ( ! empty( $shipping_field ) ) {
        $item->update_meta_data( __( 'Shipping field', 'woocommerce'), $shipping_field );
    }
}
add_action( 'woocommerce_checkout_create_order_line_item', 'delivery_time_in_order_display', 10, 4 ); 


来源:https://stackoverflow.com/questions/62870852/display-woocommerce-custom-product-field-meta-data-in-cart-and-on-checkout

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