首页 文章

Woocommerce add_filter仅在登录时有效

提问于
浏览
1

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

我使用以下代码:

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的值可用,这使我认为当用户未登录时,过滤器在某种程度上不起作用 .

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

有没有理由说add_filter不能用于未登录的用户?

1 回答

  • 0

    将此添加到我的 Headers 为我修复了问题:

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

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

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

相关问题