Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 来自订单id的产品元数据_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 来自订单id的产品元数据

Php 来自订单id的产品元数据,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,试图从上一个订单中提取产品数据,例如[key]=>pa\u size function get_last_order_id(){ global $wpdb; $statuses = array_keys(wc_get_order_statuses()); $statuses = implode( "','", $statuses ); // Getting last Order ID (max value) $results = $wpdb->get_col( " SELECT

试图从上一个订单中提取产品数据,例如
[key]=>pa\u size

function get_last_order_id(){
global $wpdb;
$statuses = array_keys(wc_get_order_statuses());
$statuses = implode( "','", $statuses );

// Getting last Order ID (max value)
$results = $wpdb->get_col( "
    SELECT MAX(ID) FROM {$wpdb->prefix}posts
    WHERE post_type LIKE 'shop_order'
    AND post_status IN ('$statuses')
" );
return reset($results);
}

$latest_order_id = get_last_order_id(); // Last order ID
$order = wc_get_order( $latest_order_id ); // Get an instance of the WC_Order oject
$order_details = $order->get_data(); // Get the order data in an array
$order_status = $order_details['status'];


foreach ($order->get_items() as $item_key => $item ):
$product_id   = $item->get_product_id();
$variation_id = $item->get_variation_id();
$item_name    = $item->get_name(); // Name of the product
$quantity     = $item->get_quantity();  

$product        = $item->get_product(); // Get the WC_Product object

$product_price  = $product->get_price();

endforeach;

$print_file = get_post_meta( $product_id, 'print_file_url', true );

// Raw output for testing
echo 'Product Price<pre> '; print_r( $product_price ); echo '</pre>';
echo 'Product Name<pre> '; print_r( $item_name ); echo '</pre>';
echo 'Product Quantity<pre> '; print_r( $quantity ); echo '</pre>';
echo 'Product ID<pre> '; print_r( $product_id ); echo '</pre>';
echo 'Variation ID<pre> '; print_r( $variation_id ); echo '</pre>';
echo 'Print File Url<pre> '; print_r( $print_file ); echo '</pre>';
echo 'Order Status<pre>'; print_r( $order_status ); echo '</pre>';
echo 'Latest Order ID<pre>'; print_r( $latest_order_id ); echo '</pre>';
echo 'Order Details<pre>'; print_r( $order_details ); echo '</pre>';
使用
automatewoo\u update\u print\u file
调用这两个函数所在的file.php,在订单中添加新注释时调用:

 function automatewoo_update_print_file( $workflow ) {

 include '/home/***/public_html/wp-content/themes/***-child/woocommerce/checkout/file.php'; 


}
更新这可以很好地提取最新的订单ID并获取产品ID,然后是ID中的元数据以及订单数据的其余部分。但是我仍然需要拉
[key]=>pa_size

function get_last_order_id(){
global $wpdb;
$statuses = array_keys(wc_get_order_statuses());
$statuses = implode( "','", $statuses );

// Getting last Order ID (max value)
$results = $wpdb->get_col( "
    SELECT MAX(ID) FROM {$wpdb->prefix}posts
    WHERE post_type LIKE 'shop_order'
    AND post_status IN ('$statuses')
" );
return reset($results);
}

$latest_order_id = get_last_order_id(); // Last order ID
$order = wc_get_order( $latest_order_id ); // Get an instance of the WC_Order oject
$order_details = $order->get_data(); // Get the order data in an array
$order_status = $order_details['status'];


foreach ($order->get_items() as $item_key => $item ):
$product_id   = $item->get_product_id();
$variation_id = $item->get_variation_id();
$item_name    = $item->get_name(); // Name of the product
$quantity     = $item->get_quantity();  

$product        = $item->get_product(); // Get the WC_Product object

$product_price  = $product->get_price();

endforeach;

$print_file = get_post_meta( $product_id, 'print_file_url', true );

// Raw output for testing
echo 'Product Price<pre> '; print_r( $product_price ); echo '</pre>';
echo 'Product Name<pre> '; print_r( $item_name ); echo '</pre>';
echo 'Product Quantity<pre> '; print_r( $quantity ); echo '</pre>';
echo 'Product ID<pre> '; print_r( $product_id ); echo '</pre>';
echo 'Variation ID<pre> '; print_r( $variation_id ); echo '</pre>';
echo 'Print File Url<pre> '; print_r( $print_file ); echo '</pre>';
echo 'Order Status<pre>'; print_r( $order_status ); echo '</pre>';
echo 'Latest Order ID<pre>'; print_r( $latest_order_id ); echo '</pre>';
echo 'Order Details<pre>'; print_r( $order_details ); echo '</pre>';
函数get\u last\u order\u id(){
全球$wpdb;
$statuses=array_key(wc_get_order_statuses());
$statuses=内爆(“,”,$statuses);
//正在获取上一个订单ID(最大值)
$results=$wpdb->get\u col(“
从{$wpdb->prefix}帖子中选择MAX(ID)
如“店铺订单”之类的post类型
以及(“$statuses”)中的post_状态
" );
返回重置($结果);
}
$latest_order_id=get_last_order_id();//最后订单ID
$order=wc_get_order($latest_order_id);//获取WC_Order项目的实例
$order_details=$order->get_data();//获取数组中的订单数据
$order_status=$order_details['status'];
foreach($order->get\u items()作为$item\u key=>$item):
$product_id=$item->get_product_id();
$variation_id=$item->get_variation_id();
$item_name=$item->get_name();//产品名称
$quantity=$item->get_quantity();
$product=$item->get_product();//获取WC_产品对象
$product_price=$product->get_price();
endforeach;
$print\u file=get\u post\u meta($product\u id,'print\u file\u url',true);
//用于测试的原始输出
回应“产品价格”;打印(产品价格);回声';
$last_order_id = wc_get_customer_last_order($user_id);
$order = wc_get_order( $order_id );
$order->get_items();
foreach ($order->get_items() as $item_key => $item ){
    // Item ID is directly accessible from the $item_key in the foreach loop or
    $item_id = $item->get_id();
    ## Using WC_Order_Item_Product methods ##
    $product      = $item->get_product(); // Get the WC_Product object
    $product_id   = $item->get_product_id(); // the Product id
    $variation_id = $item->get_variation_id(); // the Variation id
    $item_type    = $item->get_type(); // Type of the order item ("line_item")
    $item_name    = $item->get_name(); // Name of the product
    $quantity     = $item->get_quantity();  
    $tax_class    = $item->get_tax_class();
    $line_subtotal     = $item->get_subtotal(); // Line subtotal (non discounted)
    $line_subtotal_tax = $item->get_subtotal_tax(); // Line subtotal tax (non discounted)
    $line_total        = $item->get_total(); // Line total (discounted)
    $line_total_tax    = $item->get_total_tax(); // Line total tax (discounted)
    $product        = $item->get_product(); // Get the WC_Product object
    $product_type   = $product->get_type();
    $product_sku    = $product->get_sku();
    $product_price  = $product->get_price();
    $stock_quantity = $product->get_stock_quantity();
}

echo“产品名称”;打印(项目名称);回声';
function get_names( $order_id ) {
    $order = wc_get_order( $order_id );
    if (empty($order)) return false;
    $items = $order->get_items();
    foreach ( $items as $item ) {
        $item_name    = $item->get_name();
    }
    return $item_name;
}
回声“产品数量”;打印(数量);回声'; 回显“产品ID”;打印(产品id);回声'; 回声“变异ID”;打印(变更id);回声'; 回显“打印文件Url”;print\u r($print\u file);回声'; 回显“订单状态”;打印(订单状态);回声'; 回显“最新订单ID”;打印(最新订单id);回声'; 回显“订单详情”;打印(订单详细信息);回声';
使用此功能获取上次订单id

试试这个


谢谢

“但需要更进一步”-那么就这么做吧…?(或者,问一个实际的问题——见。)感谢您对此进行调查。订单id已成功返回到
$latest\u order\u id
,但需要获取第一个产品的产品元。我发布了一个更新尝试,希望在没有运气的情况下获得第一个产品名称,理想情况下,
print\u file\u url
是我尝试接收的产品元字段。感谢您的更新,不幸的是,我无法使这两个功能都正常工作。返回的只是一个空值。我已经更新了这个问题,以显示如何从
include'../checkout/file.php'调用它
我只能通过我提供的原始函数访问order_id,假设是因为它直接查询数据库?