If you use the get_id()
method, you get your item ID which is 15
in your code.
obtenir l'ID de produit:
La méthode WC_Order_Item_Product correcte pour obtenir le produit id est: get_product_id()
Obtenez le ou der ID
La méthode WC_Order_Item_Product correcte pour obtenir le numéro de commande est: get_order_id()
Obtenez l'objet WC_Product
La méthode WC_Order_Item_Product correcte pour obtenir l'objet WC_Product est: get_product()
Récupère l'objet WC_Order
Le bon WC_Order_ Item_Product méthode pour obtenir l'objet WC_order est: get_order()
Get et déprotéger les données et les méta-données utilisant WC_Data
méthodes:
get_data()
get_meta_data()
Obtenez le W objet C_Product:
$order_item_id = 15;
$order_item = new WC_Order_Item_Product($order_item_id);
// The product ID
$product_id = $order_item->get_product_id();
// The order ID
$order_id = $order_item->get_order_id();
// The WC_Product object
$product = $order_item->get_product();
// The WC_Order object
$order = $order_item->get_order();
// The item ID
$item_id = $order_item->get_id(); // which is your $order_item_id
Obtenez les éléments de commande de l'objet WC_Order:
$order_id = 156; // The order_id
// get an instance of the WC_Order object
$order = wc_get_order($order_id);
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach($order->get_items() as $item_id => $item_product){
//Get the product ID
$item_product->get_product_id();
//Get the WC_Product object
$item_product->get_product();
}
Accès et données déprotéger de WC_Order_Item_Product:
Vous peut utiliser toutes les méthodes WC_Order_Item_Product data
ou vous pouvez déprotéger les données à l'aide WC_Data
méthodes suivantes:
$order_id = 156; // The order_id
// get an instance of the WC_Order object
$order = wc_get_order($order_id);
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach($order->get_items() as $item_id => $item_product){
// Get the common data in an array:
$item_product_data_array = $item_product->get_data();
// Get the special meta data in an array:
$item_product_meta_data_array = $item_product->get_meta_data();
}
Comme référence: How to get WooCommerce order details
Je ne suis pas sûr de ce que cela fait. le mien appelle 'get_id()' que je ne sais vraiment pas si c'est l'id du produit ou un autre identifiant d'objet. Pouvez-vous élaborer sur la différence, le cas échéant ici? –
Eh bien, vous semblez retourner l'identifiant du produit, mais je crois que je dois retourner l'objet, pas l'id ... 'get_id()' vérifie s'il a un identifiant, mais l'objet est retourné pas le produit id. Et fondamentalement, je ne suis pas sûr si 'get_id()' est possible d'être utilisé dans ce contexte. Peut-être que c'est bien, je n'ai aucune idée, mais je reçois un avis PHP disant 'wc_deprecated_function' –
Merci, c'est une bonne information. Très apprécié! –