首页 > 解决方案 > 为什么 WooCommerce 订单简码会在自定义感谢页面上生成插件通知?

问题描述

order-received通过使用我的子主题和functions.php.

重定向按预期工作,我创建的所有其他短代码都可以正常工作,除了这个。

[order_cost_breakdown]在我的页面上使用 Gutenberg插入我的简码Thanks并下订单时,我收到以下通知

Notice: Undefined variable: show_purchase_note in /wp-content/plugins/woocommerce/templates/order/order-details-item.php on line 60

我不确定我需要在该模板中编辑什么才能使其工作,或者我需要在我的简码函数中更改什么才能使其工作。

这是简码功能:

function order_cost_breakdown(){

    if ( isset( $_GET['order_id']) && $_GET['order_id'] > 0 ) {

        $order_id = (int) esc_attr( $_GET['order_id'] );

        $order = wc_get_order( $order_id );

        $order_data = $order->get_data();

    ob_start();
    
    ?>

        <div class="woocommerce-account woocommerce-page"><div class="woocommerce">

            <table class="shop_table order_details">

                <thead>

                    <tr>
                        <th class="product-name"><?php _e( 'Product', 'woocommerce' ); ?></th>
                        <th class="product-total"><?php _e( 'Total', 'woocommerce' ); ?></th>

                    </tr>

                </thead>

            <tbody>

        <?php foreach ( $order->get_items() as $item_id => $item ) {

        wc_get_template( 'order/order-details-item.php', array (

            'order' => $order,
            'item_id' => $item_id,
            'item' => $item,
            'product' => apply_filters( 'woocommerce_order_item_product', $item->get_product( $item ), $item ) ) );
        }
    ?>

    <?php do_action( 'woocommerce_order_items_table', $order ); ?>

            </tbody>
        
        <tfoot>

    <?php

        foreach ( $order->get_order_item_totals() as $key => $total ){
    ?>

    <tr>
    
        <th scope="row"><?php echo $total['label']; ?></th>
        <td><?php echo $total['value']; ?></td>
    </tr>

        <?php
    }

    ?>
            </tfoot>
    
        </table>
    
    </div>

</div>

<?php

    return ob_get_clean();
    
    }

}

如果有人可以帮助我理解和/或解决这个问题,那将非常有帮助。

标签: phpwoocommerceshortcode

解决方案


在woocommerce的源码中可以看到:https ://github.com/woocommerce/woocommerce/blob/00e38b51ef9276f0063d9df7796f19b92ca3ff76/templates/order/order-details.php ,有这个order level $show_purchase_note,传递给每一个item在预测循环中查看。这控制是否应显示产品级别的购买说明(如果有)。你应该这样做:

弄清楚它的价值:

$show_purchase_note    = $order->has_status( apply_filters( 'woocommerce_purchase_note_order_statuses', array( 'completed', 'processing' ) ) );

并将它与产品购买说明的值一起发送到循环中的每个项目视图(注意最后两个模板参数):


wc_get_template( 'order/order-details-item.php', array (
            'order' => $order,
            'item_id' => $item_id,
            'item' => $item,
            'product' => apply_filters( 'woocommerce_order_item_product', $item->get_product( $item ), $item ),
            'show_purchase_note' => $show_purchase_note,
            'purchase_note'      => $product ? $product->get_purchase_note() : '',
));

推荐阅读