【问题标题】:Set product custom field and display value in cart, checkout and view order设置产品自定义字段并在购物车中显示值、结帐和查看订单
【发布时间】:2017-01-10 20:14:06
【问题描述】:

我通过 function.php 在我的产品页面中创建了一个自定义保修字段。

add_action( 'woocommerce_product_options_general_product_data', 'test_custom_fields' );
function test_custom_fields() {
    // Print a custom text field
    woocommerce_wp_text_input( array(
        'id' => '_warranty',
        'label' => 'i.e. 15 years',
        'description' => '',
        'desc_tip' => 'true',
        'placeholder' => 'i.e. 15 years'
    ) );        
}

add_action( 'woocommerce_process_product_meta', 'test_save_custom_fields' );
function test_save_custom_fields( $post_id ) {
    if ( ! empty( $_POST['_warranty'] ) ) {
        update_post_meta( $post_id, '_warranty', esc_attr( $_POST['_warranty'] ) );
    }
}

我想根据购物车/订单中的产品(不带插件)在管理订单页面上的一个自行生成的自定义字段中“复制”这个带有键和值的自定义字段。

因此,有了订单页面上的这个自定义字段,我终于可以使用 WooCommerce PDF 发票插件在我的 pdf 发票中显示“保修”。

另一种解释:

  1. 作为管理员,我在“product1”页面中填写 _warranty 值
  2. 当“product1”在订单中时,在管理订单视图中,我希望在 product1 页面中看到一个包含“_warranty + value”的自定义字段。
  3. 所以,作为管理员,我可以在 WooCommerce PDF 发票插件中设置 {{_warranty}} 以显示“保修:15 年”

非常感谢您的帮助。

我刚刚测试了以下案例:show product meta in order items table in Order Details

但这并没有给我一个自定义字段,所以我无法获得我的 {{_warranty}} 值宽度。

我做错了什么?
我怎样才能做到这一点?

谢谢。

【问题讨论】:

标签: php wordpress woocommerce product orders


【解决方案1】:

第一: “在管理订单页面上的自行生成的自定义字段中复制此自定义字段的键和值” 不是好方法.

为了达到您的期望,您只是错过了一些小事。您需要:

  1. 在购物车中存储自定义字段(将产品添加到购物车时)
  2. 在购物车和结帐页面上呈现此内容
  3. 将订单中的信息添加为元数据(使其成为订单的一部分)

使用 第 3 点,您将能够在 WooCommerce PDF 发票插件上显示“保修:15 年”。

所以你需要的代码是:

// create the custom field on product admin tab
add_action( 'woocommerce_product_options_general_product_data', 'create_warranty_custom_field' );
function create_warranty_custom_field() {
    // Create a custom text field
    woocommerce_wp_text_input( array(
        'id'            => '_warranty',
        'type'          => 'text',
        'label'         => __('Warranty', 'woocommerce' ),
        'description'   => '',
        'desc_tip'      => 'true',
        'placeholder'   =>  __('i.e. 15 years', 'woocommerce' ),
    ) );
}

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

// Store custom field in Cart
add_filter( 'woocommerce_add_cart_item_data', 'store_warranty_custom_field', 10, 2 );

function store_warranty_custom_field( $cart_item_data, $product_id ) {
    $warranty_item = get_post_meta( $product_id , '_warranty', true );
    if( !empty($warranty_item) ) {
        $cart_item_data[ '_warranty' ] = $warranty_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', $warranty_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_data, $cart_item ) {
    $custom_items = array();
    // Woo 2.4.2 updates
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['_warranty'] ) ) {
        $custom_items[] = array( "name" => __( "Warranty", "woocommerce" ), "value" => $cart_item['_warranty'] );
    }
    return $custom_items;
}

// Add the information in the order as meta data
add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3 );
function add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {
    // Retrieving the product id for the order $item_id
    $product_id = wc_get_order_item_meta( $item_id, '_product_id', true );
    // Getting the warranty value for this product Id
    $warranty = get_post_meta( $product_id, '_warranty', true );
    // Add the meta data to the order
    wc_add_order_item_meta($item_id, 'Warranty', $warranty, true);
}

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

此代码已经过测试并且可以工作。


参考资料:

【讨论】:

  • 我刚刚测试了您的代码,不幸的是,当我将它应用到我的 function.php 中时,我得到了 502 Bad Gatweay。你有什么主意吗 ?如果可能的话,我不想在我的购物车和结帐页面中添加此字段,此页面上不需要此信息“保修”。它只在我的发票中是必需的。能否请您从我不是开发人员那里删除不必要的代码,所以在 php 中非常菜鸟......非常感谢!
【解决方案2】:

代码最后有个错误,你先调用一个变量$prod_id,然后$product_id。正确且有效的代码是:

// Add the information in the order as meta data
add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3 );
function add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {
    // Retrieving the product id for the order $item_id
    $prod_id = wc_get_order_item_meta( $item_id, '_product_id', true );
    // Getting the warranty value for this product Id
    $warranty = get_post_meta( $prod_id, '_warranty', true );
    // Add the meta data to the order
    wc_add_order_item_meta($item_id, 'Warranty', $warranty, true);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2019-07-21
    • 2019-05-11
    • 1970-01-01
    • 2018-01-07
    相关资源
    最近更新 更多