Get Products with exclude_pid parameter

Hello everyone,

I am trying to exclude specific products according to usergroup_id… Thus, I have this code in my func.php file (inside addon my_changes, this is a PHP hook get_products_pre from fn_get_products(..) ):

function fn_my_changes_get_products_pre($params, $items_per_page, $lang_code) {
    $auth = & Tygh::$app['session']['auth'];
    if (in_array(32,$auth['usergroup_ids'])) {
        $exclude_products = fn_my_changes_get_products_ids();
        $params['exclude_pid'] = $exclude_products;
    }
}

In function fn_my_changes_get_kare_products_ids() getting product_ids that I want to exclude according to specific variant IDs:

function fn_my_changes_get_kare_products_ids() {
    $kare_products_excluded = db_get_fields("SELECT product_id 
                                                FROM `?:product_features_values` 
                                                WHERE (`variant_id` = 137 OR `variant_id` = 394) AND lang_code = ?s", CART_LANGUAGE);
    if($kare_products_excluded) {
        return $kare_products_excluded;
    }
}

I have already checked that exclude_pid gets array… I have already tried to pass integers in array but still getting all the products…

Last but not least, the products I want to exclude is about 15K… I do not know if this makes any troubles in mysql query (I guess not, but just saying)

Any help, please?

You should pass the $params function parameter as a reference.
Replace:

function fn_my_changes_get_products_pre($params, $items_per_page, $lang_code) {

with this:

function fn_my_changes_get_products_pre(&$params, $items_per_page, $lang_code) {

As for performance, I don’t know. You can try both variants: the current one and another one where you join the product_features_values table to the original query and add the parameters to the query that should exclude unnecessary products. Which one will show the best time - that one you should use in your implementation.

1 Like

Thanks for that reference. It’s working now!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.