doulan6150 2018-10-12 15:25
浏览 61
已采纳

有条件的功能,检查产品是否已经在Woocommerce 3中的购物车中

Hi solution provided here WooCommerce - Check if item's are already in cart working perfect. This is the function code:

function woo_in_cart($arr_product_id) {
    global $woocommerce;
    $cartarray=array();

    foreach($woocommerce->cart->get_cart() as $key => $val ) {
       $_product = $val['product_id'];
       array_push($cartarray,$_product);
    }

    if (!empty($cartarray)) {
       $result = array_intersect($cartarray,$arr_product_id);
    }

    if (!empty($result)) {
       return true;
    } else {
       return false;
    };

}

Usage

  $my_products_ids_array = array(22,23,465);
if (woo_in_cart($my_products_ids_array)) {
  echo 'ohh yeah there some of that products in!';
}else {
  echo 'no matching products :(';
}

But I need use as if(in_array) but no luck so far. What i doing wrong?

$my_products_ids_array = array("69286", "69287",);
if (in_array("69286", $my_products_ids_array)) {
    echo '<p>' . the_field ( 'cart_field', 'option' ) . '</p>';
}
if (in_array("69287", $my_products_ids_array)) {
    echo '<p>' . the_field ( 'cart_field-1', 'option' ) . '</p>';
}

Thank you

  • 写回答

1条回答 默认 最新

  • dongxi1965 2018-10-12 16:55
    关注

    Your main function code is outdated.

    For Advanced custom fields (ACF):

    • you need to use get_field() (that return the field value) instead of the_field() (that echo the field value).
    • You may need to add a product ID as 2nd argument in get_field('the_slug', $product_id ).

    So try:

    function is_in_cart( $ids ) {
        // Initialise
        $found = false;
    
        // Loop Through cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            // For an array of product IDS
            if( is_array($ids) && ( in_array( $cart_item['product_id'], $ids ) || in_array( $cart_item['variation_id'], $ids ) ) ){
                $found = true;
                break;
            }
            // For a unique product ID (integer or string value)
            elseif( ! is_array($ids) && ( $ids == $cart_item['product_id'] || $ids == $cart_item['variation_id'] ) ){
                $found = true;
                break;
            }
        }
    
        return $found;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    The custom conditional function is_in_cart( $ids ) accept a string (a unique product Id) or an array of product Ids.


    Your revisited usage (ACF get_field may need a post ID (product ID)):

    if ( is_in_cart( "69286" ) ) {
        echo '<p>' . get_field ( 'cart_field' ) . '</p>'; // or get_field ( 'cart_field', "69286" )
    }
    if ( is_in_cart( "69287" ) ) {
        echo '<p>' . get_field ( 'cart_field-1' ) . '</p>'; // or get_field ( 'cart_field', "69287" )
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类