在WooCommerce中将产品自定义字段值获取为变量 [英] Get product custom field values as variables in WooCommerce

查看:144
本文介绍了在WooCommerce中将产品自定义字段值获取为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Woocommerce注册课程;每个类别/产品最多具有20个自定义字段,我需要将其作为变量返回,以高度自定义结帐和订单详细信息页面以及客户电子邮件模板。

I am using Woocommerce for registration for classes; each class/product has up to 20 custom fields, which I need to return as variables in order to highly customize the checkout and order details pages and customer email template.

不要只是想用钩子添加所有自定义元数据,因为我需要对输出进行非常严格的控制。例如,以下答案可以很好地在购物车页面中包含自定义字段:在Woocommerce购物车中显示自定义字段的值结帐项目

I don't just want to add all of the custom meta data with hooks, because I need very tight control over the output. For instance, the following answer worked fine for including custom fields in the cart page: Display Custom Field's Value in Woocommerce Cart & Checkout items

但是我也希望它们在结帐,订单详细信息和电子邮件中使用,并完全控制它们出现在(在某些情况下,它们也需要转换,例如,将Unix时间戳转换为人类可读的时间)。

But I would like them in checkout, order details and emails as well, with full control over the HTML where they appear (in some cases they will need to be converted as well, e.g., Unix timestamps to human readable time).

order-item中-details.php 模板,我尝试了以下操作:

In order-item-details.php template I have tried the following:

$room = get_post_meta( $product->ID, 'room', true );
$room_number = get_post_meta( $product->ID, 'room_number', true );
$instructor = get_post_meta( $product->ID, 'instructor', true );

echo 'Your instructor is ' . $instructor . '.';
echo 'Your class is in ' . $room . '.';

但这没用…

如何访问和使用与特定产品关联的自定义字段值?

How can I access and use custom field values associated with a specific product?

推荐答案

第一个 $ product-> ID 将无法正常工作。 所以您的问题是如何获取产品ID。

多种情况

1)在商店等存档页面或单个产品页面中:

1) In Archive pages like shop or in Single product pages:


  • 使用 get_the_id()通常会始终为您提供产品ID(帖子ID)

  • Using get_the_id() will give you mostly always the product id (the post ID)

$room = get_post_meta( get_the_id(), 'room', true );


  • 您还可以使用 global $ product; $ product ,则$ c>与 $ product-> get_id()一起使用。

  • You can also use global $product; with $product->get_id() if $product is not defined.

    global $product;
    
    $room = get_post_meta( $product->get_id(), 'room', true );
    


  • 您还可以使用 global $ post; $ post-> ID 的$ c>(如果未定义 $ post ):

  • You can also use global $post; with $post->ID (if $post is not defined):

    global $post;
    
    $room = get_post_meta( $post->ID, 'room', true );
    


  • 2)(来自购物车对象)的购物车商品:

    2) for cart items (from the cart object):

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        // Get the WC_Product object (instance)
        $product = $cart_item['data'];
        $product_id = $product->get_id(); // get the product ID
    
        $room = get_post_meta( $product->get_id(), 'room', true );
        ## … / … 
    }
    




    • 现在产品变体,如果您需要获取父变量产品ID ,您将使用:

      • Now for product variations, if you need to get the parent variable product ID, you will use:

        // Get the parent product ID (the parent variable product)
        $parent_id = $cart_item['product_id'];
        
        $room = get_post_meta( $parent_id, 'room', true );
        ## … / … 
        


      • 3)对于订单项(来自已定义的 WC_Order 对象 $ order

        3) For order items (from a defined WC_Order object $order):

        // Loop through cart items
        foreach( $order->get_items() as $item ){
            // Get the Product ID
            $product_id = $item->get_product_id();
        
            $room = get_post_meta( $product_id, 'room', true );
            ## … / … 
        }
        




        • 现在产品变体,如果您需要获取父变量产品ID ,您将使用:

          • Now for product variations, if you need to get the parent variable product ID, you will use:

            // Loop through cart items
            foreach( $order->get_items() as $item ){
                // Get the parent Product ID
                $product = $item->get_product();
                $parent_id = $product->get_parent_id(); // The parent product ID
            
                $room = get_post_meta( $parent_id, 'room', true );
                ## … / … 
            }
            


          • 现在由您决定。

            这篇关于在WooCommerce中将产品自定义字段值获取为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆