我的functions.php中有一个函数,它根据类别添加一个自定义字段(_shirt_number)到结帐
function bbloomer_product_add_on() {
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_the_terms( get_the_ID(), 'product_cat', $cat_args );
foreach($product_categories AS $cat) {
if($cat->name == 'Goalkeeper Shirts' OR $cat->name == '16 Shirts' OR $cat->name == 'Outfield Shirts') {
$value = isset($_POST['_shirt_number'][0] );
echo '<table class="variations" cellspacing="0" id="shirtTable"> <tbody id="shirtDOM"><tr> <td class="label"><label for="shirtnumber">Shirt Number</label></td><td class="value"> <input required type="text" id="shirtnumber" class="" name="_shirt_number[]"> </td></tr></tbody> </table>';
wp_enqueue_script( 'script', get_template_directory_uri() . '/quirkypixel.js', array ( 'jquery' ), 1.1, true);
}
}
}
另一个函数将此输入保存到订单项元数据中
function bbloomer_product_add_on_order_item_meta( $item_id, $values ) {
if ( ! empty( $values['custom_numbers'] ) ) {
foreach(unserialize($values['custom_numbers']) AS $item) {
wc_add_order_item_meta( $item_id, '_shirt_numbers', $item, true );
}
}
}
但是,我无法将其添加到电子邮件模板的订单表中。我尝试了以下方法:
function bbloomer_product_add_on_display_emails( $fields ) {
$fields['custom_numbers'] = 'Shirt Number(s)';
return $fields;
}
and from the woocommerce docs:
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['custom_numbers'] = array(
'label' => __( 'Shirt Number(s)' ),
'value' => get_post_meta( $order->id, 'custom_numbers', true ),
);
return $fields;
}
并尝试用“_shirt_number”替换“custom_numbers”以查看是否有效......但仍然没有任何内容添加到电子邮件中的表格中。
我已成功将产品类别添加到表中,但无法通过此字段。(图片显示了电子邮件中的订单表,添加了类别,我希望这个字段拉到下面)
有什么想法吗?如果我没有提供足够的信息或其他任何信息,请告诉我。