WooCommerce 教程:從 $ product Object 輕鬆獲取產品信息(標題,SKU,desc)

WooCommerce 教程:從 $ product Object 輕鬆獲取產品信息(標題,SKU,desc)

我一直想出版這本指南很長一段時間。作為一個自由職業者,我每天都會重複許多操作,使我浪費時間 – 其中一個確實是 “如果我有 $ 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 用戶也希望您的參與。

文章沒看懂?代碼不會用?需要幫助您可以去論壇提問自助服務台

作者小新

大象、大象 ,你的鼻子怎麼那麼長 ,媽媽說鼻子長才是漂亮 ......