【问题标题】:WooCommerce add product in cart with the same custom parameterWooCommerce 使用相同的自定义参数在购物车中添加产品
【发布时间】:2017-04-16 22:32:24
【问题描述】:

在我的商店中,我的产品在“添加到购物车”按钮之前带有一个复选框。 如果客户选中此复选框并添加到购物车,则该值将发送到购物车会话。

我的复选框代码:

<input type="checkbox" id="check" name="check" value="colissimo">

为了输出我的复选框值,我在 cart.php 中使用它:

$cart_item['check'];

如果有带有该复选框的产品,我想创建一个功能来检查迷你购物车。

情况1:

客户在选中复选框的情况下将产品添加到购物车。 客户转到另一种产品。 在产品中,他们有一个复选框,但客户未选中并添加到购物车。

购物车返回 false,因为它没有被检查。

情况2: 客户将未选中复选框的产品添加到购物车。 客户转到另一种产品。 在产品中,他们有一个复选框,但客户选中了复选框并添加到购物车。

购物车返回 false,因为它已被选中。

我正在尝试使用下面的代码来实现这一点,但它不起作用:

function is_product_the_same_cat($valid, $product_id, $quantity) {
    global $woocommerce;
    if($woocommerce->cart->cart_contents_count == 0){
         return true;
    }
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        $items = $woocommerce->cart->get_cart();

        $target_terms = $woocommerce->cart->get_cart();

        foreach ($items as $item) {
            $cat_ids[] =  $cart_item['check'];
        }

        foreach ($target_terms as $term) {
            $target_cat_ids[] = $cart_item['check']; 
        }           
    }
    $same_cat = array_intersect($cat_ids, $target_cat_ids);
    print_r ($same_cat);
    if(count($same_cat) > 0) return $valid;
    else {
        wc_add_notice( 'This product have not the same parameter!', 'error' );
        return false;
    }
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);

感谢您的帮助。

【问题讨论】:

    标签: php wordpress woocommerce product custom-fields


    【解决方案1】:
    function is_product_the_same_cat($valid, $product_id, $quantity) {
        global $woocommerce;
        //si panier vide ok
        if($woocommerce->cart->cart_contents_count == 0){
             return true;
        }
    
    
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    
            $cat_ids[] = $cart_item['check'];
        }
    
        $coli = $cat_ids;
    
        if ((in_array("colissimo", $coli))){
            if ((in_array("colissimo", $coli)) && (($_POST['check'])==='colissimo')){
    
                return $valid;}
    
            else {
    
                wc_add_notice( 'Les produits Colissimo font l\'objet d\'un panier séparé !', 'error' );
    
                return false;
            }
        }else{
            if(!isset ($_POST['check']) ){return $valid;}
            else {
                wc_add_notice( 'Les produits Colissimo font l\'objet d\'un panier séparé !', 'error' );
    
                return false;
            }
        }
    
    
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
    

    【讨论】:

      猜你喜欢
      • 2015-05-27
      • 2018-06-08
      • 2020-09-01
      • 2015-12-04
      • 2018-08-15
      • 2019-10-02
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多