Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果特定产品在购物车中,则在购物车页面中显示通知_Php_Wordpress_Woocommerce_Cart_Notice - Fatal编程技术网

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

Php 如果特定产品在购物车中,则在购物车页面中显示通知,php,wordpress,woocommerce,cart,notice,Php,Wordpress,Woocommerce,Cart,Notice,如何使下面的函数检查多个产品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( $produ

如何使下面的函数检查多个产品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' );
  
   }
  
}
我们尝试了
$product_id=array(“17285”、“17302”)//但它不起作用

我们也试过了,但也不起作用

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' );
        } 
    }
}

对于购物车中的产品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. 

对于购物车中的产品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. 

要检查购物车项目是否有一个或多个产品标识,并显示通知,请改用:

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
        }
    }
}

代码进入活动子主题(或活动主题)的functions.php文件。已测试并正常工作。

要检查购物车项目是否有一个或多个产品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
        }
    }
}

代码进入活动子主题(或活动主题)的functions.php文件。经过测试且有效。

我们如何将其应用到示例中。。之后如何打印消息添加过滤器('woocommerce'u before\u cart','ts\u woocommerce\u quantity\u selected\u number',10,2);函数ts_\u商业_数量_所选_编号($product){$array_product_id=array();foreach(WC()->cart->get_cart()作为$cart_项目){$product_id=$cart_项目['product_id'];$array_product_id=$product_id}如果(在数组($product->get_id(),$array_product_id())中)//{//your notice here}}}}}}我们如何将其应用到我们的示例中..之后如何打印一个messageadd_过滤器('woocommerce_before_cart','ts_woocommerce_quantity_selected_number',10,2);函数ts_woocommerce_quantity_selected_number($product){$array_product_id=array();foreach(WC()->cart->get_cart()作为$cart_item){$product_id=$cart_item['product_id'];$array_product_id=$product_id}如果(在_数组中($product->get_id(),$array_product_id)){//在购物车中如果(!is_cart()){//您在这里的通知}}