变量$cart获取WooCommerce购物车信息总数、商品等
怎样才能得到WooCommerce购物车总数或者怎样才能显示WooCommerce购物车商品或者可能是购物车费用、申请的优惠券、购物车内容总量、总重量等等,这里使用$cart 变量.
1. 如果您可以访问 $cart 变量
钩子(do_action 和 apply_filters)使用传递给函数的附加参数。如果他们允许您使用“$cart”对象,您就在做生意。
但由于这种情况非常罕见,我们将直接进入第 2 步。请记住,如果您可以使用“$cart”对象,这与“WC()->cart”对象完全相同,您可以在 WooCommerce 网站的任何前端部分全局调用它。
简而言之:
$cart = WC()->cart;
2. 如果您无权访问 $cart
如果您没有直接访问 $cart 对象的权限,您可以在 WooCommerce 网站的任何页面上全局调用它。这就是WC()->cart的美妙之处;例如,购物车页面使用此方法加载购物车对象,您也可以在任何您喜欢的地方加载。
// $cart conditionals (if) WC()->cart->is_empty() WC()->cart->needs_payment() WC()->cart->show_shipping() WC()->cart->needs_shipping() WC()->cart->needs_shipping_address() WC()->cart->display_prices_including_tax() // Get $cart totals WC()->cart->get_cart_contents_count(); WC()->cart->get_cart_subtotal(); WC()->cart->subtotal_ex_tax; WC()->cart->subtotal; WC()->cart->get_displayed_subtotal(); WC()->cart->get_taxes_total(); WC()->cart->get_shipping_total(); WC()->cart->get_coupons(); WC()->cart->get_coupon_discount_amount( 'coupon_code' ); WC()->cart->get_fees(); WC()->cart->get_discount_total(); WC()->cart->get_total(); WC()->cart->total; WC()->cart->get_tax_totals(); WC()->cart->get_cart_contents_tax(); WC()->cart->get_fee_tax(); WC()->cart->get_discount_tax(); WC()->cart->get_shipping_total(); WC()->cart->get_shipping_taxes(); // Loop over $cart items foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $product = $cart_item['data']; $product_id = $cart_item['product_id']; $quantity = $cart_item['quantity']; $price = WC()->cart->get_product_price( $product ); $subtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] ); $link = $product->get_permalink( $cart_item ); // Anything related to $product, check $product tutorial $attributes = $product->get_attributes(); $whatever_attribute = $product->get_attribute( 'whatever' ); $whatever_attribute_tax = $product->get_attribute( 'pa_whatever' ); $any_attribute = $cart_item['variation']['attribute_whatever']; $meta = wc_get_formatted_cart_item_data( $cart_item ); } // Get $cart customer billing / shipping WC()->cart->get_customer()->get_billing_first_name(); WC()->cart->get_customer()->get_billing_last_name(); WC()->cart->get_customer()->get_billing_company(); WC()->cart->get_customer()->get_billing_email(); WC()->cart->get_customer()->get_billing_phone(); WC()->cart->get_customer()->get_billing_country(); WC()->cart->get_customer()->get_billing_state(); WC()->cart->get_customer()->get_billing_postcode(); WC()->cart->get_customer()->get_billing_city(); WC()->cart->get_customer()->get_billing_address(); WC()->cart->get_customer()->get_billing_address_2(); WC()->cart->get_customer()->get_shipping_first_name(); WC()->cart->get_customer()->get_shipping_last_name(); WC()->cart->get_customer()->get_shipping_company(); WC()->cart->get_customer()->get_shipping_country(); WC()->cart->get_customer()->get_shipping_state(); WC()->cart->get_customer()->get_shipping_postcode(); WC()->cart->get_customer()->get_shipping_city(); WC()->cart->get_customer()->get_shipping_address(); WC()->cart->get_customer()->get_shipping_address_2(); // Other stuff WC()->cart->get_cross_sells(); WC()->cart->get_cart_item_tax_classes_for_shipping(); WC()->cart->get_cart_hash(); WC()->cart->get_customer();
相关文章
变量$order获取WooCommerce订单信息
在WooCommerce我怎样才能得到订单总额或者我怎样才能得到订单物品或者可能是订单 ID、客户 ID、账单信息、付款方式、总退款等等,使用变量$order可以获取。可能有 $order_id 可用。在这种...
2022-04-15使用$product获取WooCommerce产品信息ID、S
在WooCommerce中,使用变量$product 如何获取产品 SKU或者我怎样才能得到产品的简短描述或者可能是产品库存水平、运输等级、税收等级、价格、正常价格、销售价格等等.并非总是可以访问...
2022-04-15在WooCommerce列表页显示商品品牌
安装好WooCommerce Brands 插件网站将显示品牌分类,商品也会出现品牌链接,但是商品列表页如何也显示商品品牌呢?仅将这些添加到单个产品页面中。 在woomcomerce当中添加以下代码: ad...
2022-04-15在WooCommerce添加产品视频代替图片
WooCommerce 不允许您添加产品视频,但是我们可以用几行代码添加它,不需要第三方插件。 WooCommerce 产品图片用于说明您的产品,但有些产品需要视频才能更好地展示它们。在这里,我将...
2022-04-04重命名WooCommerce产品选项卡里的附加信息
系统WooCommerce产品选项卡里的附加信文字一般为Additional information,意思附加信息,但是我们可能需要修改成产品规格,文字Specifications,使用插件太过冗余,直接采用以下代码片段: a...
2022-04-04WooCommerce启用PayPal Standard标准版
WooCommerce一次次升级把PayPal Standard标准版给隐藏了,推荐用高级版,对于广大外贸独立站来说,使用高级企业版就不能提现到个人账户了,我们需要PAYPAL标准版,这里最模板提供代码如下...
2022-04-04当WooCommerce订单金额超过1000隐藏paypal
在当WooCommerce当中我们使用最多的支付工具是paypal,但是手续费太高了,4.4%加提现35美金,对于我们广大独立站来说,利润太薄了,若是出现一个更大的订单,超过1000,一下子就收走了...
2022-04-04控制WooCommerce在产品页标签数量
如果您使用 WooCommerce 产品标签,一个产品可能属于其中的 10 个或更多。发生这种情况时,单个产品页面会显示一长串标签链接,这可能会占用太多空间(请参阅下面的屏幕截图以可视化...
2022-03-28显示WooCommerce选定的变化信息如价格、重
让我们访问WooCommerce变体 ID。如果我需要读取变化价格、库存数量、SKU、重量等怎么办?那么,在这种情况下,我们需要不同的代码。...
2022-03-28变量$cart获取WooCommerce购物车信息总数、
怎样才能得到WooCommerce购物车总数或者怎样才能显示WooCommerce购物车商品或者可能是购物车费用、申请的优惠券、购物车内容总量、总重量等等,这里使用$cart 变量. 1. 如果您可以访问 ...
2022-04-15