WooCommerce无法从产品类别访问购物车 [英] WooCommerce cannot access cart from product class
本文介绍了WooCommerce无法从产品类别访问购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义的WooComerce产品类型,我需要从中访问购物车网址。
I have a custom WooComerce product type, and I need to access the cart url from within it.
看起来很简单:
class WC_Product_My_Product extends WC_Product_Simple {
public function some_method() {
global $woocommerce;
$href = $woocommerce->cart->get_cart_url();
}
}
但是:
Fatal error: Call to a member function get_cart_url() on a non-object
$上调用成员函数get_cart_url() b
$ b
可能会出错吗?
What can possibily be wrong?
定义自定义产品类别时, $ woocommerce
变量不可用吗?
Is the $woocommerce
variable not available when defining a custom product class?
如果是,是否有一些内部方法/变量可以访问它? (或具体是购物车?)
If so, is there some internal method / variable to access it? (Or the cart specifically?)
推荐答案
已针对WC 3+更新
使用 $ woocommerce-> cart = new WC_Cart();
创建新对象实例显然是避免错误的解决方案:
Using $woocommerce->cart = new WC_Cart();
to create a new object instance is an apparently the solution to avoid error:
class WC_Product_My_Product extends WC_Product_Simple {
public function some_method() {
WC()->cart = new WC_Cart();
$href = WC()->cart->get_cart_url();
}
}
这篇关于WooCommerce无法从产品类别访问购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文