簡體   English   中英

在購物車,結帳和查看訂單中設置產品自定義字段和顯示值

[英]Set product custom field and display value in cart, checkout and view order

我已經通過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 Invoice插件在我的pdf發票中顯示“保修”。

另一種解釋:

  1. 作為管理員,我在“product1”頁面填寫_warranty值
  2. 當“product1”在訂單中時,在管理訂單視圖中,我希望在product1頁面中看到包含“_warranty + value”的自定義字段。
  3. 所以,作為管理員,我可以在WooCommerce PDF Invoice插件中設置{{_warranty}}來顯示“保修期:15年”

非常感謝您的幫助。

我剛剛測試了以下案例: 在訂單詳細信息中的訂單項表中顯示產品元數據

但這並沒有給我一個自定義字段,所以我無法獲得{{_warranty}}值寬度。

我做錯了什么?
我怎樣才能做到這一點?

謝謝。

首先: “在管理訂單頁面上的自生成自定義字段中,使用鍵和值復制此自定義字段” 不是一個好方法。

為了達到你所期望的目標,你錯過了一些小事。 你需要:

  1. 將自定義字段存儲在購物車中(將產品添加到購物車時)
  2. 在購物車和結帳頁面上呈現此內容
  3. 將訂單中的信息添加為元數據(使其成為訂單的一部分)

通過第3點,您將能夠在WooCommerce PDF Invoice插件上顯示“保修期: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文件中,也可以在任何插件文件中。

此代碼經過測試和運行。


參考文獻:

代碼末尾有一個錯誤,你首先調用變量$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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM