为了账号安全,请及时绑定邮箱和手机立即绑定

限制购物车中的 WooCommerce 产品仅来自一种自定义分类

限制购物车中的 WooCommerce 产品仅来自一种自定义分类

PHP
慕娘9325324 2023-06-24 18:18:48
我试图限制 Woocommerce 商店的客户一次只能从 1 个“供应商”订购产品。我通过称为“供应商”的自定义分类来定义“供应商”。我正在尝试的代码只是出于某种原因限制了一切。function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {// If passedif ( $passed ) {        // If cart is NOT empty when a product is added         if ( !WC()->cart->is_empty() ) {                // Set vars        $current_product_tag_ids = array();        $in_cart_product_tag_ids = array();                // Get current product categories via product_id        $current_product_tag_ids = wc_get_product_term_ids( $product_id, 'supplier' );        // Loop through cart items checking for product categories        foreach ( WC()->cart->get_cart() as $cart_item ) {            // Get product categories from product in cart via cart item product id            $in_cart_product_tag_ids = array_merge( $in_cart_product_tag_ids, wc_get_product_term_ids( $cart_item['product_id'], 'product_cat' ) );        }                // Removes duplicate values        $in_cart_product_tag_ids = array_unique( $in_cart_product_tag_ids, SORT_NUMERIC );                // Compare        $compare = array_diff( $current_product_tag_ids, $in_cart_product_tag_ids );                // Result is NOT empty        if ( !empty ( $compare ) ) {            wc_add_notice( 'This product is with a different supplier. Please only order from 1 supplier at a time.', 'error' );            $passed = false;        }    }}return $passed;}add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );我并不是试图将其限制为每个供应商 1 个产品,而是试图限制他们,以便他们每个订单只能从 1 个供应商处订购产品。例如,一旦他们将供应商“供应商 1”的产品添加到购物篮中,他们将无法添加“供应商 1”以外的任何其他供应商的产品。
查看完整描述

1 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

以下内容仅允许将来自一个“供应商”的商品添加到购物车:


add_filter( 'woocommerce_add_to_cart_validation', 'only_one_supplier_by_order', 10, 2 );

function only_one_supplier_by_order( $passed, $product_id ) {

    if ( WC()->cart->is_empty() )  

        return $passed;

    

    $taxonomy = 'supplier';

    $term_ids = wp_get_post_terms( $product_id, $taxonomy, ['fields' => 'ids']);


    // Loop through cart items

    foreach ( WC()->cart->get_cart() as $cart_item ) {

        if( ! has_term( $term_ids, $taxonomy, $cart_item['product_id'] ) ) {

            // Displaying a custom notice

            wc_add_notice( __('This product is with a different supplier. Please only order from 1 supplier at a time.'), 'error' );

            return false; // Avoid add to cart

        }

    }

    return $passed;

}

现在,为了确保客户无法向购物车中的不同供应商结帐,您可以添加以下内容:


// To be sure (avoiding checkout)

add_action( 'woocommerce_check_cart_items', 'only_one_supplier_by_order_check' );

function only_one_supplier_by_order_check() {

    $taxonomy   = 'supplier';

    $term_names = []; // Initializing


    // Loop through cart items

    foreach ( WC()->cart->get_cart() as $cart_item ) {

        $terms = wp_get_post_terms( $cart_item['product_id'], $taxonomy );

        if( ! empty($terms) ) {

            $term  = reset($terms);

            

            $term_names[$term->term_id] = $term->name;  

        }

    }


    // Allow only one supplier in cart (avoid checkout for more than one

    if( count( $term_names ) > 1 ) {


        // Displaying a custom notice

        wc_add_notice( __('Only items from one supplier at the time are allowed in cart'), 'error' );

    }

}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。


查看完整回答
反对 回复 2023-06-24
  • 1 回答
  • 0 关注
  • 67 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信