【问题标题】:Woocommerce custom product field in checkout [duplicate]结帐中的 Woocommerce 自定义产品字段 [重复]
【发布时间】:2017-09-08 12:29:51
【问题描述】:

我有一个看似简单的任务,但我终其一生都想不通。

我的 Woocommerce 产品的管理页面上有一个自定义字段。我只需要在此处设置的值显示在 Woocommerce 结帐页面和电子邮件中。

我已经设法使用以下代码使该字段显示在产品页面上:

add_action( 'woocommerce_product_options_general_product_data', 'create_shipdate_custom_field' );
function create_shipdate_custom_field() {
woocommerce_wp_text_input( array(
'id'            => '_shipdate',
'type'          => 'text',
'label'         => __('Shipping Date', 'woocommerce' ),
'description'   => '',
'desc_tip'      => 'true',
'placeholder'   =>  __('i.e. 16 December 2017', 'woocommerce' ),
) );
}

// save the data value from this custom field on product admin tab
add_action( 'woocommerce_process_product_meta', 'save_shipdate_custom_field' );
function save_shipdate_custom_field( $post_id ) {
$wc_text_field = $_POST['_shipdate'];
if ( !empty($wc_text_field) ) {
update_post_meta( $post_id, '_shipdate', esc_attr( $wc_text_field ) );
}
}

function product_date() {
echo get_post_meta( get_the_ID(), '_shipdate', true );
}

add_action('woocommerce_single_product_summary', 'product_date', 30);

我可以使用以下代码在结帐页面上显示静态文本,但它不适用于我的自定义字段:

function emaildate() { echo "This will display fine"; } 

add_action('woocommerce_order_item_meta_start', 'emaildate', 10, 1);

提前感谢您的帮助!

【问题讨论】:

  • 我能够在结帐页面上显示静态文本 - 可以展示你是如何尝试这个的,即emaildate 方法?
  • 所以,例如:function emaildate() { echo "This will display fine"; }add_action('woocommerce_order_item_meta_start', 'emaildate', 10, 1);
  • 请在您的问题中添加任何其他相关信息。 cmets 中的信息很容易被遗漏。
  • 您应该可以使用echo get_post_meta( get_the_ID(), '_shipdate', true ); 来显示_shipdate 值。
  • 感谢您到目前为止的帮助。我已经用附加信息编辑了我的问题。 @Und3rTow,该代码适用于在单个产品页面中显示该字段。但是,结帐页面没有显示它。

标签: php wordpress woocommerce


【解决方案1】:

您缺少几个步骤才能让您的自定义值显示在购物车/结帐页面上,即将自定义字段存储在购物车中并按顺序存储为元数据。以下是应按预期工作的完整代码:

 // create the custom field on product admin tab
add_action( 'woocommerce_product_options_general_product_data', 'create_shipping_custom_field' );
function create_shipping_custom_field() {
    // Create a custom text field
    woocommerce_wp_text_input( array(
        'id'            => '_shipdate',
        'type'          => 'text',
        'label'         => __('Shipping Date', 'woocommerce' ),
        'description'   => '',
        'desc_tip'      => 'true',
        'placeholder'   =>  __('i.e. 16 December 2017', 'woocommerce' ),
    ) );
}

// save the data value from this custom field on product admin tab
add_action( 'woocommerce_process_product_meta', 'save_shipping_custom_field' );
function save_shipping_custom_field( $post_id ) {
    $wc_text_field = $_POST['_shipdate'];
    if ( !empty($wc_text_field) ) {
        update_post_meta( $post_id, '_shipdate', esc_attr( $wc_text_field ) );
    }
}

// Store custom field in Cart
add_action( 'woocommerce_add_cart_item_data', 'store_shipping_custom_field', 10, 2 );
function store_shipping_custom_field( $cart_item_data, $product_id ) {
    $ship_date = get_post_meta( $product_id , '_shipdate', true );
    if( !empty($ship_date) ) {
        $cart_item_data[ '_shipdate' ] = $ship_date;

        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'date_shipping', $ship_date );
    }
    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_data, $cart_item ) {
    $custom_items = array();
    // Woo 2.4.2 updates
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['_shipdate'] ) ) {
        $custom_items[] = array( "name" => __( "Shipping Date", "woocommerce" ), "value" => $cart_item['_shipdate'] );
    }
    return $custom_items;
}

// Add the information in the order as meta data
add_action('woocommerce_add_order_item_meta','add_shipping_to_order_item_meta', 1, 3 );
function add_shipping_to_order_item_meta( $item_id, $values, $cart_item_key ) {
    $prod_id = wc_get_order_item_meta( $item_id, '_product_id', true );
    $shipdate = get_post_meta( $prod_id, '_shipdate', true );
    wc_add_order_item_meta( $item_id, 'Shipping Date', $shipdate, true );
}

【讨论】:

  • 你是个传奇。这几乎是完美的。现在唯一没有显示日期的是订单收据电子邮件。
  • 哦,对了,今天有空就去看看:)
  • 非常感谢,再次感谢!
  • 原来这只是一个错字,修改了最后一个函数 - 上面的add_shipping_to_order_item_meta,您只需在代码中将$product_id替换为$prod_id,然后值应该显示在订单上收到的页面以及订单电子邮件中
  • 太棒了!我真的很感激帮助。它完美地工作。出于兴趣考虑,我添加了这行代码以使其也显示在单个产品页面上:function product_date() { echo get_post_meta( get_the_ID(), '_shipdate', true ); } add_action('woocommerce_single_product_summary', 'product_date', 30);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2023-04-10
  • 2020-02-22
相关资源
最近更新 更多