Php 如何检查订单项是否有元字段';延期交货';?

Php 如何检查订单项是否有元字段';延期交货';?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,请告知如何检查新订单中某些项目是否存在“缺货”元字段。我试图在新订单上为客户添加注释,但无法找出我的功能不起作用的原因: function tt_backorder_warning_note ( $order_id ) { $tt_order = new WC_Order( $order_id ); $tt_items = $tt_order->get_items(); $tt_backorder = FALSE; $tt_backordered_not

请告知如何检查新订单中某些项目是否存在“缺货”元字段。我试图在新订单上为客户添加注释,但无法找出我的功能不起作用的原因:

function tt_backorder_warning_note ( $order_id ) { 

    $tt_order = new WC_Order( $order_id );
    $tt_items = $tt_order->get_items();
    $tt_backorder = FALSE;
    $tt_backordered_note = 'Some text to warn customer that they will wait longer';

    foreach ($tt_items as $tt_item) {
        if ($tt_item['Предзаказано']) { // Предзаказано = Backordered
            $tt_backorder = TRUE;
            break;
        }
    }

    if($tt_backorder) {
        $tt_order->add_order_note( $tt_backordered_note, $is_customer_note = 1 );
    }
}

add_action( 'woocommerce_new_order', 'tt_backorder_warning_note' );

您需要获取产品上的元数据:

function tt_backorder_warning_note ( $order_id ) { 

    $tt_order = new WC_Order( $order_id );
    $tt_items = $tt_order->get_items();
    $tt_backorder = FALSE;
    $tt_backordered_note = 'Some text to warn customer that they will wait longer';

    foreach ($tt_items as $tt_item) {

        if ($tt_item->is_on_backorder()) {
            $tt_backorder = TRUE;
            break;
        }
    }

    if($tt_backorder) {
        $tt_order->add_order_note( $tt_backordered_note);
    }
}

add_action( 'woocommerce_new_order', 'tt_backorder_warning_note' );

谢谢你的帮助,但它仍然没有添加注释。也许还有另一种方法可以检查某些产品是否缺货?实际上,我查看了WC_产品中的一个函数,名为
is_on_backorder()
。编辑我的答案来使用它。我只是从数学上做了,它做到了:```foreach($tt_order->get_items()as$item_id=>$item_data){$product=$item_data->get_product();$item_quantity=$item_data->get_quantity();$current_stock=$product->get_stock_quantity()$out\u of_stock=$current\u stock-$item\u quantity;if($out\u of_stock<0&&$product->backorders\u allowed()){$tt\u backorder=TRUE;}```