WooCommerce 自定义 PDF 插件与订单和订单项目数据相关的问题

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

对于我的 WooCommerce 商店,我正在编写一个插件,当放置一个项目时,它会创建一个带有提货清单和交货标签的 PDF。这一切都有效,除了我似乎无法获取订单中的商品,我可以获取订单状态和订单的其他部分,但不能获取商品。

    // Getting an instance of the WC_Order object from a defined ORDER ID
    $order = wc_get_order( $order_id );

    // Iterating through each "line" items in the order
    foreach ($order->get_items() as $item_id => $item ) {

    // Get an instance of corresponding the WC_Product object
    $product        = $item->get_product();
    $product_name   = $item->get_name(); // Get the item name (product name)
    $item_quantity  = $item->get_quantity(); // Get the item quantity


    // Add item name and quantity to the PDF (Picking List)
    $pdf->Cell(0, 10, "Product name: '.$product_name.' | Quantity: '.$item_quantity.'", 0, 1);
}

我尝试将项目推送到调试日志,但它没有显示项目的详细信息,所以我认为这是我获取项目的方式,而不是将它们写入 PDF 时出现错误。

php wordpress woocommerce plugins orders
1个回答
1
投票

首先,您需要确保获得正确的订单 ID,否则什么都不起作用(正如您所描述的那样)。

尝试启用

WP_DEBUG
,如如何在 WooCommerce 3+ 中调试答案中所述。

然后在

$order = wc_get_order( $order_id );
之前插入以下 (临时的,用于调试):

error_log( 'Order_id: ' . $order_id ); // Display the order ID in the debug.log file

if ( ! ( $order_id && $order_id > 0 ) ) {
    return; // Exit if not a numerical order ID greater than 0
}

然后当您确定您拥有与订单相关的正确订单ID时,您可以删除调试代码,并禁用

WP_DEBUG


现在,最后一行代码有一点错误(双引号与单引号字符串连接问题):

// Add item name and quantity to the PDF (Picking List)
  $pdf->Cell(0, 10, "Product name: '.$product_name.' | Quantity: '.$item_quantity.'", 0, 1);

需要更换为:

// Add item name and quantity to the PDF (Picking List)
$pdf->Cell(0, 10, "Product name: {$product_name} | Quantity: {$item_quantity}", 0, 1);

或与:

// Add item name and quantity to the PDF (Picking List)
$pdf->Cell(0, 10, "Product name: ".$product_name." | Quantity: ".$item_quantity, 0, 1);

应该可以。

相关:

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