#php #wordpress #woocommerce #custom-fields #email-notifications
#php #wordpress #woocommerce #пользовательские поля #уведомления по электронной почте
Вопрос:
Итак, вот в чем дело, когда я пишу этот код в 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',
'placeholder' => 'Custom Product Text Field',
'label' => __('Meeting ID', 'woocommerce'),
'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, '_custom_product_text_field', true) . "</p>";
echo "<p><strong>Meeting ID:</strong>" . get_post_meta($product_id, '_custom_product_number_field', true) . "</p>";
echo "<p><strong>Password:</strong>" . get_post_meta($product_id, '_custom_product_textarea', true) . "</p>";
//more code
}
}
echo get_post_meta($id, '_custom_product_text_field', true);
echo get_post_meta($id, '_custom_product_number_field', true);
echo get_post_meta($id, '_custom_product_textarea', true);
это работает только в серверной части (страница редактирования продукта)
он сохраняет переменные, но затем не отображается ни в meta, ни до или после кнопки добавить в карточку, я не смог найти решение для этого
я попробовал echo, но по-прежнему безрезультатно
так я думал, что это моя тема? нет, я установил wordpress на другой vps, у меня все еще не отображается текстовое поле -текст-ничего
если вы знаете о проблеме, пожалуйста, будьте добры и скажите мне, что я делаю не так! Спасибо
Ответ №1:
Этот код немного устарел со времен 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', 'woocommerce'),
'zoom_password' => __('Meeting password', 'woocommerce'),
);
}
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 файл вашей активной дочерней темы (или active theme). Протестировано и работает.