代码之家  ›  专栏  ›  技术社区  ›  Fjott

显示基于文本的自定义产品类别订单项目名称

  •  1
  • Fjott  · 技术社区  · 6 年前

    我在woocommerce thankyou.php中得到了以下代码,如果只购买了一个类别的产品,那么这个代码就有效了。当同时购买“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>;
         ?>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   LoicTheAztec    6 年前

    // 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")
    );
    

    测试和工作