我已经更改了主要的WooCommerce购物车页面,使用CSS隐藏产品在这个页面。
更改的原因是,如果购物车在购物车中,我将尝试将购物车页面仅显示产品“谢谢您的提示”,而不会将任何WooCommerce产品设置更改为隐藏。
添加到购物车中的所有其他产品都需要隐藏在主WooCommerce购物车页面上。
我发现我不能用CSS实现这一点,因为我对CSS所做的更改将隐藏所有添加到购物车中的产品。
最接近于找到PHP解决方案的是以下代码段:
add_filter( 'woocommerce_cart_item_visible', 'bbloomer_hide_hidden_product_from_cart' , 10, 3 );
add_filter( 'woocommerce_widget_cart_item_visible', 'bbloomer_hide_hidden_product_from_cart', 10, 3 );
add_filter( 'woocommerce_checkout_cart_item_visible', 'bbloomer_hide_hidden_product_from_cart', 10, 3 );
add_filter( 'woocommerce_order_item_visible', 'bbloomer_hide_hidden_product_from_order_woo333', 10, 2 );
function bbloomer_hide_hidden_product_from_cart( $visible, $cart_item, $cart_item_key ) {
$product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( $product->get_catalog_visibility() == 'hidden' ) {
$visible = false;
}
return $visible;
}
function bbloomer_hide_hidden_product_from_order_woo333( $visible, $order_item ) {
$product = $order_item->get_product();
if ( $product->get_catalog_visibility() == 'hidden' ) {
$visible = false;
}
return $visible;
}然而,这并没有给出预期的结果。有什么建议吗?
发布于 2022-01-18 11:39:30
要将某些WooCommerce产品仅隐藏在购物车页面上,而不是其他地方,只需使用woocommerce_cart_item_visible过滤器钩子就够了。
在$targeted_ids数组中,您可以指示哪些产品In应该保持可见。这也适用于变量for。
function filter_woocommerce_cart_item_visible( $true, $cart_item, $cart_item_key ) {
// The targeted product ids
$targeted_ids = array( 30, 53 );
// Computes the intersection of arrays
if ( ! array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$true = false;
}
return $true;
}
add_filter( 'woocommerce_cart_item_visible', 'filter_woocommerce_cart_item_visible', 10, 3 ); 应用反向执行,并隐藏数组中发生的产品in。
替换
if ( ! array_intersect(..使用
if ( array_intersect(..https://stackoverflow.com/questions/70748667
复制相似问题