首页 > 解决方案 > WooCommerce 更新后,不再包含自定义计费字段

问题描述

在更新 WooCommerce 和 WordPress 后,许多自定义设置被覆盖,因此我试图恢复与旧版本相同的功能。(使用 childtheme 所以为什么它首先丢失了我不明白)修复了除此之外的所有内容。在结帐时的帐单信息中,缺少几个字段,默认的 company_name 我又开始工作了,由于某种原因,它在主题 functions.php 中被停用。但是,为 IVA 编号和组织编号保留了两个自定义字段。

因此,我使用了 WooCommerce 的 Checkout Manager 将自定义字段添加到帐单信息中。它适用于结帐页面,信息最终出现在订单上。但它不会出现在感谢页面上,更重要的是它不会出现在给客户的电子邮件中。

尝试将其添加到主题 functions.php 但没有运气。

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['billing_wooccm13'] = array(
        'label' => __( 'Moms/IVA' ),
        'value' => get_post_meta( $order->id, 'billing_wooccm13', true ),
    );
    return $fields;
}

并且还尝试了这个:

add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {

    $output = '';
    $billing_field_testing = get_post_meta( $order->id, 'billing_wooccm13', true );

    if ( !empty($billing_wooccm13) )
        $output .= '<div><strong>' . __( "Some text:", "woocommerce" ) . '</strong> <span class="text">' . $billing_wooccm13 . '</span></div>';

    echo $output;
}

任何想法如何去做?

标签: phpwordpresswoocommercecustom-fieldsorders

解决方案


由于 WoooCommerce 3$order->id被替换为$order->get_id()……还有其他一些方法。

确保您的自定义字段的元键是billing_wooccm13 (因为它可以改为以下划线开头_billing_wooccm13

尝试以下操作:

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 ) {
    if ( $value  = $order->get_meta( 'billing_wooccm13' ) ) {
        $fields['billing_wooccm13'] = array(
            'label' => __( 'Moms/IVA' ),
            'value' => $value,
        );
    }
    return $fields;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中。它应该工作。


或者这也是:

add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $value  = $order->get_meta( 'billing_wooccm13' ) ) {
        echo '<div><strong>' . __( "Some text:", "woocommerce" ) . '</strong> <span class="text">' . $value . '</span></div>';
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中。它应该工作。


推荐阅读