【问题标题】:Show custom field value in cart in WooCommerce在 WooCommerce 的购物车中显示自定义字段值
【发布时间】:2016-05-21 12:30:18
【问题描述】:

我正在为我的商店使用 Wordpress 和 WooCommerce。
我想在我的购物车和电子邮件订单确认中输出我的自定义字段的值。

我在我的functions.php中创建了一个自定义字段:

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {

  global $woocommerce, $post;

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

// Input
woocommerce_wp_text_input( 
    array( 
        'id'                => '_gram', 
        'label'             => __( 'somelabel', 'woocommerce' ), 
        'placeholder'       => '', 
        'description'       => __( 'sometext', 'woocommerce' ),
        'type'              => 'number', 
        'custom_attributes' => array(
                'step'  => 'any',
                'min'   => '0'
            ) 
    )
);

  echo '</div>';

}

function woo_add_custom_general_fields_save( $post_id ){
    // Number Field
    $woocommerce_number_field = $_POST['_gram'];
    if( !empty( $woocommerce_number_field ) )
        update_post_meta( $post_id, '_gram', esc_attr( $woocommerce_number_field ) );

}

在我正在使用的页面上:

get_post_meta( get_the_ID(), '_gram', true );

展示价值和完美的工作。

现在我想在我的购物车和电子邮件确认中的产品名称下显示相同的值。 但我不知道该怎么做。

有人知道怎么做吗?

【问题讨论】:

  • woocommerce 购物车在会话中工作,因此您还需要将此值存储在购物车中,然后从购物车页面上的购物车会话中获取值,然后执行下一个流程。
  • 你得到的id是什么,请查收

标签: php wordpress woocommerce


【解决方案1】:

这是一个漫长的过程,您必须实现两个过滤器和一个操作来完成此操作。

还有一个 plugin 用于此确切目的,以及与 woocommerce 自定义字段相关的许多其他选项。

这是您问题的直接解决方案。

/**
 * Here we are trying to add your custom data as Cart Line Item
 * SO that we can add this custom data on your cart, checkout, order and email later
 */
function save_custom_data( $cart_item_data, $product_id ) {
    $custom_data = get_post_meta( $product_id, '_gram', true );
    if( $custom_data != null && $custom_data != ""  ) {
        $cart_item_data["gram"] = $custom_data;
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data', 10, 2 );

/**
 * Here we are trying to display that custom data on Cart Table & Checkout Order Review Table 
 */
function render_custom_data_on_cart_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["gram"] ) ) {
        $custom_items[] = array( "name" => "Gram", "value" => $cart_item["gram"] );
    }
    return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_custom_data_on_cart_checkout', 10, 2 );

/**
 * We are adding that custom data ( gram ) as Order Item Meta, 
 * which will be carried over to EMail as well 
 */
function save_custom_order_meta( $item_id, $values, $cart_item_key ) {
    if( isset( $values["gram"] ) ) {
        wc_add_order_item_meta( $item_id, "Gram", $values["gram"] );
    }
}
add_action( 'woocommerce_add_order_item_meta', 'save_custom_order_meta', 10, 3 );

【讨论】:

  • 该功能正在运行。你知道如何使用/显示另外一个自定义字段吗?
猜你喜欢
  • 2018-07-16
  • 2017-10-09
  • 1970-01-01
  • 2018-08-28
  • 2019-08-07
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多