Woocommerce add_filter only works when logged in

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.

I use the following code:

Read More
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;
    }
}

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] corresponds to a number which should be returned for each product and update its quantity.

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

Related posts

Leave a Reply

1 comment

  1. Adding this to my header fixed the problem for me:

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

    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.

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