php - 为什么 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();
}
}
如果有人可以帮助我理解和/或解决这个问题,那将非常有帮助。
解决方案
在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() : '',
));
推荐阅读
- sql - SQL 可行性:可以按值挤出行并以自定义方式组合表吗?
- javascript - 在输入中格式化数字(掩码)
- python - 在 Python 中从字符串中去除非字母字符的代码
- flutter - 如何重建提供者选择器但不重建它们的孩子
- jenkins - 依赖于 Jenkins 构建布尔参数的表达式在管道中不起作用
- ruby-on-rails - Rails:限制用户可以上传的文件数量
- python - 如何将无穷大附加到熊猫 DatetimeIndex
- google-apps-script - 在用户制作了脚本绑定到的 gdoc 的副本后,有没有办法避免重新授权绑定的脚本?
- java - 运行 Docker 时更改配置文件中的变量值
- azure - 使用 validate-jwt 验证 jwt 令牌时如何检查错误(上一篇:如何在 validate-jwt 中使用自定义构建的 jwt?)