我已使用 CSS 更改了 WooCommerce 購物車主頁面以隱藏此頁面上的產品。
更改的原因,我試圖將購物車頁面專用于僅在購物車中顯示產品“感謝您的提示”,而不將任何 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;
}
然而,這并沒有給出預期的結果。有什么建議嗎?
uj5u.com熱心網友回復:
要僅在購物車頁面上而不在其他任何地方隱藏某些 WooCommerce 產品,只需使用woocommerce_cart_item_visible過濾器掛鉤就足夠了。
在$targeted_ids陣列中,您可以指示哪些產品 ID 應該保持可見。這也適用于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 );
反向應用,并隱藏陣列中出現的 productID
代替
if ( ! array_intersect(..
和
if ( array_intersect(..
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/414429.html
標籤:
下一篇:WP_Query在頁面上產生沖突
