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 用户也希望您的参与。

文章没看懂?代码不会用?需要帮助您可以去论坛提问自助服务台

作者小新

大象、大象 ,你的鼻子怎么那么长 ,妈妈说鼻子长才是漂亮 ......