【问题标题】:Display a custom text based product category order item names in Woocommerce thankyou在 Woocommerce 中显示基于自定义文本的产品类别订单项目名称谢谢
【发布时间】:2018-11-02 11:00:13
【问题描述】:

我在 woocommercethankyou.php 中获得了以下代码,如果只购买 ONE 类别的产品,则此代码有效。当购买了 'ebook' 和 'ticket' 类别的产品时,'ticket' 将被添加到 $productname 变量中。

如何判断列表中的商品属于一个类别还是多个类别?

    <?php 
        $email = $order->billing_email;
        $order_info = wc_get_order( $order );
        $productname = "";

        foreach( $order_info->get_items() as $item ) {
            // check if a product is in specific category
            if ( has_term( 'ebook', 'product_cat', $item['product_id'] ) ) {
                $productname = ' ebook';
            } elseif (has_term( 'ticket', 'product_cat', $item['product_id'] )) {
                $productname = 'ticket';
            } else {
                $productname = 'order details';
            }           

        }

        echo '<p> Your ' . $productname . ' will be send to <strong>' . $email . '</strong> as soon as the payment is received.<br>;
     ?>

【问题讨论】:

  • 你到底想做什么?从您正在运行的循环来看,只会显示订单列表中的最后一个产品。

标签: php wordpress woocommerce product orders


【解决方案1】:

尝试以下方法,将您的产品名称存储在一个数组中,删除重复值并在您的消息中显示产品名称:

// Get the instance of the WC_Order Object from the $order_id variable
$order = wc_get_order( $order_id );

$product_names = array(); // Initializing

// Loop through order items
foreach( $order->get_items() as $item ) {
    // check if a product is in specific category
    if ( has_term( 'ebook', 'product_cat', $item['product_id'] ) )
    {
        $product_names[] = '"Ebook"';
    }
    elseif ( has_term( 'ticket', 'product_cat', $item['product_id'] ) )
    {
        $product_names[] = '"Ticket"';
    }
    else
    {
        $product_names[] = '"Others"';
    }
}
$product_names = array_unique( $product_names );


echo sprintf( '<p>%s %s %s <strong>%s</strong> %s</p><br>',
    _n( "Your", "Yours", sizeof( $product_names ), "woocommerce-bookings" ),
    implode( ', ', $product_names ),
    __("will be sent to", "woocommerce-bookings"),
    $order->get_billing_email(),
    __("as soon as the payment is received.", "woocommerce-bookings")
);

经过测试并且可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-21
    • 2021-06-08
    • 2019-07-21
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多