Woocommerce add_filter 仅在登录时有效 [英] Woocommerce add_filter only works when logged in

查看:25
本文介绍了Woocommerce add_filter 仅在登录时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Woocommerce Composite Products 插件提供的过滤器来更新一组产品的数量.当我登录时,此过滤器按预期工作,但当我未登录时,数量不会更新.

I use a filter provided by Woocommerce Composite Products plugin to update quantities of products in a set. When I am logged in this filter works as intended, but when I am not logged in the quantities are not updated.

我使用以下代码:

add_filter( 'woocommerce_composited_product_quantity', 'update_quantity', 10, 6);

function update_quantity($qty_value, $min_quantity, $max_quantity, $product, $component_id, $composite_product)
{

  $category = $_POST['soort'];

  $retrieve_data = WC()->session->get( 'quantities' );

  $postname = $product->post->post_name;

    if($postname == 'product-basis') {
        return 1;
    }
    else if (strpos($postname, 'product-')) {
        return 1;
    } else {
        $value = is_numeric($retrieve_data[$category][$postname]) && $retrieve_data[$category][$postname] > 0 ? $retrieve_data[$category][$postname] : 1;
        return (int)$value;
    }
}

$soort_verwarming 和 $retrieve_data 的值是可用的,这让我认为过滤器在用户未登录时以某种方式不起作用.

The values of $soort_verwarming and $retrieve_data are available, which led me thinking that the filter is somehow not working when a user is not logged in.

$retrieve_data[$category][$postname] 对应于每个产品应返回的数字并更新其数量.

$retrieve_data[$category][$postname] corresponds to a number which should be returned for each product and update its quantity.

是否存在 add_filter 对未登录用户不起作用的原因?

Are there reasons why add_filter would not work for not logged in users?

推荐答案

将此添加到我的标题中为我解决了问题:

Adding this to my header fixed the problem for me:

WC()->session->set_customer_session_cookie(true);

我在其他地方使用 Wordpress session 来保存一些我需要用来更新数量的数据.我的猜测是没有为已注销的客户设置此会话,这解释了为什么在我登录时它可以工作.

I used Wordpress session somewhere else to save some data that I needed to update the quantities with. My guess is that this session was not being set for customers that are logged out, which explains why it was working when I was logged in.

如有需要,请提供任何其他信息:https://docs.woothemes.com/wc-apidocs/source-class-WC_Session_Handler.html#73-91

Any additional info if needed: https://docs.woothemes.com/wc-apidocs/source-class-WC_Session_Handler.html#73-91

这篇关于Woocommerce add_filter 仅在登录时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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