php购物车生成订单,php – 在购物车,结帐和查看订单中设置产品自定义字段和显示值...
更新 – 更正了拼寫錯(cuò)誤:代碼末尾的$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.
為了達(dá)到你所期望的目標(biāo),你錯(cuò)過了一些小事.你需要:
>將自定義字段存儲(chǔ)在購物車中(將產(chǎn)品添加到購物車時(shí))
>在購物車和結(jié)帳頁面上渲染
>將訂單中的信息添加為元數(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);
}
當(dāng)然,這可以在您的活動(dòng)子主題(或主題)的function.php文件中,也可以在任何插件文件中.
此代碼經(jīng)過測(cè)試和運(yùn)行.
參考文獻(xiàn):
總結(jié)
以上是生活随笔為你收集整理的php购物车生成订单,php – 在购物车,结帐和查看订单中设置产品自定义字段和显示值...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php如何实现根据地区内筛选,PHP区块
- 下一篇: php中mb substr,php中中文