基于产品 ID 在 WooCommerce 感谢页面上添加自定义消息

我使用插件“代码片段”在 wordpress 中放入以下代码。


我用以下代码更改了字幕:


add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );

function woo_change_order_received_text( $str, $order ) {

    $new_str = 'Deine Bestellung ist bei uns eingegangen und wird umgehend von uns bearbeitet.';

    return $new_str;

}

它工作得很好,但我想在客户购买特定产品时添加不同的文本。


我发现了以下代码:


<?php $present = false; ?>

<?php foreach( $order->get_items() as $item ):  

    $_product = wc_get_product( $item['product_id'] );

         // Add whatever product id you want below here

        if ( $item['product_id'] == 643 ):  

                $present = true; ?>


<?php endif; endforeach; ?>


<?php if( $present ): ?>


    <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you for buying this shirt', '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 endif; ?>

但是当我将它添加到主题的 functions.php 或插件“代码片段”时,它失败了。


手掌心
浏览 96回答 1
1回答

神不在的星期二

您可以使用以下内容,在此答案中产品 id = 30function filter_woocommerce_thankyou_order_received_text( $str, $order ) {&nbsp; &nbsp; // Get items&nbsp; &nbsp; $items = $order->get_items();&nbsp;&nbsp; &nbsp; foreach ( $items as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; // Compare&nbsp; &nbsp; &nbsp; &nbsp; if ( $item->get_product_id() == 30 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $str = __( 'Deine Bestellung ist bei uns eingegangen und wird umgehend von uns bearbeitet.', 'woocommerce' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return $str;}add_filter( 'woocommerce_thankyou_order_received_text', 'filter_woocommerce_thankyou_order_received_text', 10, 2 );
打开App,查看更多内容
随时随地看视频慕课网APP