title: "WooCommerce 中的类" post_status: publish comment_status: open taxonomy: category: - woocommerce post_tag: - Core Concepts - Extensions - Repos
WooCommerce 中的类
WooCommerce 中类的列表
要查看 WooCommerce 中类的列表,请参阅 WooCommerce 代码参考。
常用类
WooCommerce
主要的类是 woocommerce,它通过全局变量 $woocommerce 提供。该类负责 WooCommerce 的主要功能,初始化其他类,存储全局变量,并处理错误/成功消息。当创建 woocommerce 类时,它会初始化以下类:
WC_Query- 存储在$woocommerce->query中WC_Customer- 存储在$woocommerce->customer中WC_Shipping- 存储在$woocommerce->shipping中WC_Payment_Gateways- 存储在$woocommerce->payment_gateways中WC_Countries- 存储在$woocommerce->countries中
其他类会根据需要自动加载。
查看 WooCommerce 类代码参考,以获取此类中包含的所有方法的完整列表。
WC_Product
WooCommerce 包含多个产品类,负责加载和输出产品数据。您可以使用 PHP 加载它们:
$product = wc_get_product( $post->ID );
在循环中,这可能并不总是必要的,因为调用 the_post() 会自动将产品数据填充到全局 $product 变量中,如果该帖子是产品的话。
查看 WC_Product 代码参考,以获取此类中包含的所有方法的完整列表。
WC_Customer
客户类允许您获取有关当前客户的数据,例如:
global $woocommerce;
$customer_country = $woocommerce->customer->get_country();
查看 WC_Customer 代码参考,以获取此类中包含的所有方法的完整列表。
WC_Cart
购物车类加载并存储用户的购物车数据,例如,要获取购物车的小计,可以使用:
global $woocommerce;
$cart_subtotal = $woocommerce->cart->get_cart_subtotal();
查看 WC_Cart 代码参考,以获取此类中包含的所有方法的完整列表。
内部类 (请勿使用)
WooCommerce 包含一些不应由扩展程序使用的内部基础设施代码:
Automattic\WooCommerce\Internal\*: 命名空间中的所有类都是内部类。不保证向后兼容性:这些类可能会在 WooCommerce 的未来版本中发生更改、被重命名或被移除。- 带有
@internal注解的类: 同样,任何带有@internal注解的类、方法或钩子,都应该由扩展程序避免使用。
使用内部代码可能会导致您的扩展程序在 WooCommerce 更新时出现问题。 任何不属于上述定义的内部代码都是公共代码,通常可以安全使用。
有关更多详情,请参阅 内部命名空间文档。