【问题标题】:Output product custom field values in order email notification在订单电子邮件通知中输出产品自定义字段值
【发布时间】:2017-04-13 13:21:53
【问题描述】:

我有一个 Woocommerce 产品的自定义字段,我想在订单电子邮件中显示它的价值。

由于我使用的是自定义产品提交表单,因此我在下面的自定义字段中添加了此代码以创建自定义字段:

<?php 
    WCVendors_Pro_Form_Helper::select( array(  
        'post_id'       => $object_id,
        'class'         => 'select2',
        'id'            => 'wcv_custom_product_ingredients', 
        'label'         => __( 'What time?', 'wcvendors-pro' ), 
        'placeholder'   => __( 'Pick a time', 'wcvendors-pro' ),
        'wrapper_start' => '<div class="all-100">',
        'wrapper_end'   => '</div>',
        'desc_tip'      => 'true', 
        'description'   => __( 'Pick a time', 'wcvendors-pro' ),
        'options'       => array( '12:00 midnight' => __('12:00 midnight', 'wcv_custom_product_ingredients'), '12:15 midnight'=> __('12:15 midnight', 'wcv_custom_product_ingredients') )
) );
?>

我也尝试将下面的代码添加到functions.php,但这只会显示“什么时间?”没有价值的订单电子邮件...

add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email');
function wcv_ingredients_email() {
    $output = get_post_meta( get_the_ID(), 'wcv_custom_product_ingredients', true );
    echo 'What time? ' . $output . '<br>';
}

可能是什么问题?

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    您使用了正确的钩子,但您只是忘记了钩子函数中的钩子参数。

    此外,由于您要定位产品自定义字段值,并且在一个订单中可以有许多商品(产品),下面的代码将为每个订单商品显示此值。

    试试下面的代码,它现在可以工作了:

    // Tested on WooCommerce version 2.6.x and 3+ — For simple products only.
    add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email', 10, 4);
    function wcv_ingredients_email( $order,  $sent_to_admin,  $plain_text,  $email ){
        foreach($order->get_items() as $item_values){
            // Get the product ID for simple products (not variable ones)
            $product_id = $item_values['product_id'];
            $output = get_post_meta( $product_id, 'wcv_custom_product_ingredients', true );
            echo 'What time? ' . $output . '<br>';
        }
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    【讨论】:

    • 嗨,非常感谢!您能告诉我如何在 woocommerce 的订单页面上包含此自定义字段中的值吗?
    • @MarkoI。您是指订单接收页面和我的帐户订单查看页面(客户订单页面)吗?
    • @MarkoI。我肯定已经不止一次地回答了这个问题。我需要找回我的答案。您想在哪里准确输出?
    • 我想在example.com/my-account/view-order/00000的订单明细表中输出,即单视图订单页面。 (在产品名称下会很棒。)。以及在收到订单时的订单详情表中,在此页面上:example.com/checkout/order-received/00000/…
    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2018-08-29
    • 2020-09-09
    相关资源
    最近更新 更多