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" )
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改