yf_shop_products_similar_by_price user module

Fileplugins/shop/modules/shop/yf_shop_products_similar_by_price.class.php
Usemodule('shop_products_similar_by_price'), URL ?object=shop_products_similar_by_price

Public properties

PropertyDefaultDescription
None declared in this class.

Public methods 1

public function products_similar_by_price($price, $id)
    public function products_similar_by_price($price, $id)
    {
        $cats_id = $prods = null;
        $price_min = floor($price - ($price * 10 / 100));
        $price_max = ceil($price + ($price * 10 / 100));
        $sql1 = 'SELECT category_id FROM ' . db('shop_product_to_category') . ' WHERE product_id =  ' . $id . '';
        $cat_id = db()->query($sql1);
        $cats_id = '';
        while ($A = db()->fetch_assoc($cat_id)) {
            $cats_id .= $A['category_id'] . ',';
        }
        $cats_id = rtrim($cats_id, ',');
        $sql2 = 'SELECT product_id FROM ' . db('shop_product_to_category') . ' WHERE category_id IN ( ' . $cats_id . ')';
        $prod = db()->query($sql2);
        $prods = '';
        while ($A = db()->fetch_assoc($prod)) {
            $prods .= $A['product_id'] . ',';
        }
        $prods = rtrim($prods, ',');
        $sql = 'SELECT * FROM ' . db('shop_products') . ' WHERE price > ' . $price_min . ' AND price < ' . $price_max . ' AND id != ' . $id . ' AND id IN(' . $prods . ')';
        $product = db()->query_fetch_all($sql);
        $items = null;
        foreach ((array) $product as $k => $product_info) {
            $thumb_path = $product_info['url'] . '_' . $product_info['id'] . '_1' . module('shop')->THUMB_SUFFIX . '.jpg';
            $URL_PRODUCT_ID = module('shop')->_product_id_url($product_info);
            $items[$product_info['id']] = [
                'name' => _prepare_html($product_info['name']),
                'price' => module('shop')->_format_price(module('shop')->_product_get_price($product_info)),
                'currency' => _prepare_html(module('shop')->CURRENCY),
                'image' => file_exists(module('shop')->products_img_dir . $thumb_path) ? module('shop')->products_img_webdir . $thumb_path : '',
                'link' => ($product_info['external_url']) ? $product_info['external_url'] : process_url('./?object=shop&action=product_details&id=' . $URL_PRODUCT_ID),
                'special' => '',
            ];
        }
        $replace = [
            'items' => $items,
            'title' => 'Similar price',
        ];
        return tpl()->parse('shop/products_similar_by_price', $replace);
    }