久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在購物車、結帳和查看訂單中設置產(chǎn)品自定義字

Set product custom field and display value in cart, checkout and view order(在購物車、結帳和查看訂單中設置產(chǎn)品自定義字段和顯示值)
本文介紹了在購物車、結帳和查看訂單中設置產(chǎn)品自定義字段和顯示值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我已經(jīng)通過 function.php 在我的產(chǎn)品頁面中創(chuàng)建了一個自定義的保修字段.

add_action( 'woocommerce_product_options_general_product_data', 'test_custom_fields' );函數(shù) test_custom_fields() {//打印自定義文本字段woocommerce_wp_text_input(數(shù)組('id' =>'_保修單','標簽' =>'IE.15年','說明' =>'','desc_tip' =>'真的','占位符' =>'IE.15年') );}add_action('woocommerce_process_product_meta', 'test_save_custom_fields');功能 test_save_custom_fields( $post_id ) {如果(!空($_POST['_warranty'])){update_post_meta( $post_id, '_warranty', esc_attr( $_POST['_warranty'] ) );}}

我想根據(jù)購物車/訂單中的產(chǎn)品(無插件)在管理訂單頁面上的一個自生成的自定義字段中使用鍵和值復制"這個自定義字段.

因此,通過訂單頁面上的這個自定義字段,我終于可以使用 WooCommerce PDF Invoice 插件在我的 pdf 發(fā)票中顯示保修"了.

另一種解釋:

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

非常感謝您的幫助.

我剛剛測試了以下案例:在訂單詳情中的訂單項表中顯示產(chǎn)品元數(shù)據(jù)

但這并沒有給我一個自定義字段,所以我無法得到我的 {{_warranty}} 值寬度它.

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

謝謝.

解決方案

第一:在管理訂單頁面上的一個自生成的自定義字段中復制帶有鍵和值的自定義字段" 不是好方法.

為了實現(xiàn)您的期望,您只是錯過了一些小事.您需要:

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

<塊引用>

使用第 3 點,您將能夠在 WooCommerce PDF Invoice 插件上獲得此信息,以顯示保修:15 年".

所以你需要的代碼是:

//在產(chǎn)品管理選項卡上創(chuàng)建自定義字段add_action('woocommerce_product_options_general_product_data', 'create_warranty_custom_field');函數(shù) create_warranty_custom_field() {//創(chuàng)建自定義文本框woocommerce_wp_text_input(數(shù)組('id' =>'_保修單','類型' =>'文本','標簽' =>__('保修', 'woocommerce'),'說明' =>'','desc_tip' =>'真的','占位符' =>__('即 15 年', 'woocommerce'),) );}//將此自定義字段中的數(shù)據(jù)值保存在產(chǎn)品管理選項卡上add_action('woocommerce_process_product_meta', 'save_warranty_custom_field');功能 save_warranty_custom_field( $post_id ) {$wc_text_field = $_POST['_warranty'];如果(!空($wc_text_field)){update_post_meta( $post_id, '_warranty', esc_attr( $wc_text_field ) );}}//將自定義字段存儲在購物車中add_filter( 'woocommerce_add_cart_item_data', 'store_warranty_custom_field', 10, 2 );函數(shù) store_warranty_custom_field( $cart_item_data, $product_id ) {$warranty_item = get_post_meta( $product_id , '_warranty', true );如果(!空($warranty_item)){$cart_item_data['_warranty'] = $warranty_item;//下面的語句確保每個添加到購物車的操作都是唯一的訂單項$cart_item_data['unique_key'] = md5( microtime().rand() );WC()->session->set('days_manufacture', $warranty_item);}返回 $cart_item_data;}//在購物車和結帳上呈現(xiàn)元數(shù)據(jù)add_filter('woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2);函數(shù) rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {$custom_items = array();//Woo 2.4.2 更新if( !empty( $cart_data ) ) {$custom_items = $cart_data;}if( isset( $cart_item['_warranty'] ) ) {$custom_items[] = array( "name" => __( "Warranty", "woocommerce" ), "value" => $cart_item['_warranty'] );}返回 $custom_items;}//將訂單中的信息添加為元數(shù)據(jù)add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3);函數(shù) add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {//檢索訂單 $item_id 的產(chǎn)品 ID$product_id = wc_get_order_item_meta( $item_id, '_product_id', true );//獲取此產(chǎn)品 ID 的保修值$warranty = get_post_meta( $product_id, '_warranty', true );//將元數(shù)據(jù)添加到訂單中wc_add_order_item_meta($item_id, '保修', $warranty, true);}

當然,這會出現(xiàn)在活動子主題(或主題)的 function.php 文件或任何插件文件中.

此代碼已經(jīng)過測試且有效.

<小時>

參考文獻:

  • WooCommerce:將自定義 Metabox 添加到管理訂單頁面
  • 管理產(chǎn)品頁面自定義字段顯示在購物車和結帳中

I've created a custom field for warranty in my products pages, via 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'] ) );
    }
}

I would like to "duplicate" this custom field with key and value, in an self-generated custom field on the admin order page depending on products in cart/order (without plugin).

So, with this custom field on order page, I will finally be able to display "warranty" in my pdf invoice with WooCommerce PDF Invoice plugin.

Another explanation :

  1. As admin, I fill _warranty value in "product1" page
  2. When "product1" is in an order, on the admin order view, I would like to see a custom field containing "_warranty + value" from product1 page.
  3. So, as admin, I could set {{_warranty}} in WooCommerce PDF Invoice plugin to display "Warranty : 15 years"

Many thanks for your help.

I have just tested the following case: show product meta in order items table in Order Details

But this does not give me a custom field, so I couldn't get my {{_warranty}} value width it.

What I am doing wrong?
How can I achieve this?

Thanks.

解決方案

First: "Duplicating this custom field with key and value, in an self-generated custom field on the admin order page" is not the good approach.

To achieve what you are expecting, you have missed just some little things. You need to:

  1. Store custom field in Cart (when product is added to cart)
  2. Render this on cart and checkout pages
  3. Add the information in the order as meta data (to make it part of the order)

With point 3 you will be able to get this on WooCommerce PDF Invoice plugin to display "Warranty : 15 years".

So the code you need is:

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

Naturally, this goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.


References:

  • WooCommerce : Add custom Metabox to admin order page
  • Admin product pages custom field displayed in Cart and checkout

這篇關于在購物車、結帳和查看訂單中設置產(chǎn)品自定義字段和顯示值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 99精品国产成人一区二区 | 亚洲 精品 综合 精品 自拍 | 97视频久久 | av在线免费不卡 | 在线精品一区二区三区 | 精品欧美一区二区三区久久久 | 中文视频在线 | 国产精品无码专区在线观看 | 风间由美一区二区三区在线观看 | 中文字幕在线剧情 | 成人久久18免费网站麻豆 | 国产99热精品 | 国产精品久久久久久久久久尿 | 综合久久99 | 欧美日韩一区二区在线 | 亚洲精品一区二区 | 久久久国产一区二区三区 | 欧美一区二区在线免费观看 | 日干夜操 | 日韩一区二区在线免费观看 | 99久久99 | 欧美在线视频二区 | 国产精品不卡一区 | 亚洲福利av | 国产一级免费视频 | 亚洲天堂中文字幕 | 国产精品久久久久无码av | 精精国产视频 | 日日夜夜天天 | 国产一区二区高清在线 | 99久久免费精品国产免费高清 | 中文字幕在线第一页 | 伊人网伊人网 | 精品婷婷 | 久久99精品久久久久久国产越南 | 日日夜精品视频 | 久久99深爱久久99精品 | 精品一区二区在线看 | 农村妇女毛片精品久久久 | 久久精品亚洲精品国产欧美 | 亚洲男人的天堂网站 |