【问题标题】:WooCommerce: One product per category in the cartWooCommerce:购物车中每个类别一个产品
【发布时间】:2017-03-26 18:59:32
【问题描述】:

我试图阻止用户将多个特定类别的商品添加到他们的购物车中。我找到了那个功能:

function cart_has_licence_in_it()
{
  //Check to see if user has product in cart
  global $woocommerce;
  //assigns a default negative value
  $product_in_cart = false;

  foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {

    $_product = $values['data'];
    $terms    = get_the_terms($_product->id, 'product_cat');

    if ($terms) {
      foreach ($terms as $term) {
        $_categoryid = $term->term_id;

        if ($_categoryid === 23) {
          $product_in_cart = true;

        }
      }

    }
  }
  return $product_in_cart;
}

为此,我刚刚将类别 ID 号更改为 23。 现在我想检查购物车是否已经有该类别的商品,如果有,请从购物车中删除该商品并显示一条消息:

add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );

function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$categorie = get_the_terms($cart_item_data->id, 'product_cat');
$_lacat = $categorie->term_id;
if (($_lacat===23)&&(cart_has_licence_in_it())) {
        wc_add_notice('You cannot add this license to your cart because there is already another license in it. Please remove the other license from your cart first.', 'error' );
        $woocommerce->cart->remove_cart_item($cart_item_data->id);
    }
else return $cart_item_data;
}

但它不起作用,我什至没有收到消息。

由于我是一般的 WordPress 和 PHP 编码新手,我很确定我的代码中有很多错误。

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    经过多次反复试验,我设法解决了我的问题。这是解决方案。

    请注意,使用此 sn-p,新项目会覆盖旧项目,而不是警告客户他们需要删除旧项目。

    add_filter('woocommerce_add_to_cart', 'my_woocommerce_add_to_cart', 8, 6);
    
    function my_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
    global $woocommerce;
    $_category_id = 23; //put your category ID here
    
    //Doe the item you added belong to that category ?
    
    $_categorie = get_the_terms($product_id, 'product_cat');
    if ($_categorie) {
        $product_is_licence = false;
        foreach ($_categorie as $_cat) {
            $_lacat = $_cat->term_id;
            if ($_lacat === $_category_id) {
                $product_is_licence = true;
            }
        }
    }
    
    //If it does, remove all items from same category from cart
    
    if($product_is_licence){
        foreach ($woocommerce->cart->get_cart() as $cart_item_key => $value) {
            $_product = $value['data'];
            $_thisID    = $_product->id;
            $terms    = get_the_terms($_product->id, 'product_cat');
            if ($terms) {
                foreach ($terms as $term) {
                    $_categoryid = $term->term_id;
                    if (($_categoryid === $_category_id)&&($product_id !== $_thisID)) {
                        $woocommerce->cart->remove_cart_item($cart_item_key);
                        $message = sprintf( '%s has been removed from your cart.',$_product->get_title()); //displays the removal message
                        wc_add_notice($message, 'success');
                    }
                }
            }
        }
    }
    }
    

    【讨论】:

    • 如果您想要第一个行为(警告用户让他们自行删除项目),您只需更改最后一个 if 语句中的行://comment or remove this line : $woocommerce->cart->remove_cart_item($cart_item_key); $message = sprintf( 'You cannot order two items from that category; please remove %s from your cart first.',$_product->get_title());
    猜你喜欢
    • 2017-09-27
    • 2019-03-02
    • 1970-01-01
    • 2013-07-28
    • 2015-12-09
    • 1970-01-01
    • 2018-06-29
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多