从Woocommerce购物车项目中的自定义字段显示自定义缺货订单文本

时间:2018-10-06 07:05:16

标签: php wordpress woocommerce cart custom-fields

  

这与:   Replace product "on backorder" to a custom field value in Woocommerce

我想在缺货的购物车商品中显示_backorder_text产品自定义字段值。

基于Admin product pages custom field displayed in Cart and checkout,这是我拥有的代码:

public Grades(double num1, double num2, double num3) {
    this.num1 = num1;
    this.num2 = num2;
    this.num3 = num3;
}

但这不起作用。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

  

继续:Replace product "on backorder" to a custom field value in Woocommerce

要在缺货的购物车商品(和订购商品)中显示_backorder_text产品自定义字段值,请使用以下命令:

// Display in cart items backorder text on cart and checkout pages
add_filter('woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2);
function display_custom_cart_item_data( $cart_item_data, $cart_item ) {
    if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
        $backorder_text = $cart_item['data']->get_meta('_backorder_text');
    }

    if( isset($backorder_text) && ! empty($backorder_text) ) {
        $cart_item_data[] = array(
            'name'  => __("Backordered", "woocommerce"),
            'value' => $backorder_text,
        );
    }

    return $cart_item_data;
}

// Order items: Save "backorder text" as order item meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
    if( $values['data']->is_on_backorder( $values['quantity'] ) ) {
        $backorder_text = $values['data']->get_meta('_backorder_text');
    }

    if( isset($backorder_text) && ! empty($backorder_text) ) {
        $item->update_meta_data( __("Backordered", "woocommerce"), $backorder_text );
    }
} 

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

enter image description here