php - 结帐时的 Woocommerce 自定义产品字段

标签 php wordpress woocommerce

<分区>

我有一个看似简单的任务,但我似乎一辈子都无法解决。

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

在此先感谢您的帮助!

最佳答案

您缺少将自定义值显示在购物车/结帐页面上的几个步骤,即,将自定义字段存储在购物车和订单中作为元数据。这是应该按预期工作的完整代码:

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

关于php - 结帐时的 Woocommerce 自定义产品字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46116634/

相关文章:

php - 我需要坚持准备好的陈述吗?

php - 带有从表语法错误中选择的 Wordpress 简码

php - WordPress自研AMP - 编辑查询如果URL是homepage.com/amp即可获取头版帖子

php - 有没有办法在非 WordPress 项目中使用 WPDB 类?

wordpress - 如何在 WordPress 主题标题中显示 woocommerce 产品的类别名称

jquery - Updated_cart_totals 触发器在清空购物车后不起作用

php - 如何应对带有下拉菜单的多语言系统?

php - 使用md5为CSS样式生成唯一id

php - 用php中的页面标题替换模板页面中的单词

php - 自定义优惠券类型 woocommerce WordPress