简体   繁体   English

如何在 WooCommerce 购物车页面上隐藏某些产品

[英]How to hide certain products on WooCommerce cart page

I have changed the main WooCommerce cart page using CSS to hide the products on this page.我使用 CSS 更改了主要的 WooCommerce 购物车页面以隐藏此页面上的产品。

The reason for the change, I am attempting to dedicate the cart page to only show the product "Thank You For The Tip" if it is in the cart, without changing any of the WooCommerce product settings to hidden.更改的原因,我试图将购物车页面专用于仅在购物车中显示产品“Thank You For The Tip”,而不将任何 WooCommerce 产品设置更改为隐藏。

All other products that have been added to the cart need to be hidden on the main WooCommerce cart page.已添加到购物车的所有其他产品需要隐藏在 WooCommerce 购物车主页面上。

I see that I can not achieve this with CSS as the changes I made to the CSS will hide all products that have been added to the cart.我发现使用 CSS 无法实现这一点,因为我对 CSS 所做的更改将隐藏所有已添加到购物车的产品。

The closest I have come to finding a PHP solution is the following snippet:我最接近找到 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;
}

However, this does not give the desired result.然而,这并没有给出预期的结果。 Any advice?有什么建议吗?

To hide certain WooCommerce products only on the cart page and nowhere else, it suffices to just use the woocommerce_cart_item_visible filter hook.要仅在购物车页面上而不在其他任何地方隐藏某些 WooCommerce 产品,只需使用woocommerce_cart_item_visible过滤器挂钩就足够了。

In the $targeted_ids array you can indicate which productIDs should remain visible.$targeted_ids数组中,您可以指示哪些产品 ID 应该保持可见。 This also works for variationIDs这也适用于variationID

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

To apply the reverse, and hide the productIDs that occur in the array反向应用,并隐藏数组中出现的 productID

Replace代替

if ( ! array_intersect(..

With

if ( array_intersect(..

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM