php购物车生成订单,php – 在购物车,结帐和查看订单中设置产品自定义字段和显示值...
更新 – 更正了拼写错误:代码末尾的$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.
为了达到你所期望的目标,你错过了一些小事.你需要:
>将自定义字段存储在购物车中(将产品添加到购物车时)
>在购物车和结帐页面上渲染
>将订单中的信息添加为元数据(使其成为订单的一部分)
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文件中,也可以在任何插件文件中.
此代码经过测试和运行.
参考文献:
php购物车生成订单,php – 在购物车,结帐和查看订单中设置产品自定义字段和显示值...相关推荐
- php购物车修改单价,php – woocommerce在结帐和购物车页面更改价格
通过woocommerce,在我的网站中,我想在购物车页面中添加一个选择输入,用户可以在两个选项之间选择一个值,并根据此值我将更改价格. 到目前为止,我可以获得总数并使用此更改它: function ...
- ajax 提交订单,php-在Woocommerce 3中通过ajax提交并在结帐时创建订单
我在结帐表单中添加了一个按钮: 并在functions.php文件中添加了一个AJAX代码段: add_action('wp_head', 'ajax_call_place_order'); func ...
- php中购物车功能,php如何实现购物车功能
php实现购物车功能的方法:首先登录到网站中浏览商品:然后购买指定的商品,进入购物车页面中,并在该页面实现更改商品数量.删除商品.清空购物车.继续购物等:最后实现生成订单,提交订单等操作即可. 本文介 ...
- 【MVC购物车】购买和加入购物车功能
书接上回: [MVC购物车]类关系 1 建表 DROP TABLE IF EXISTS `product`; CREATE TABLE `product` (`id` int(11) DEFAULT ...
- 用session和mysql实现购物车_Session和Cookie实现购物车
使用Session和Cookie实现购物车的比较 购物车相当于现实中超市的购物车,不同的是一个是实体车,一个是虚拟车而已.用户可以在购物网站的不同页面之间跳转,以选购自己喜爱的商品,点击购买时,该商品 ...
- 购物车及商品php代码_php购物车代码_php网上商城购物车代码一例
摘要 腾兴网为您分享:php网上商城购物车代码一例,有信,英语字典,携程,网赚招聘等软件知识,以及迅雷vip补丁,全能素描,苏酒门户,inshort视频剪辑,北京地铁高清大图,监控上网,云播搜,维融打 ...
- php 购物车类的属性,php购物车类
<?php //购物车类 //支持自定义列名及列数 // 作者:天地小子 twt326@163.com //转载或修改请保留原版权,谢谢 class twt_Cart { //类属性****** ...
- js前端开发案例教程之DOM购物车(动手实践:购物车)
js前端开发案例教程 之 DOM购物车(动手实践:购物车) html和css <!DOCTYPE html> <html><head><meta charse ...
- 微信小程序(购物车)--在wxml中设置保留小数位数
微信小程序(购物车)–在wxml中设置保留小数位数 一.在该页面文件夹下新建一个wxs后缀的文件 var filters = {toFix: function (value) {return valu ...
最新文章
- 0x15.基本数据结构 — 字符串 (KMP算法(含详细证明)和最小表示法)
- 后端服务不得不了解之限流
- 如何构造天然满足某些约束的神经网络?
- 项目解析jsx文件_React 基础:JSX 扩展语法
- asteroids模板 游戏 java_在高级Java游戏中存储全局/静态变量的最佳方法是什么?...
- 鸿蒙电视是无线么,鸿蒙系统首秀,在自家设备上和普通电视大不相同赵崇带你走世界...
- JVM内存区域(一)
- Win10系统下LaserJet Pro MFP M227sdn无法自动双面打印问题解决
- 【暗恋不可耻但无用】QQ空间爬虫-Python版(pyzone-crawler)
- android麦克风设置在哪,手机麦克风设置实用教程
- ROS Bridge 笔记(02)— carla_ros_bridge 功能包(准备 ROS环境、运行 ROS Bridge、配置 CARLA 参数、同步模式下使用 ROS Bridge、主车辆控制)
- 麻省理工学院公开课:单变量微积分
- Python数据分析与挖掘——回归模型的假设检验
- ios免越狱脚本实现方案,苹果手机实现自动抖音发私信且是中文输入,FN键切换输入法,最新的苹果11以上圆角的屏幕一并支持。
- vue拦截器设置请求头失败,laravel设置前端请求头跨域
- 函数连续,函数可微,函数可导,偏导数存在,偏导数连续之间的关系
- 3 MySQL数据管理
- 面向考试数据库—单表查询(包含建表数据)
- 3des加密 java php_php 3des加密 兼容JAVA 多么痛的领悟呀
- h5py数据存储格式与图像加载