检查订单项目相关产品是否仍然存在于 WooCommerce 中
Check if the order item related product still exists in WooCommerce
我正在使用以下代码显示一系列 WooCommerce 订单的数量和产品名称:
foreach ($order->get_items() as $item_id => $item_data) {
// Get an instance of corresponding the WC_Product object
$product = $item_data->get_product();
$product_name = $product->get_name(); // Get the product name
$item_quantity = $item_data->get_quantity(); // Get the item quantity
echo $item_data->get_quantity() . ' x ' . $product->get_name() . ' (' . $product->get_sku() . ')<br />' ;
}
一切正常,但它卡在某个产品已被删除的特定订单上(因此产品 ID 不再存在)。
有什么方法可以检查这种情况并显示类似 "product no longer exists" 的内容并继续下一个产品?
以下将检查产品是否仍然存在以获取其 SKU (也处理产品变体):
// Loop through order items
foreach ($order->get_items() as $item_id => $item ) {
$product_id = (int) $item->get_product_id(); // The product ID
$variation_id = (int) $item->get_variation_id(); // The variation ID
$item_name = $item->get_name(); // Get the product name
$item_qty = $item->get_quantity(); // Get the item quantity
// Get the product SKU: Check that the product exist
if ( ( get_post_type( $product_id ) === 'product' && $variation_id === 0 )
|| ( get_post_type( $product_id ) === 'product' && $variation_id > 0
&& get_post_type( $variation_id ) === 'product_variation' ) ) {
// Get the WC_Product Object instance
$product = $item->get_product();
// Check if it is a valid WC_Product Object instance (and that the sku exist)
if ( is_a($product, 'WC_Product') && $product->get_sku() != '' ) {
$sku = ' ('.$product->get_sku().')'; // Get the sku
} else {
$sku = ''; // empty
}
} else {
$sku = ''; // empty
}
// Output
echo $item_qty . ' × ' . $item_name . $sku . '<br>';
}
已测试并有效。
注意: 对于订单商品,您可以从订单商品中获取相关产品名称,而不是 (因为它保存在订单商品本身).
我正在使用以下代码显示一系列 WooCommerce 订单的数量和产品名称:
foreach ($order->get_items() as $item_id => $item_data) {
// Get an instance of corresponding the WC_Product object
$product = $item_data->get_product();
$product_name = $product->get_name(); // Get the product name
$item_quantity = $item_data->get_quantity(); // Get the item quantity
echo $item_data->get_quantity() . ' x ' . $product->get_name() . ' (' . $product->get_sku() . ')<br />' ;
}
一切正常,但它卡在某个产品已被删除的特定订单上(因此产品 ID 不再存在)。
有什么方法可以检查这种情况并显示类似 "product no longer exists" 的内容并继续下一个产品?
以下将检查产品是否仍然存在以获取其 SKU (也处理产品变体):
// Loop through order items
foreach ($order->get_items() as $item_id => $item ) {
$product_id = (int) $item->get_product_id(); // The product ID
$variation_id = (int) $item->get_variation_id(); // The variation ID
$item_name = $item->get_name(); // Get the product name
$item_qty = $item->get_quantity(); // Get the item quantity
// Get the product SKU: Check that the product exist
if ( ( get_post_type( $product_id ) === 'product' && $variation_id === 0 )
|| ( get_post_type( $product_id ) === 'product' && $variation_id > 0
&& get_post_type( $variation_id ) === 'product_variation' ) ) {
// Get the WC_Product Object instance
$product = $item->get_product();
// Check if it is a valid WC_Product Object instance (and that the sku exist)
if ( is_a($product, 'WC_Product') && $product->get_sku() != '' ) {
$sku = ' ('.$product->get_sku().')'; // Get the sku
} else {
$sku = ''; // empty
}
} else {
$sku = ''; // empty
}
// Output
echo $item_qty . ' × ' . $item_name . $sku . '<br>';
}
已测试并有效。
注意: 对于订单商品,您可以从订单商品中获取相关产品名称,而不是 (因为它保存在订单商品本身).