WooCommerce 更新后,不再包含自定义账单字段

After WooCommerce update, custom billing fields are not included any more

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

所以我使用 WooCommerce 的结账管理器将自定义字段添加到账单信息中。 它在结账页面上有效,信息最终出现在订单上。但它不会出现在感谢页面上,更重要的是,它不会出现在给客户的电子邮件中。

尝试将其添加到主题中 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;
}

有什么想法可以解决这个问题吗?

因为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 文件。应该可以。

非常感谢 LoicTheAztec。

这两种解决方案都有效,但第一种解决方案的文本最终位于发票地址之前的高位。第二个结束在下面,不幸的是之前有一些空白行但没什么大不了的。假设我必须使用电子邮件模板来删除这些行。我最终得到的代码:


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_wooccm11' ) ) {
        echo '<div><strong>' . __( "Ert organisationsnummer", "woocommerce" ) . '</strong> <span class="text">' . $value . '</span></div>';

    if ( $value  = $order->get_meta( '_billing_wooccm13' ) ) {
        echo '<div><strong>' . __( "Ert Moms/IVA:", "woocommerce" ) . '</strong> <span class="text">' . $value . '</span></div>';
    }
    }
}