使用操作挂钩在打印的 WooCommerce 订单详细信息中显示自定义字段

问题描述 投票:0回答:1

在 WooCommerce 中,我尝试在打印订单中使用由 Checkout Field Editor for WooCommerce 插件制作的自定义字段。我们使用连接到 BizSwoop 打印服务的 BizPrint Print Manager 插件在我们的餐厅打印它们。

以下是操作挂钩的开发人员文档: https://getbizprint.com/documentation/developer/#actions

我尝试使用以下代码,但遗憾的是它不起作用:

add_action( 'Zprinttemplatesorder-htmlafterShippingDetails', 'afterShippingDetails', 10, 1 );

function afterShippingDetails( int $order_id  ): void {
    echo 'Sede de dónde sale el pedido ' . $order_id->billing_sede;
}

但是如果我将其硬编码到 html/php 中,如下所示:

<tr>
<td colspan="4"><?php _e('Sede de dónde sale el pedido :', 'Print-Google-Cloud-Print-GCP-WooCommerce'); ?>
<?= $order->billing_sede; ?></td>
</tr>

它会起作用...

如有任何帮助,我们将不胜感激。

php wordpress woocommerce printing hook
1个回答
0
投票

您的代码中有错误,请尝试以下其中一项:

  1. WordPress 方式:
add_action( 'Zprinttemplatesorder-htmlafterShippingDetails', 'get_head_office_order_origin', 10, 1 );
function get_head_office_order_origin( int $order_id  ) : void {
    printf( __('Sede de dónde sale el pedido %s'), get_post_meta( $order_id, 'billing_sede', true ) );
}
  1. WooCommerce 方式:
add_action( 'Zprinttemplatesorder-htmlafterShippingDetails', 'get_head_office_order_origin', 10, 1 );
function get_head_office_order_origin( int $order_id  ) : void {
    $order = wc_get_order( $order_id ); // Get the WC_Order object instance

    printf( __('Sede de dónde sale el pedido %s'), $order->get_meta('billing_sede') );
}

代码位于子主题的functions.php 文件中(或插件中)。两种方法都应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.