Woocommerce 在产品详细信息页面(Woo commerce产品详细信息页面)中添加自定义元数据库
我想添加可以在后端修改的自定义字段,但也可以在前端使用自定义字段填充 自定义字段:-Woocommerce 在产品详细信息页面(Woo commerce产品详细信息页面)中添加自定义元数据库,woocommerce,Woocommerce,我想添加可以在后端修改的自定义字段,但也可以在前端使用自定义字段填充 自定义字段:- 产品名称 斯诺。数 发票号 采购日期 位置 产品价格 客户名称 手机号码 //将字段添加到产品中 添加操作(“添加到购物车按钮之前的woocommerce”和“我的自定义”和“结帐”字段”); 函数my\u custom\u checkout\u field(){ echo“”。_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu; 回
- 产品名称
- 斯诺。数
- 发票号
- 采购日期
- 位置
- 产品价格
- 客户名称
- 手机号码
//将字段添加到产品中
添加操作(“添加到购物车按钮之前的woocommerce”和“我的自定义”和“结帐”字段”);
函数my\u custom\u checkout\u field(){
echo“”。_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
回音“产品名称”;
回声'产品SNO';
回音“发票编号”;
回音“采购日期”;
回声“位置”;
回音“产品价格”;
echo“品牌保修”;
回显“客户名称”;
回音“手机号码”;
回声';
}
//存储自定义字段
函数保存\我的\自定义\结帐\字段($cart\项目\数据,$product\ id){
如果(isset($_请求['Product_name'])){
$cart_item_data['Product_name']=$u请求['Product_name'];
$cart\u item\u data['SNO\u number']=$\u请求['SNO\u number'];
$cart_item_data['Invoice_NO']=$_REQUEST['Invoice_NO'];
$cart_item_data['PurchageDate']=$u请求['PurchageDate'];
$cart_item_data['Location']=$u请求['Location'];
$cart_item_data['Product_price']=$u请求['Product_price'];
$cart_item_data['BrandWarranty']=$u请求['BrandWarranty'];
$cart_item_data['customer_name']=$u请求['customer_name'];
$cart_item_data['MobileNo']=$u请求['MobileNo'];
/*下面的语句确保每个“添加到购物车”操作都是唯一的行项目*/
$cart_item_data['unique_key']=md5(microtime().rand());
}
返回$cart\u item\u数据;
}
添加操作('woocommerce\u add\u cart\u item\u data','save\u my\u custom\u checkout\u field',10,2);
//在购物车上呈现元数据并结帐
函数render\u meta\u on\u cart\u和\u checkout($cart\u data,$cart\u item=null){
$custom_items=array();
/*Woo 2.4.2更新*/
如果(!空($cart_数据)){
$custom\u items=$cart\u数据;
}
如果(设置($cart\u item['Product\u name'])和(!empty($cart\u item['Product\u name'])){
$custom_items[]=array(“名称”=>“产品保修”、“价值”=>“-”);
$custom_items[]=array(“name”=>'Product name',“value”=>$cart_items['Product_name']);
$custom_items[]=array(“name”=>“SNO”,“value”=>$cart_items['SNO_number]”);
$custom_items[]=数组(“name”=>“发票号”,“value”=>$cart_items['Invoice_号]);
$custom_items[]=array(“name”=>'Purchage Date',“value”=>$cart_items['PurchageDate');
$custom_items[]=array(“name”=>'Location',“value”=>$cart_items['Location']);
$custom_items[]=数组(“name”=>“产品价格”,“value”=>$cart_items['Product_价格]);
$custom_items[]=数组(“name”=>“Product name”,“value”=>$cart_items['BrandWarranty']);
$custom_items[]=array(“name”=>'customer name',“value”=>$cart_items['customer_name']);
$custom_items[]=array(“name”=>'Mobile No',“value”=>$cart_items['MobileNo']);
}
返回$custom_项目;
}
添加过滤器('woocommerce\u get\u item\u data','render\u meta\u on\u cart\u and \u checkout',10,2);
//显示为订单元
函数my_field_order_meta_handler($item_id,$values,$cart_item_key){
if(isset($values['my_field_name'])){
wc_add_order_item_meta($item_id,“Product_name”,$values['Product_name']);
wc_add_order_item_meta($item_id,“SNO_number”,$values['SNO_number']);
wc_添加_订单_项目_元($item_id,“Invoice_NO”,$values['Invoice_NO']);
wc_add_order_item_meta($item_id,“PurchageDate”,$values['PurchageDate']);
wc_添加_订单_项目_元($item_id,“Location”,$values['Location']);
wc_添加_订单_项目_元($item_id,“Product_price”,$values['Product_price']);
wc_添加_订单_项目_元($item_id,“BrandWarranty”,$values['BrandWarranty']);
wc_add_order_item_meta($item_id,“customer_name”,$values['customer_name']);
wc_add_order_item_meta($item_id,“MobileNo”,$values['MobileNo']);
}
}
添加操作('woocommerce\u add\u order\u item\u meta','my\u field\u order\u meta\u handler',1,3);
/**这就是我被困的地方**/
添加_操作('woocmerce_checkout_process'、'my_custom_checkout_field_process');
函数my\u custom\u checkout\u field\u process(){
全球商业;
//检查是否设置,如果未设置,则添加错误。此错误仅适用于公司
如果($_POST['billing_company']))
如果(!$\u POST['Product\u name'])
$woocommerce->add_错误(uuu('请输入您的XXX');
}
//使用字段值更新用户元
添加操作('woocommerce\u checkout\u update\u user\u meta'、'my\u custom\u checkout\u field\u update\u user\u meta');
函数my\u custom\u checkout\u field\u update\u user\u meta($user\u id){
如果($user\u id&$\u POST['Product\u name']))
更新用户元($user\u id,'Product\u name',esc\u attr($\u POST['Product\u name']);
更新用户元($user\u id,'SNO\u number',esc\u attr($\u POST['SNO\u number']);
更新用户元数据($用户id,'发票编号',esc属性($POST['发票编号]);
更新用户元($用户id,'PurchageDate',esc_attr($\u POST['PurchageDate']);
更新用户元($user\u id,'Location',esc\u attr($\u POST['Location']);
更新用户元($user\u id,'Product\u price',esc\u attr($\u POST['Product\u price']);
更新_user_meta($user_id,'BrandWarranty',esc_attr($_POST['BrandWarranty']);
更新用户元($user\u id,'customer\u name',esc\u attr($\u POST['customer\u name']);
更新用户元($user\u id,'MobileNo',esc\u attr($u POST['MobileNo']);
}
//使用字段值更新订单元
添加操作('woocommerce\u checkout\u update\u order\u meta'、'my\u custom\u checkout\u field\u update\u order\u meta');
功能我的_
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
echo '<div id="my_custom_checkout_field"><h4>'.__('Warranty On Your Product:').'</h4>';
echo '<p><label>Product Name</label> <input type="text" name="Product_name"></p>';
echo '<p><label>Product SNO</label> <input type="text" name="SNO_number"></p>';
echo '<p><label>Invoice N0</label> <input type="text" name="Invoice_NO"></p>';
echo '<p><label>Date Of Purchage</label> <input type="text" name="PurchageDate"></p>';
echo '<p><label>Location</label> <input type="text" name="Location"></p>';
echo '<p><label>Product Price </label> <input type="text" name="Product_price"></p>';
echo '<p><label>Brand Warranty</label> <input type="text" name="BrandWarranty"></p>';
echo '<p><label>Customer Name</label> <input type="text" name="customer_name"></p>';
echo '<p><label>Mobile No</label> <input type="text" name="MobileNo"></p>';
echo '</div>';
}
// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['Product_name'] ) ) {
$cart_item_data['Product_name'] = $_REQUEST['Product_name'];
$cart_item_data['SNO_number'] = $_REQUEST['SNO_number'];
$cart_item_data['Invoice_NO'] = $_REQUEST['Invoice_NO'];
$cart_item_data['PurchageDate'] = $_REQUEST['PurchageDate'];
$cart_item_data['Location'] = $_REQUEST['Location'];
$cart_item_data['Product_price'] = $_REQUEST['Product_price'];
$cart_item_data['BrandWarranty'] = $_REQUEST['BrandWarranty'];
$cart_item_data['customer_name'] = $_REQUEST['customer_name'];
$cart_item_data['MobileNo'] = $_REQUEST['MobileNo'];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['Product_name']) &&(!empty($cart_item['Product_name']))) {
$custom_items[] = array( "name" =>'Warranty On Your Product', "value" => '-' );
$custom_items[] = array( "name" =>'Product name', "value" => $cart_item['Product_name'] );
$custom_items[] = array( "name" =>'SNO', "value" => $cart_item['SNO_number'] );
$custom_items[] = array( "name" =>'Invoice NO', "value" => $cart_item['Invoice_NO'] );
$custom_items[] = array( "name" =>'Purchage Date', "value" => $cart_item['PurchageDate'] );
$custom_items[] = array( "name" =>'Location', "value" => $cart_item['Location'] );
$custom_items[] = array( "name" =>'Product price', "value" => $cart_item['Product_price'] );
$custom_items[]= array( "name" =>'Product name', "value" => $cart_item['BrandWarranty'] );
$custom_items[] = array( "name" =>'customer name', "value" => $cart_item['customer_name'] );
$custom_items[] = array( "name" => 'Mobile No', "value" => $cart_item['MobileNo'] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['my_field_name'] ) ) {
wc_add_order_item_meta( $item_id, "Product_name", $values['Product_name'] );
wc_add_order_item_meta( $item_id, "SNO_number", $values['SNO_number'] );
wc_add_order_item_meta( $item_id, "Invoice_NO", $values['Invoice_NO'] );
wc_add_order_item_meta( $item_id, "PurchageDate", $values['PurchageDate'] );
wc_add_order_item_meta( $item_id, "Location", $values['Location'] );
wc_add_order_item_meta( $item_id, "Product_price", $values['Product_price'] );
wc_add_order_item_meta( $item_id, "BrandWarranty", $values['BrandWarranty'] );
wc_add_order_item_meta( $item_id, "customer_name", $values['customer_name'] );
wc_add_order_item_meta( $item_id, "MobileNo", $values['MobileNo'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3 );
/** THIS IS WHERE I'M STUCK **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ($_POST['billing_company'])
if (!$_POST['Product_name'])
$woocommerce->add_error( __('Please enter your XXX.') );
}
// Update the user meta with field value
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['Product_name'])
update_user_meta( $user_id, 'Product_name', esc_attr($_POST['Product_name']) );
update_user_meta( $user_id, 'SNO_number', esc_attr($_POST['SNO_number']) );
update_user_meta( $user_id, 'Invoice_NO', esc_attr($_POST['Invoice_NO']) );
update_user_meta( $user_id, 'PurchageDate', esc_attr($_POST['PurchageDate']) );
update_user_meta( $user_id, 'Location', esc_attr($_POST['Location']) );
update_user_meta( $user_id, 'Product_price', esc_attr($_POST['Product_price']) );
update_user_meta( $user_id, 'BrandWarranty', esc_attr($_POST['BrandWarranty']) );
update_user_meta( $user_id, 'customer_name', esc_attr($_POST['customer_name']) );
update_user_meta( $user_id, 'MobileNo', esc_attr($_POST['MobileNo']) );
}
// Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['Product_name'])
update_post_meta( $order_id, 'My Name', esc_attr($_POST['Product_name']));
update_post_meta( $order_id, 'SNO Number', esc_attr($_POST['SNO_number']));
update_post_meta( $order_id, 'Invoice NO', esc_attr($_POST['Invoice_NO']));
update_post_meta( $order_id, 'Purchage Date', esc_attr($_POST['PurchageDate']));
update_post_meta( $order_id, 'Location', esc_attr($_POST['Location']));
update_post_meta( $order_id, 'Product Price', esc_attr($_POST['Product_price']));
update_post_meta( $order_id, 'Brand Warranty', esc_attr($_POST['BrandWarranty']));
update_post_meta( $order_id, 'Customer Name', esc_attr($_POST['customer_name']));
update_post_meta( $order_id, 'Mobile No', esc_attr($_POST['MobileNo']));
}
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
echo '<div id="my_custom_checkout_field"><h4>'.__('Warranty On Your Product:').'</h4>';
echo '<p><label>Product Name</label> <input type="text" name="Product_name"></p>';
echo '<p><label>Product SNO</label> <input type="text" name="SNO_number"></p>';
echo '<p><label>Invoice N0</label> <input type="text" name="Invoice_NO"></p>';
echo '<p><label>Date Of Purchage</label> <input type="text" name="PurchageDate"></p>';
echo '<p><label>Location</label> <input type="text" name="Location"></p>';
echo '<p><label>Product Price </label> <input type="text" name="Product_price"></p>';
echo '<p><label>Brand Warranty</label> <input type="text" name="BrandWarranty"></p>';
echo '<p><label>Customer Name</label> <input type="text" name="customer_name"></p>';
echo '<p><label>Mobile No</label> <input type="text" name="MobileNo"></p>';
echo '</div>';
}
// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['Product_name'] ) ) {
$cart_item_data['Product_name'] = $_REQUEST['Product_name'];
$cart_item_data['SNO_number'] = $_REQUEST['SNO_number'];
$cart_item_data['Invoice_NO'] = $_REQUEST['Invoice_NO'];
$cart_item_data['PurchageDate'] = $_REQUEST['PurchageDate'];
$cart_item_data['Location'] = $_REQUEST['Location'];
$cart_item_data['Product_price'] = $_REQUEST['Product_price'];
$cart_item_data['BrandWarranty'] = $_REQUEST['BrandWarranty'];
$cart_item_data['customer_name'] = $_REQUEST['customer_name'];
$cart_item_data['MobileNo'] = $_REQUEST['MobileNo'];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['Product_name']) &&(!empty($cart_item['Product_name']))) {
$custom_items[] = array( "name" =>'Warranty On Your Product', "value" => '-' );
$custom_items[] = array( "name" =>'Product name', "value" => $cart_item['Product_name'] );
$custom_items[] = array( "name" =>'SNO', "value" => $cart_item['SNO_number'] );
$custom_items[] = array( "name" =>'Invoice NO', "value" => $cart_item['Invoice_NO'] );
$custom_items[] = array( "name" =>'Purchage Date', "value" => $cart_item['PurchageDate'] );
$custom_items[] = array( "name" =>'Location', "value" => $cart_item['Location'] );
$custom_items[] = array( "name" =>'Product price', "value" => $cart_item['Product_price'] );
$custom_items[]= array( "name" =>'Product name', "value" => $cart_item['BrandWarranty'] );
$custom_items[] = array( "name" =>'customer name', "value" => $cart_item['customer_name'] );
$custom_items[] = array( "name" => 'Mobile No', "value" => $cart_item['MobileNo'] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['my_field_name'] ) ) {
wc_add_order_item_meta( $item_id, "Product_name", $values['Product_name'] );
wc_add_order_item_meta( $item_id, "SNO_number", $values['SNO_number'] );
wc_add_order_item_meta( $item_id, "Invoice_NO", $values['Invoice_NO'] );
wc_add_order_item_meta( $item_id, "PurchageDate", $values['PurchageDate'] );
wc_add_order_item_meta( $item_id, "Location", $values['Location'] );
wc_add_order_item_meta( $item_id, "Product_price", $values['Product_price'] );
wc_add_order_item_meta( $item_id, "BrandWarranty", $values['BrandWarranty'] );
wc_add_order_item_meta( $item_id, "customer_name", $values['customer_name'] );
wc_add_order_item_meta( $item_id, "MobileNo", $values['MobileNo'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3 );
/** THIS IS WHERE I'M STUCK **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ($_POST['billing_company'])
if (!$_POST['Product_name'])
$woocommerce->add_error( __('Please enter your XXX.') );
}
// Update the user meta with field value
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['Product_name'])
update_user_meta( $user_id, 'Product_name', esc_attr($_POST['Product_name']) );
update_user_meta( $user_id, 'SNO_number', esc_attr($_POST['SNO_number']) );
update_user_meta( $user_id, 'Invoice_NO', esc_attr($_POST['Invoice_NO']) );
update_user_meta( $user_id, 'PurchageDate', esc_attr($_POST['PurchageDate']) );
update_user_meta( $user_id, 'Location', esc_attr($_POST['Location']) );
update_user_meta( $user_id, 'Product_price', esc_attr($_POST['Product_price']) );
update_user_meta( $user_id, 'BrandWarranty', esc_attr($_POST['BrandWarranty']) );
update_user_meta( $user_id, 'customer_name', esc_attr($_POST['customer_name']) );
update_user_meta( $user_id, 'MobileNo', esc_attr($_POST['MobileNo']) );
}
// Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['Product_name'])
update_post_meta( $order_id, 'My Name', esc_attr($_POST['Product_name']));
update_post_meta( $order_id, 'SNO Number', esc_attr($_POST['SNO_number']));
update_post_meta( $order_id, 'Invoice NO', esc_attr($_POST['Invoice_NO']));
update_post_meta( $order_id, 'Purchage Date', esc_attr($_POST['PurchageDate']));
update_post_meta( $order_id, 'Location', esc_attr($_POST['Location']));
update_post_meta( $order_id, 'Product Price', esc_attr($_POST['Product_price']));
update_post_meta( $order_id, 'Brand Warranty', esc_attr($_POST['BrandWarranty']));
update_post_meta( $order_id, 'Customer Name', esc_attr($_POST['customer_name']));
update_post_meta( $order_id, 'Mobile No', esc_attr($_POST['MobileNo']));
}