【问题标题】:Add Custom fields values to Woocommerce email notification将自定义字段值添加到 Woocommerce 电子邮件通知
【发布时间】:2019-04-25 07:06:12
【问题描述】:

我已使用 YITH 结帐管理器 billing_customfield1 和 billing_customfield2 添加到结帐页面上的自定义字段。 我需要在 woocommerce 发送的电子邮件中显示此字段。

就是这样,结帐的字段显示在电子邮件模板上:

<?php if ( $order->get_billing_first_name() ) : ?>
                <br/><?php echo '<b>Nombre: </b>'.esc_html( $order->get_billing_first_name().' '.$order->get_billing_last_name() ); ?>
            <?php endif; ?>             


            <?php if ( $order->get_billing_phone() ) : ?>
                <br/><?php echo '<b>Teléfono: </b>'.esc_html( $order->get_billing_phone() ); ?>
            <?php endif; ?>

            <?php if ( $order->get_billing_company() ) : ?>
                <br/><?php echo '<b>Nombre de la empresa: </b>'.esc_html( $order->get_billing_company() ); ?>
            <?php endif; ?>

            <?php if ( $order->get_billing_address_1() ) : ?>
                <br/><?php echo '<b>Dirección: </b>'.esc_html( $order->get_billing_address_1() ); ?>
            <?php endif; ?>

            <?php if ( $order->get_billing_address_2() ) : ?>
                <br/><?php echo '<b>Dirección 2: </b>'.esc_html( $order->get_billing_address_2() ); ?>
            <?php endif; ?>

            <?php if ( $order->get_billing_city() ) : ?>
                <br/><?php echo '<b>Ciudad: </b>'.esc_html( $order->get_billing_city() ); ?>
            <?php endif; ?>

我还需要显示新的 2 个自定义字段,但没有方法

$order->get_customfield1()

【问题讨论】:

    标签: php wordpress woocommerce custom-fields orders


    【解决方案1】:

    从 Woocommerce 3 开始,有一个从 WC_Data 类继承的方法是 get_meta() 并且必须在 WC_Order $order 实例对象上使用。

    大多数情况下,订单自定义字段在数据库中使用 元键 注册,并在 wp_postmeta 表中以 下划线 开头。
    所以我将使用:_billing_customfield1_billing_customfield2

    这是您重新访问的代码:

    <?php 
    if ( $order->get_formatted_billing_full_name() ) {
        echo '<br/><b>'.__("Nombre", "woocommerce") . ': </b>' . esc_html( $order->get_formatted_billing_full_name() );
    }             
    if ( $order->get_billing_phone() ) {
        echo '<br/><b>'.__("Teléfono", "woocommerce") . ': </b>' . esc_html( $order->get_billing_phone() );
    }
    if ( $order->get_billing_company() ) {
        echo '<br/><b>'.__("Nombre de la empresa", "woocommerce") . ': </b>' . esc_html( $order->get_billing_company() );
    }
    if ( $order->get_billing_address_1() ) {
        echo '<br/><b>'.__("Dirección", "woocommerce") . ': </b>' . esc_html( $order->get_billing_address_1() ); 
    }
    if ( $order->get_billing_address_2() ) {
        echo '<br/><b>'.__("Dirección 2", "woocommerce") . ': </b>' . esc_html( $order->get_billing_address_2() );
    }
    if ( $order->get_billing_city() ) {
        echo '<br/><b>'.__("Ciudad", "woocommerce") . ': </b>' . esc_html( $order->get_billing_city() ); 
    }
    if ( $order->get_meta('_billing_customfield1') ) {
        echo '<br/><b>'.__("Campo personalizado 1", "woocommerce") . ': </b>' . esc_html( $order->get_meta('_billing_customfield1') ); 
    }
    if ( $order->get_meta('_billing_customfield2') ) {
        echo '<br/><b>'.__("Campo personalizado 2", "woocommerce") . ': </b>' . esc_html( $order->get_meta('_billing_customfield2') ); 
    }
    ?>
    

    如果这些自定义字段 slug 是 _billing_customfield1_billing_customfield2,它应该可以工作

    您应该需要在wp_postmeta 表下的数据库中验证正确的meta_key_billing_customfield1_billing_customfield2...

    如果不是,您将用正确的替换它们。


    您还可以使用 WordPress get_post_meta() 函数,该函数需要您可以通过 $order-&gt;get_id() 获得的订单 ID,例如:

    if ( get_post_meta( $order->get_id(), '_billing_customfield1', true ) ) {
        echo '<br/><b>'.__("Campo personalizado 1", "woocommerce") . ': </b>' . esc_html( get_post_meta( $order->get_id(), '_billing_customfield1', true ) ); 
    }
    if ( get_post_meta( $order->get_id(), '_billing_customfield2', true ) ) {
        echo '<br/><b>'.__("Campo personalizado 2", "woocommerce") . ': </b>' . esc_html( get_post_meta( $order->get_id(), '_billing_customfield2', true ) ); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 2015-10-08
      • 2018-09-19
      • 2019-01-13
      • 2018-09-21
      • 2018-08-31
      • 2021-12-11
      相关资源
      最近更新 更多