我一直想出版這本指南很長一段時間。作為一個自由職業者,我每天都會重複許多操作,使我浪費時間 – 其中一個確實是 “如果我有 $ product 變數/物件,如何獲得____” 。

例如,“如何獲得產品 SKU”?或 “如何獲取產品簡短說明”?或者也許產品庫存水平,運輸類,稅收類,價格,正常價格,銷售價格等等… 希望這篇文章可以節省您的時間🙂

您可以使用 $產品

Hooks(do_action 和 apply_filters)使用傳遞給函式的其他引數。如果他們允許您使用您的 “$ product” 物件。或者,您可以在函式中宣告 “全域性 $ product” 。

在這兩種情況下,如何獲取所有產品資訊:

 // Get Product info if $product is available to you  $product->get_id(); (fixes the error: "Notice: id was called incorrectly. Product properties should not be accessed directly") $product->get_type(); $product->get_name(); $product->get_slug(); $product->get_date_created(); $product->get_date_modified(); $product->get_status(); $product->get_featured(); $product->get_catalog_visibility(); $product->get_description(); $product->get_short_description(); $product->get_sku(); $product->get_price(); $product->get_regular_price(); $product->get_sale_price(); $product->get_date_on_sale_from(); $product->get_date_on_sale_to(); $product->get_total_sales(); $product->get_tax_status(); $product->get_tax_class(); $product->get_manage_stock(); $product->get_stock_quantity(); $product->get_stock_status(); $product->get_backorders(); $product->get_sold_individually(); $product->get_weight(); $product->get_length(); $product->get_width(); $product->get_height(); $product->get_dimensions(); $product->get_upsell_ids(); $product->get_cross_sell_ids(); $product->get_parent_id(); $product->get_reviews_allowed(); $product->get_purchase_note(); $product->get_attributes(); $product->get_default_attributes(); $product->get_menu_order(); $product->get_category_ids(); $product->get_tag_ids(); $product->get_virtual(); $product->get_gallery_image_ids(); $product->get_shipping_class_id(); $product->get_downloads(); $product->get_download_expiry(); $product->get_downloadable(); $product->get_download_limit(); $product->get_image_id(); $product->get_rating_counts(); $product->get_average_rating(); $product->get_review_count();  // source: https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html  

您可以訪問 $ product_id

If you have access to the product ID (once again, usually the do_action or apply_filters will make this possible to you), you have to get the product object first. Then, do the exact same things as above.

 // Get $product object from product ID  $product = wc_get_product( id );  // Now you have access to (see above)...  $product->get_type(); $product->get_name(); // etc. // etc.  

3. You have access to the Order object or Order ID

How to get the product information inside the Order? In this case you will need to loop through all the items present in the order, and then apply the rules above.

 // Get $product object from $order / $order_id  $order = new WC_Order( $order_id ); $items = $order->get_items();  foreach ( $items as $item ) {      $product = wc_get_product( $item['product_id'] );      // Now you have access to (see above)...      $product->get_type();     $product->get_name();     // etc.     // etc.  }  

4. You have access to the Cart object

How to get the product information inside the Cart? In this case, once again, you will need to loop through all the items present in the cart, and then apply the rules above.

 // Get $product object from Cart object  $cart = WC()->cart->get_cart();  foreach( $cart as $cart_item ){      $product = wc_get_product( $cart_item['product_id'] );      // Now you have access to (see above)...      $product->get_type();     $product->get_name();     // etc.     // etc.  }  


可以在哪裡新增此程式碼?

您可以將 PHP 程式碼片段放置在主題或子主題的 functions.php 檔案的底部(如果是 CSS 程式碼,請新增到主題的 style.css 檔案底部),修改之前建議先備份原始檔案,若出現錯誤請先刪除此程式碼。


這段程式碼是否正常可用?

或者是您有更好的解決方案想要分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 使用者也希望您的參與。


需要關於 WooCommerce 的幫助?

請觀看我們提供的免費影片教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。