【问题标题】:After WooCommerce update, custom billing fields are not included any moreWooCommerce 更新后,不再包含自定义计费字段
【发布时间】:2021-05-17 17:56:33
【问题描述】:

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

所以我使用 Checkout Manager for 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;
}

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

【问题讨论】:

    标签: php wordpress woocommerce custom-fields orders


    【解决方案1】:

    由于WoooCommerce 3 $order-&gt;id$order-&gt;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 文件中。它应该可以工作。

    【讨论】:

      【解决方案2】:

      非常感谢 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>';
          }
          }
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-09
        • 2019-03-22
        • 2018-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多