从我的插件调用Woocommerce插件的功能 [英] Call a function of Woocommerce plugin from my plugin
问题描述
我是wordpress的新手,他试图开发一个 wordpress插件,在这里我需要从类woocommerce/includes/class-wc-cart.php
调用woocommerce方法add_to_cart
.有什么办法吗?
I am new to wordpress, trying to develop a wordpress plugin where I need to call a woocommerce method add_to_cart
from the class woocommerce/includes/class-wc-cart.php
. Is there any way to do that ?
推荐答案
WooCommerce声明了一个便捷的全局WC()
,您可以在插件内部使用它来调用其功能.
WooCommerce declares a handy globlal WC()
that you can use inside your plugin to call its functions.
将以下代码添加到您的插件
Add the following code to your plugin
add_action('woocommerce_after_single_product', 'woo_foo');
function woo_foo() {
WC()->cart->add_to_cart( 254, 1 ); //ensure to change 254 with product ID on your system.
}
当您访问单个产品页面时,以上代码将自动将产品添加到购物车.以下是挂钩和清单的列表. WooCommerce提供的过滤器,您可以将其插入.
Above code will automatically add a product to the cart when you visit the single product page. Here's a list of hooks & filters offered by WooCommerce that you can hook into.
这篇关于从我的插件调用Woocommerce插件的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!