问题描述
所以当我在mytheme / functions.PHP文件中编写此代码时,这就是交易
//this code for adding field in product backend
// display Fields
add_action('woocommerce_product_options_general_product_data','woocommerce_product_custom_fields1');
// Save Fields
add_action('woocommerce_process_product_Meta','woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields1()
{
global $woocommerce,$post;
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(array(
'id' => '_custom_product_text_field','placeholder' => 'Custom Product Text Field','label' => __('Zoom Meeting Url ','woocommerce'),'desc_tip' => 'true'
));
//Custom Product Number Field
woocommerce_wp_text_input(array(
'id' => '_custom_product_number_field','label' => __('Meeting ID','type' => 'text','custom_attributes' => array(
// 'step' => 'any',// 'min' => '0'
)
));
//Custom Product Textarea
woocommerce_wp_textarea_input(array(
'id' => '_custom_product_textarea','placeholder' => 'Custom Product Text','label' => __('Password','woocommerce')
));
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
if (!empty($woocommerce_custom_product_text_field))
update_post_Meta($post_id,'_custom_product_text_field',esc_attr($woocommerce_custom_product_text_field));
// Custom Product Number Field
$woocommerce_custom_product_number_field = $_POST['_custom_product_number_field'];
if (!empty($woocommerce_custom_product_number_field))
update_post_Meta($post_id,'_custom_product_number_field',esc_attr($woocommerce_custom_product_number_field));
// Custom Product Textarea Field
$woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea'];
if (!empty($woocommerce_custom_procut_textarea))
update_post_Meta($post_id,'_custom_product_textarea',esc_html($woocommerce_custom_procut_textarea));
}
add_action( 'woocommerce_email_after_order_table','wdm_add_shipping_method_to_order_email',10,2 );
function wdm_add_shipping_method_to_order_email( $order,$is_admin_email ) {
echo '<h4>Zoom Meeting Details</h4>';
$order_id = $order->get_id();
$order = wc_get_order( $order_id ); //returns WC_Order if valid order
$items = $order->get_items(); //returns an array of WC_Order_item or a child class (i.e. WC_Order_Item_Product)
foreach( $items as $item ) {
$type = $item->get_type();
$product_id = $item->get_product_id();
echo "<p>" . get_the_title($product_id) . "</p>";
echo "<p><strong>Zoom Meeting Url:</strong>" . get_post_meta($product_id,true) . "</p>";
echo "<p><strong>Meeting ID:</strong>" . get_post_meta($product_id,true) . "</p>";
echo "<p><strong>Password:</strong>" . get_post_meta($product_id,true) . "</p>";
//more code
}
}
echo get_post_meta($id,true);
echo get_post_meta($id,true);
它仅在后端工作(产品编辑页面)
它保存了变量,但不显示在Meta中或在添加到卡片按钮之前或之后-或之后都没有显示
我尝试了回声,但仍然没有结果
所以我以为我是我的主题?不 我在其他vps上安装了wordpress,但仍然没有显示文本字段-text-nothing
如果您知道该问题,请告诉我我做错了什么! 谢谢
解决方法
此代码自WooCommerce 3起有点过时了。您还使用了textarea字段作为密码,而应该是一个简单的文本输入字段。
我已经完全重新考虑了您的代码:
// Custom function that handle your custom fields settings
function product_custom_fields_settings(){
// Fieds Id key | Field label name (pairs)
return array(
'zoom_meeting_url' => __('Meeting Url','woocommerce'),'zoom_meeting_id' => __('Meeting Id','zoom_password' => __('Meeting password',);
}
add_action('woocommerce_product_options_general_product_data','display_product_custom_fields');
function display_product_custom_fields(){
echo '<div class="product_custom_field">';
// Loop through settings Fieds Id key | Field label name (pairs)
foreach ( product_custom_fields_settings() as $key => $label ) {
// Input text fields
woocommerce_wp_text_input(array(
'id' => '_'.$key,'label' => $label,));
}
echo '</div>';
}
add_action('woocommerce_admin_process_product_object','save_product_custom_fields');
function save_product_custom_fields( $product ){
foreach ( product_custom_fields_settings() as $key => $label ) {
if ( isset($_POST['_'.$key]) ) {
$product->update_meta_data( '_'.$key,sanitize_text_field( $_POST['_'.$key] ) );
}
}
}
add_action( 'woocommerce_email_after_order_table','product_custom_fields_to_email_notification',10,4 );
function product_custom_fields_to_email_notification( $order,$sent_to_admin,$plain_text,$email ) {
// The HTML Structure
$html_output = '<h3>'.__('Zoom meeting Details','woocommerce').'</h3>
<div class="meeting-info"><table cellspacing="0" cellpadding="6"><tbody>';
// Loop through order items
foreach( $order->get_items() as $item ) {
$product = $item->get_product();
$html_output .= '<tr><th colspan="2">'.$item->get_name().'</th></tr>'; // Product name
// Loop through settings Fieds Id key | Field label name (pairs)
foreach ( product_custom_fields_settings() as $key => $label ) {
if( $value = $product->get_meta('_'.$key) ) {
$html_output .= '<tr><th>'.$label.':</th><td>'.$value.'</td></tr>';
}
}
// … More code …
}
// The CSS styling
$styles = '<style>
.meeting-info table{width: 100%; font-family: \'Helvetica Neue\',Helvetica,Roboto,Arial,sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
.meeting-info table th,table.teilnehmer-info td{text-align: left; border-top-width: 4px;
color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
.meeting-info table td{text-align: left; border-top-width: 4px; color: #737373;
border: 1px solid #e4e4e4; padding: 12px; word-break: break-all;}
.meeting-info table tr.subtitle h3{margin: 0;}
</style>';
// The Output CSS + HTML
echo $styles . $html_output . '</tbody></table></div><br>';
}
代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。