在前端切换缺货项目 - woocommerce [英] Toggle out of stock items on front end - woocommerce

查看:28
本文介绍了在前端切换缺货项目 - woocommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在网站上添加一个按钮,以便用户可以打开和关闭缺货商品.默认情况下,我希望缺货商品为假.当用户浏览时,我需要他应用的设置保持一致.这可能吗?

I wish to include a button on the website so that users can toggle out of stock items on and off. By default, I want the out of stock items to be false. When the user browses around I need the setting he applies to be consistent. Is this possible?

这就是我现在所拥有的:

This is what I have till now:

/*
 * ADDING A SIMPLE BUTTON TO SHOW OR HIDE SOLD PRODUCTS
 * source: https://www.offshorly.com/news/woocommerce-show-hide-sold-products-toggle/
 */
function hide_sold_products_param() {
    global $wp;
    $wp->add_query_var('hide_sold_products');
}
add_filter('init', 'hide_sold_products_param');

add_action('pre_get_posts', 'hide_sold_products_query', 10);
function hide_sold_products_query($query){
    if($query->get('hide_sold_products') == 'true'){
        $query->set('meta_query', array(
            array(
            'key' => '_stock',
            'value' => '0',
            'compare' => '>'
            )
        ));
    }
}

我还在侧边栏上有一个按钮来切换状态.

I also have a button on the sidebar to toggle the status.

  • 目前不一致

  • Currently, it is not consistent

默认不隐藏缺货商品

非常感谢任何帮助.

推荐答案

我对此进行了更多研究,并提出了一个解决方案,虽然它并不理想,但它确实以我想要的方式工作.我认为您最大的问题是您需要将打开/关闭切换存储到用户会话中,以便在他们导航时保存.在 header.php 我添加:

I looked more into this and came up with a solution, it is not ideal but it does work the way that I wanted it too. I think your biggest issue is you need to store the toggle on/off into the users session so that it gets saved as they navigate it. In header.php I added:

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

这会存储 ?hide_sold_products= url 变量中的用户隐藏/显示值.同样,以下内容并不理想,但我会分享,还将其添加到 header.php

This stores the users hide/show value from the ?hide_sold_products= url variable. Again the following is not ideal but I'll share, also added this to header.php

<?php
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>
<?php } ?>  

这会检查用户是否尝试访问 woocommerce 中的/store/url,然后将它们重定向并添加 ?hide_sold_products= 和会话值中的 true/false,然后 url 调用 functions.php 脚本.同样在前端的切换按钮/文本上,显示切换需要附加到它:?hide_sold_products=false

This checks to see if the user is trying to access the /store/ url in woocommerce and then will redirect them and add ?hide_sold_products= with the true/false from the session value then the url calls functions.php script. Also on your toggle button/text on the front end the show toggle needs to have this appended to it: ?hide_sold_products=false

除了 JS 重定向之外,这对我来说效果很好.我尝试了 wp_redirect() 但遇到了标题已经发送的问题,因为代码在 header.php 中

Other than the JS redirection this works well for me. I tried the wp_redirect() but ran into the headers already sent issue since the code is in the header.php

这篇关于在前端切换缺货项目 - woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆