我正在使用 WC Marketplace 经营 WooCommerce 商店。我试图用下面的钩子实现的是,如果篮子中已经有来自不同供应商的产品,则防止将新项目添加到篮子中。例如,如果购物者将来自供应商 y 的产品 x 添加到他的购物篮中,如果他们要添加来自供应商 b 的产品 a,则不会添加该项目并且用户将被告知错误。
我有两个问题:
- 首先,钩子什么时候运行,是在主函数触发之前还是之后?我有一个函数的钩子woocommerce_add_to_cart。所以我想知道钩子会在函数woocommerce_add_to_cart运行之后还是之前触发。
- 我的第二个问题是,我在下面附上了钩子,您认为这行得通吗?
function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
$same_vendor = 1;
$empty = WC_Cart::is_empty();
//If there is an item in the cart then,
if (!$empty) {
//Get the VendorId of the product being added to the cart.
$vendor = get_wcmp_product_vendors($product_id);
$vendor_id = $vendor->id;
foreach( WC()->cart->get_cart() as $cart_item ) {
//Get the vendor Id of the item …Run Code Online (Sandbox Code Playgroud)