在 WooCommerce 中获取和使用产品类型 [英] Get and use the product type in WooCommerce

查看:76
本文介绍了在 WooCommerce 中获取和使用产品类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,但我一直坚持将 Woocommerce 产品类型设为简单"、可变"、分组"或外部"...

我想要达到的目标:
在谢谢"页面上,显示谢谢.您的订单已收到.".
如果产品是简单的",而另一个文本是产品是可变的、分组的或外部的,我想在那里显示一个特定的文本,例如:

if (product->get_type() == 'simple') {//(对于简单的产品)//显示一段文字}else {//(用于变量、分组和外部产品)//显示另一个文本}

我已经能够使用这个:

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 == '简单'){ ?><p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( '谢谢你给你的钱包充值.已经更新!', 'woocommerce' ), $order );?></p><?php}其他{?><p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( '谢谢.您的订单已收到!','woocommerce' ), $order );?></p><?php}}add_shortcode('thank-u-msg', 'custome_tank_you_text');

但这只会回应 Else 语句.

我做错了什么吗?

解决方案

更新:

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

相关:获取在 WooCommerce 3 中订购商品和 WC_Order_Item_Product

<小时>

附加 - 如何获取 WC_Product 对象 (使用 is_type() get_type() 方法)<块引用>

有时您无法全局获取产品类型……因为它取决于 WC_Product 对象.

1) 来自动态产品 ID 变量(当您没有 $product 对象时:

$product = wc_get_product( $product_id );

$product = wc_get_product( get_the_id() );

2) $product 全局变量在单个产品页面(以及循环内的产品存档页面):

全局$product;//检查 WC_Product 实例 $product 变量是否已定义并可访问如果 ( !is_a( $product, 'WC_Product' ) ) {$product = wc_get_product( get_the_id() );

3) 购物车内商品:

//循环遍历购物车项目foreach(WC()->cart->get_cart() 作为 $cart_item ){$product = $cart_item['data'];}

3) 按顺序排列的商品:

//遍历订单项foreach ( $order->get_items() as $item ) {$product = $item->get_product();}

I'm working on a project and I'm stuck in getting Woocommerce product types as 'simple', 'variable', 'grouped' or 'external'...

What I want to achieve:
On the Thank you page, where it says "Thank you. Your order has been received.".
I want to show a particular text there if product is 'simple' and another text is product is variable or grouped or external, so something like:

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

I have been able to use this:

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');

But this will only echo the Else statement.

Is there something I'm doing wrong?

解决方案

Updated:

For the product type, you can use the following WC_Product methods:

  • To get the product type you will use get_type() method
  • To check the product type inside an IF statement you will use is_type() method

See at the end of this answer how to get the WC_Product object instance.

Now since WooCommerce 3 your code is a bit outdated and with some errors… Also remember that an order can have many items, so it's needed to break the loop (keeping the first item). You can use directly the dedicated filter hook woocommerce_thankyou_order_received_text this way:

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;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

Related: Get Order items and WC_Order_Item_Product in WooCommerce 3


ADDITION - How to get the WC_Product object (to use is_type() or get_type() methods)

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();
}

这篇关于在 WooCommerce 中获取和使用产品类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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