在 WooCommerce 中获取和使用产品类型

Get and use the product type in WooCommerce

我正在做一个项目,我一直在获取 Woocommerce 产品类型,如 'simple'、'variable'、'grouped' 或 'external'...

我想达到的效果:
在感谢页面上,显示“谢谢。您的订单已收到。”。
我想在那里显示一个特定的文本,如果产品是 'simple' 而另一个文本是产品是可变的或分组的或外部的,所以像:

if (product->get_type() == 'simple') {// (for simple product)
      //show a text
}else {// (for variable, grouped and external product) 
      //show another text
}

我已经能够使用这个:

function custome_tank_you_text($order_id) {
    $order = new WC_Order( $order_id );
    $items = $order->get_items();

    foreach ( $items as $item ) {
        $product = wc_get_product( $item['product_id'] );

        $product->get_type();
    }

    if( $product == 'simple'){ ?>
        <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you for topping up your wallet. It has been updated!', 'woocommerce' ), $order ); ?></p>
    <?php
    } else {?>
    <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received!', 'woocommerce' ), $order ); ?></p>
    <?php
    }
}
add_shortcode('thank-u-msg', 'custome_tank_you_text');

但这只会回应 Else 语句。

我做错了什么吗?

更新:

对于产品类型,您可以使用以下WC_Product methods

  • 要获取产品类型,您将使用 get_type() 方法
  • 检查 IF 语句中的产品类型,您将使用is_type() 方法

请参阅本答案末尾如何获取 WC_Product 对象实例。

自从 WooCommerce 3 以来,您的代码有点过时并且有一些错误……还要记住,一个订单可以有很多项目,因此需要打破循环(保留第一个项目)。您可以这样直接使用专用过滤器挂钩 woocommerce_thankyou_order_received_text

add_filter( 'woocommerce_thankyou_order_received_text', 'custom_thankyou_order_received_text', 20, 2 );
function custom_thankyou_order_received_text( $thankyou_text, $order ){
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Get an instance of the WC_Product Object from the WC_Order_Item_Product
        $product = $item->get_product();

        if( $product->is_type('simple') ){
            $thankyou_text = __( 'Thank you for topping up your wallet. It has been updated!', 'woocommerce' );
        } else {
            $thankyou_text = __( 'Thank you. Your order has been received!', 'woocommerce' );
        }
        break; // We stop the loop and keep the first item
    }
    return $thankyou_text;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。经过测试并有效。

相关:


添加 - 如何获取 WC_Product 对象 (使用 is_type() or get_type() 方法)

Sometimes you can't get the product type globally… as it depends on the WC_Product object.

1) From a dynamic product id variable (when you doesn't have the $product object:

$product = wc_get_product( $product_id );

or

$product = wc_get_product( get_the_id() );

2) From $product global variable on single product pages (and on product archive pages inside the loop):

global $product;

// Check that WC_Product instance $product variable is defined and accessible
if ( ! is_a( $product, 'WC_Product' ) ) {
    $product = wc_get_product( get_the_id() );

3) In cart items:

// Loop throught cart items
foreach( WC()->cart->get_cart() as $cart_item ){
    $product = $cart_item['data'];
}

3) In order items:

// Loop through order items
foreach ( $order->get_items() as $item ) {
    $product = $item->get_product();
}