pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

限時送ChatGPT賬號..

我已經通過 function.php 在我的產品頁面中創建了一個自定義的保修字段.

add_action( 'woocommerce_product_options_general_product_data', 'test_custom_fields' );函數 test_custom_fields() {//打印自定義文本字段woocommerce_wp_text_input(數組('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'] ) );}}

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

因此,通過訂單頁面上的這個自定義字段,我終于可以使用 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 年".

所以你需要的代碼是:

//在產品管理選項卡上創建自定義字段add_action('woocommerce_product_options_general_product_data', 'create_warranty_custom_field');函數 create_warranty_custom_field() {//創建自定義文本框woocommerce_wp_text_input(數組('id' =>'_保修單','類型' =>'文本','標簽' =>__('保修', 'woocommerce'),'說明' =>'','desc_tip' =>'真的','占位符' =>__('即 15 年', 'woocommerce'),) );}//將此自定義字段中的數據值保存在產品管理選項卡上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 );函數 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;}//在購物車和結帳上呈現元數據add_filter('woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2);函數 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;}//將訂單中的信息添加為元數據add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3);函數 add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {//檢索訂單 $item_id 的產品 ID$product_id = wc_get_order_item_meta( $item_id, '_product_id', true );//獲取此產品 ID 的保修值$warranty = get_post_meta( $product_id, '_warranty', true );//將元數據添加到訂單中wc_add_order_item_meta($item_id, '保修', $warranty, true);}

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

此代碼已經過測試且有效.

<小時>

參考文獻:

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

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

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

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

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
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 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 贴板式电磁阀-不锈钢-气动上展式放料阀-上海弗雷西阀门有限公司 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 | 板框压滤机-隔膜压滤机配件生产厂家-陕西华星佳洋装备制造有限公司 | 协议书_协议合同格式模板范本大全 | 便携式谷丙转氨酶检测仪|华图生物科技百科 | 恒温槽_恒温水槽_恒温水浴槽-上海方瑞仪器有限公司 | 品牌策划-品牌设计-济南之式传媒广告有限公司官网-提供品牌整合丨影视创意丨公关活动丨数字营销丨自媒体运营丨数字营销 | 切铝机-数控切割机-型材切割机-铝型材切割机-【昆山邓氏精密机械有限公司】 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 危废处理系统,水泥厂DCS集散控制系统,石灰窑设备自动化控制系统-淄博正展工控设备 | 广州云仓代发-昊哥云仓专业电商仓储托管外包代发货服务 | 安徽净化工程设计_无尘净化车间工程_合肥净化实验室_安徽创世环境科技有限公司 | 京马网,京马建站,网站定制,营销型网站建设,东莞建站,东莞网站建设-首页-京马网 | 过滤器_自清洗过滤器_气体过滤器_苏州华凯过滤技术有限公司 | 最新电影-好看的电视剧大全-朝夕电影网 | 冷库安装厂家_杭州冷库_保鲜库建设-浙江克冷制冷设备有限公司 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | 温州食堂承包 - 温州市尚膳餐饮管理有限公司 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | 定量包装秤,吨袋包装称,伸缩溜管,全自动包装秤,码垛机器人,无锡市邦尧机械工程有限公司 | 深圳公司注册-工商注册代理-注册公司流程和费用_护航财税 | 北京康百特科技有限公司-分子蒸馏-短程分子蒸馏设备-实验室分子蒸馏设备 | 瑞典Blueair空气净化器租赁服务中心-专注新装修办公室除醛去异味服务! | 亮点云建站-网站建设制作平台 | 塑料异型材_PVC异型材_封边条生产厂家_PC灯罩_防撞扶手_医院扶手价格_东莞市怡美塑胶制品有限公司 | 高速龙门架厂家_监控杆_多功能灯杆_信号灯杆_锂电池太阳能路灯-鑫世源照明 | 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 一氧化氮泄露报警器,二甲苯浓度超标报警器-郑州汇瑞埔电子技术有限公司 | 粉碎机_塑料粉碎机_塑料破碎机厂家-星标机械 | 橡胶接头_橡胶软接头_可曲挠橡胶接头-巩义市创伟机械制造有限公司 | 环压强度试验机-拉链拉力试验机-上海倾技仪器仪表科技有限公司 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | 电动高尔夫球车|电动观光车|电动巡逻车|电动越野车厂家-绿友机械集团股份有限公司 | 领先的大模型技术与应用公司-中关村科金 | 纳米二氧化硅,白炭黑,阴离子乳化剂-臻丽拾科技 | 北京租车公司_汽车/客车/班车/大巴车租赁_商务会议/展会用车/旅游大巴出租_北京桐顺创业租车公司 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 液压中心架,数控中心架,自定心中心架-烟台恒阳机电设计有限公司 行星搅拌机,双行星搅拌机,动力混合机,无锡米克斯行星搅拌机生产厂家 |