简体   繁体   English

如果特定产品在 WooCommerce 购物车中,则在购物车页面中显示通知

[英]Display a notice in cart page if specific products are in WooCommerce cart

how can we make this function below to check multiple product ids instead of just one id, how can we convert it to array ?我们如何在下面使用此函数来检查多个产品 id 而不是一个 id,我们如何将其转换为数组?

add_action( 'woocommerce_before_cart', 'bbloomer_find_product_in_cart' );
    
function bbloomer_find_product_in_cart() {
  
   $product_id = 17285;
  
   $product_cart_id = WC()->cart->generate_cart_id( $product_id );
   $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
  
   if ( $in_cart ) {
  
      $notice = 'DON\'t Forget to Apply the discount coupon code you received to complete purchasing with the discounted price';
      wc_print_notice( $notice, 'notice' );
  
   }
  
}

We tried $product_ids = array("17285", "17302");我们试过$product_ids = array("17285", "17302"); // but it doesn't work // 但它不起作用

we also tried this but not working too我们也试过这个,但也不起作用

add_filter( 'woocommerce_before_cart', 'ts_woocommerce_quantity_selected_numbe', 10, 2 );
function ts_woocommerce_quantity_selected_numbe( $product ) {
    $product_ids = array("15757", "15758"); // Here the array of product Ids

    if ( in_array( $product->get_id(), $product_ids ) ) {
        // In cart
        if ( ! is_cart() ) {
            $notice = 'DON\'t Forget to Apply the discount coupon code you received to complete purchasing with the discounted price';
      wc_print_notice( $notice, 'notice' );
        } 
    }
}

To check cart items for one or multiple product Ids, displaying a notice, use instead:要检查一个或多个产品 ID 的购物车项目,显示通知,请改用:

add_action( 'woocommerce_before_cart', 'found_product_in_cart_display_message' );
function found_product_in_cart_display_message() {
    $product_ids = array(17285, 17302); // Array or product ids

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( array_intersect( $product_ids, array($item['product_id'], $item['variation_id']) ) ) {
            // Display a notice
            wc_print_notice( __("Don't forget to apply the discount coupon code you received to complete purchasing with the discounted price."), 'notice' );
            break; // Stop the loop
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

For product id array which are in cart use this code,对于购物车中的产品 ID 数组,请使用此代码,

$array_product_ids = array();

foreach( WC()->cart->get_cart() as $cart_item ){
    $product_id = $cart_item['product_id'];
    $array_product_ids =   $product_id
}
print_r($array_product_ids);    // here you find all product id which are in the cart. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在购物车中显示特定类别产品的通知? - Display notice for products of specific categories in cart? 在 Woocommerce 购物车页面上显示特定的产品属性 - Display specific product attribute on Woocommerce cart page 在 Woocommerce 单个产品的“添加到购物车”下方显示特定的运输类别 - Display specific shipping class below "Add to cart" on Woocommerce single products 在结帐 WooCommerce 页面上获取购物车产品 ID,以显示产品图片 - Get Cart products id on checkout WooCommerce page, to display product images 如何在购物车页面上显示所有 woocommerce 产品? - How can I display all woocommerce products on the cart page? WooCommerce - 未设置“<product> 已删除通知...”在购物车页面上 - WooCommerce - unset “<product> removed notice…” on cart page 在WooCommerce购物车页面上取消设置已删除的购物车项目通知 - Unset removed cart item notice on WooCommerce cart page 在 WooCommerce 购物车页面的购物车商品名称后添加产品 ID 用于特定产品 - Add the product ID after the cart item name in WooCommerce cart page for specific products 向产品添加下拉菜单并在 Woocommerce 购物车上显示价值 - Add dropdown to products and display the value on Woocommerce cart Woocommerce 如何显示购物车中产品的售价 - Woocommerce How to Display Sale Price of Products in Cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM