作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用的是 WooComerce 插件版本 2.6.11,WordPress 版本是 4.7.2。我正在尝试使用订单的产品元数据 (order_id=273) 获取所有产品。我试过如下。但是 $item['product_id']
返回空。什么都没有返回。
function custom_get_order_details($order_id){
global $woocommerce;
// for test im using $order_id = 273
$order = new WC_Order( $order_id );
foreach ( $order->get_items() as $item_id => $item ){
print_r($item_id);
echo $product_id = wc_get_order_item_meta($item_id, '_product_id', true);
echo "<br>test_30<br>";
echo $variation_id = wc_get_order_item_meta($item_id, '_variation_id', true);
echo $quantity = wc_get_order_item_meta($item_id, '_qty', true);
}
//$curl = curl_init();
//curl post here
}
add_action( 'woocommerce_payment_complete', 'custom_get_order_details', 10, 1 );
$item
在 for 循环内。它打印
array(4) { ["name"]=> string(4) "Test" ["type"]=> string(9) "line_item" ["item_meta"]=> NULL ["item_meta_array"]=> array(9) { [1]=> object(stdClass)#823 (2) { ["key"]=> string(4) "_qty" ["value"]=> string(1) "1" } [2]=> object(stdClass)#822 (2) { ["key"]=> string(10) "_tax_class" ["value"]=> string(0) "" } [3]=> object(stdClass)#821 (2) { ["key"]=> string(11) "_product_id" ["value"]=> string(4) "2856" } [4]=> object(stdClass)#820 (2) { ["key"]=> string(13) "_variation_id" ["value"]=> string(1) "0" } [5]=> object(stdClass)#819 (2) { ["key"]=> string(14) "_line_subtotal" ["value"]=> string(3) "100" } [6]=> object(stdClass)#818 (2) { ["key"]=> string(11) "_line_total" ["value"]=> string(3) "100" } [7]=> object(stdClass)#817 (2) { ["key"]=> string(18) "_line_subtotal_tax" ["value"]=> string(1) "0" } [8]=> object(stdClass)#816 (2) { ["key"]=> string(9) "_line_tax" ["value"]=> string(1) "0" } [9]=> object(stdClass)#815 (2) { ["key"]=> string(14) "_line_tax_data" ["value"]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } } }
$order->get_item_meta($item_id, '_qty', true)
最佳答案
您可以通过以下方式获取订单的订单项目详细信息
// Getting an instance of the order object
$order = new WC_Order( $order_id );
$items = $order->get_items();
//Loop through them, you can get all the relevant data:
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}
关于php - WC_Order 中的产品 ID 返回空 - WooCommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42563322/
我正在尝试构建一个应用程序,它向我发送我的 woo commerce 订单、订单项目和数量, 我已经完成了 90%, function custom_woocommerce_complete_orde
我使用的是 WooComerce 插件版本 2.6.11,WordPress 版本是 4.7.2。我正在尝试使用订单的产品元数据 (order_id=273) 获取所有产品。我试过如下。但是 $ite
我正在使用 Javascript 直接从 WooCommerce 中的 functions.php 文件(通过 Wordpress)调用 PHP 文件,该文件使用 GitHub 上提供的“PHP XL
我可以添加订单备注(私有(private)备注): $order->add_order_note($info_for_order); 但是当我尝试使用以下方式获取某些页面中的值时: get_comme
我是一名优秀的程序员,十分优秀!