Метаданные продукта из идентификатора заказа WooCommerce - PullRequest
0 голосов
/ 14 января 2020

Попытка получить данные о продукте из последнего заказа, например [key] => pa_size

Использование automatewoo_update_print_file для вызова файла. php там, где расположены две функции, она вызывается при добавлении новой заметки в заказ:

 function automatewoo_update_print_file( $workflow ) {

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


}

Обновление Это хорошо сработало, чтобы получить самый последний идентификатор заказа и получить идентификатор продукта, затем метаданные из идентификатора, а также остальные данные заказа , Но мне все равно нужно тянуть [key] => pa_size

<code>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 '
'; echo 'Название продукта
'; print_r( $item_name ); echo '
'; echo 'Количество продукта
'; print_r( $quantity ); echo '
'; echo 'ID продукта
'; print_r( $product_id ); echo '
'; echo 'ID варианта
'; print_r( $variation_id ); echo '
'; echo 'URL файла печати
'; print_r( $print_file ); echo '
'; echo 'Статус заказа
'; print_r( $order_status ); echo '
'; echo 'ID последнего заказа
'; print_r( $latest_order_id ); echo '
'; echo 'Детали заказа
'; print_r( $order_details ); echo '
';

1 Ответ

0 голосов
/ 14 января 2020

Используйте эту функцию для получения последнего идентификатора заказа

$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();
}

Попробуйте это

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;
}

Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...