之前遇到过这样的一个需求:如果购物车中有某个产品,则需要在Checkout页面中显示某些特殊的内容提示;
如果购物车中有某个产品,则需要在Checkout页面中显示某些特殊的内容提示;方法1:在购物车中点击 “购物车 “按钮。

方法1:

function fjj_find_product_in_cart() {
   $product_id = 282;
   $product_cart_id = WC()->cart->generate_cart_id( $product_id );
   $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
   如果 ( $in_cart ) {
      $notice = 'Product ID ' . $product_id . ' is in the Cart!';
      wc_print_notice( $notice, 'notice' );
   }
}
add_action( 'woocommerce_before_cart', 'fjj_find_product_in_cart' );

方法2:

函数 fjj_find_product_in_cart_alt() {
   $product_id = 282;
   $in_cart = false;
   foreach( WC()->cart->get_cart() as $cart_item ) {
      $product_in_cart = $cart_item['product_id'];
      如果 ( $product_in_cart === $product_id ) $in_cart = true;
   }
   如果 ( $in_cart ) {
      $notice = 'Product ID ' . $product_id . ' is in the Cart!';
      wc_print_notice( $notice, 'notice' );
   }
}
add_action( 'woocommerce_before_cart', 'fjj_find_product_in_cart_alt' );

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。