日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php购物车生成订单,php – 在购物车,结帐和查看订单中设置产品自定义字段和显示值...

發(fā)布時間:2025/4/16 php 70 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php购物车生成订单,php – 在购物车,结帐和查看订单中设置产品自定义字段和显示值... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

更新 – 更正了拼寫錯誤:代碼末尾的$product_id變量名稱

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.

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

>將自定義字段存儲在購物車中(將產(chǎn)品添加到購物車時)

>在購物車和結帳頁面上渲染

>將訂單中的信息添加為元數(shù)據(jù)(使其成為訂單的一部分)

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

所以你需要的代碼是:

// 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文件中,也可以在任何插件文件中.

此代碼經(jīng)過測試和運行.

參考文獻:

總結

以上是生活随笔為你收集整理的php购物车生成订单,php – 在购物车,结帐和查看订单中设置产品自定义字段和显示值...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。