购物车和结帐中显示的管理产品页面自定义字段 [英] Admin product pages custom field displayed in Cart and checkout

查看:79
本文介绍了购物车和结帐中显示的管理产品页面自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在WooCommerce Admin的产品页面的常规设置"选项卡上创建了一个自定义字段",以插入几天用于制造.我想在每个产品名称上方的购物车和结帐页面上显示此自定义字段值.

I have created a Custom Field in WooCommerce Admin on the general settings tab of product pages, to insert a some days for manufacture. I would like to show this custom field value on cart and checkout pages above the name of each product.

这是我的代码:

// Insert a Custom Admin Field
function woo_add_custom_general_fields() {

    echo '<div class="options_group">';

    woocommerce_wp_text_input( array( 
        'id'                => 'days_manufacture', 
        'label'             => __( 'Days for Manufacture', 'woocommerce' ), 
        'placeholder'       => '', 
        'description'       => __( 'Insert here', 'woocommerce' ),
        'type'              => 'number', 
        'custom_attributes' => 
        array(
            'step'  => 'any',
            'min'   => '1'
        ) 
    ) );
    echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );


// Save field
function woo_add_custom_general_fields_save( $post_id ){
    $woocommerce_number_field = $_POST['days_manufacture'];
    if( !empty( $woocommerce_number_field ) )
        update_post_meta( $post_id, 'days_manufacture', esc_attr( $woocommerce_number_field ) );
}
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );


function save_days_field( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['days_manufacture'] ) ) {
        $cart_item_data[ 'days_manufacture' ] = get_post_meta( $post->ID, 'days_manufacture',true );

/* below statement make sure every add to cart action as unique line item */

   $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );


function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $custom_items = array();
    /* Woo 2.4.2 updates */
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['days_manufacture'] ) ) {
        $custom_items[] = array( "name" => 'Days:', "value" => $cart_item['days_manufacture'] );
    }
    return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );

但这无法正常工作,因为我无法在购物车和结帐页面上显示自定义字段值.

But this is not working as I can't display the custom field value on cart and checkout pages.

我该如何实现?

谢谢

推荐答案

我已经测试了您的代码,并更正了一些部分,以使产品自定义字段显示在购物车和结帐页面上.

I have tested your code and corrected some portions to make that product custom field appear on cart and checkout pages.

这是已更正的代码:

// Insert a Custom Admin Field
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
    echo '<div class="options_group">';

    woocommerce_wp_text_input( array(
        'id'                => 'days_manufacture',
        'label'             => __( 'Days for Manufacture', 'woocommerce' ),
        'placeholder'       => '',
        'description'       => __( 'Insert here', 'woocommerce' ),
        'type'              => 'number',
        'custom_attributes' => array(
            'step'  => 'any',
            'min'   => '1'
        ),
    ) );

    echo '</div>';
}

// Save the field
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_number_field = $_POST['days_manufacture'];
if( !empty( $woocommerce_number_field ) )
    update_post_meta( $post_id, 'days_manufacture', esc_attr( $woocommerce_number_field ) );
}

// Store custom field
add_filter( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
function save_days_field( $cart_item_data, $product_id ) {
    $special_item = get_post_meta( $product_id , 'days_manufacture',true );
    if(!empty($special_item)) {
        $cart_item_data[ 'days_manufacture' ] = $special_item;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'days_manufacture', $special_item );
    }
    return $cart_item_data;
}


// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );

function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
    if( isset( $cart_item['days_manufacture'] ) ) {
        $cart_item_data[] = array( "name" => __( "Days", "woocommerce" ), "value" => $cart_item['days_manufacture'] );
    }
    return $cart_item_data;
}

自然,这会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

此代码已经过测试并且可以正常工作.

参考: WooCommerce:将自定义Metabox添加到管理订单页面

这篇关于购物车和结帐中显示的管理产品页面自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆