我正在寻找一个片段,可以在前端显示“显示/隐藏”缺货产品,以便客户切换产品类别页面。
我找到了这个答案(下面的链接和代码),但它对我不起作用。
非常感谢任何帮助!
Toggle out of stock items on front end - woocommerce
if (isset($_GET['hide_sold_products']) && $_GET['hide_sold_products'] === 'true') {
WC()->session->set( 'hide_sold_products', 1 );
}
if (isset($_GET['hide_sold_products']) && $_GET['hide_sold_products'] === 'false') {
WC()->session->set( 'hide_sold_products', 0 );
}
if($wp->request == 'store'){
$url = $wp->request;
} else {
$url = strstr($wp->request, '/', true);
}
if(!isset($_GET['hide_sold_products']) && $url == 'store'){
$currentUrl = "http://" . $_SERVER['HTTP_HOST'];
$hide_sold_products = WC()->session->get( 'hide_sold_products' );
if($hide_sold_products == 1){
$hide_sold_products_text = 'true';
} else {
$hide_sold_products_text = 'false';
}
?>
<script>
window.location = "<?php echo $currentUrl . '/' . $wp->request . '/?hide_sold_products=' . $hide_sold_products_text; ?>";
</script>