pop*_*gun 0 wordpress hook woocommerce
我想根据当前购物车内容有条件地在类别页面上隐藏一组 Woocommerce 产品。我有一个名为“盒子”的类别,其中包含四种产品。其中两个也属于纸板类别,两个属于塑料类别。
如果 ID 23 的产品已在购物车中,我想显示塑料盒。如果不是,我想隐藏它们。我知道如何检查购物车内容,但是一旦我得到答案,如何从该页面隐藏塑料类别的产品?
add_action( 'woocommerce_before_shop_loop', 'my_before_shop_loop' );
function my_before_shop_loop() {
global $woocommerce;
$flag = 0;
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['data'];
if ($_product->id == '23') {
$flag = 1;
}
}
if ($flag == 0) {
// hide products that are in the plastic category
// this is where I need help
}
}
Run Code Online (Sandbox Code Playgroud)
The hook which you are using right now, is triggerred after products are fetch from database. You can filter the products from the query itself. In below code, you can pass products which you want to hide on front end.
function custom_pre_get_posts_query( $q ) {
// Do your cart logic here
// Get ids of products which you want to hide
$array_of_product_id = array();
$q->set( 'post__not_in', $array_of_product_id );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4789 次 |
最近记录: |