X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Ramdom Best Sellers on Home Page (https://forum.x-cart.com/showthread.php?t=47581)

mltriebe 05-16-2009 08:25 AM

Ramdom Best Sellers on Home Page
 
I have been trying to combine these 2 mods http://forum.x-cart.com/showpost.php?p=158429&postcount=1 and http://forum.x-cart.com/showthread.php?t=34157 I believe I am using Telafirma's of the Random Products mod.

I am stuck on how to get it to limit the number by using the Random Product Option setting in the admin panel. I can hard code the Qty but would prefer to select it in the admin section.

Here is the code:

Code:

<?php
#
# randoms.php based on recommends.php
#

if ( !defined('XCART_START') ) { header("Location: home.php"); die("Access denied"); }

$config["Random_Products"]["number_of_randoms"] = max(intval($config["Random_Products"]["number_of_randoms"]), 0);
#
# Defining the conditions for selecting products for the list
#
$avail_condition = "";
if ($config["General"]["unlimited_products"] == "N" && $config["General"]["disable_outofstock_products"] == "Y") {
        $avail_condition = " AND $sql_tbl[products].avail>0 ";
}
//Set Dates
$today = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));

$query_ids = array();
        $rnd = rand();
        $query_ids = func_query_column("SELECT xcart_order_details.productid, COUNT(*) AS count
                                                                FROM xcart_order_details
                                                                LEFT JOIN xcart_orders ON (xcart_order_details.orderid = xcart_orders.orderid)
                                                                WHERE xcart_order_details.productid NOT LIKE '0'
                                                                AND xcart_orders.status NOT LIKE 'F'
                                                                AND xcart_orders.status NOT LIKE 'I'
                                                                AND xcart_orders.status NOT LIKE 'D'
                                                                AND xcart_orders.status NOT LIKE 'X'
                                                                AND xcart_orders.date BETWEEN '$lastmonth' AND '$today'
                                                                GROUP BY xcart_order_details.productid
                                                                ORDER BY count DESC
                                                                LIMIT 20 ");

x_load("product");
$randoms = func_search_random_products(
        " AND $sql_tbl[products].productid IN ('".implode("','", $query_ids)."')",
        (isset($user_account) && isset($user_account["membershipid"])) ? max(intval($user_account["membershipid"]), 0) : 0,
        false,
        $config["Random_Products"]["number_of_randoms"]
);
#echo"<pre>";print_r($randoms);echo"</pre>";
if (!empty($randoms))
        $smarty->assign("randoms", $randoms);
?>


Thanks, Mike

mltriebe 05-16-2009 08:29 AM

Re: Ramdom Best Sellers on Home Page
 
The other file is func.php

[code]
Code:

<?php

if ( !defined('XCART_START') ) { header("Location: ../"); die("Access denied"); }

#
# Search for products in products database
#
function func_search_random_products($query, $membershipid, $limit="") {
        global $current_area, $user_account, $active_modules, $xcart_dir, $current_location, $single_mode;
        global $store_language, $sql_tbl;
        global $config;
        global $cart, $login;
        global $active_modules;

        x_load('files','taxes');

        #
        # Initialize service arrays
        #
        $fields = array();
        $from_tbls = array();
        $inner_joins = array();
        $left_joins = array();
        $where = array();
        $groupbys = array();

        #
        # Generate membershipid condition
        #
        $membershipid_condition = "";
        $membershipid_string = ($membershipid == 0 || empty($active_modules['Wholesale_Trading'])) ? "= 0" : "IN ('$membershipid', 0)";
        if ($current_area == "C") {
                $where[] = "($sql_tbl[category_memberships].membershipid = '$membershipid' OR $sql_tbl[category_memberships].membershipid IS NULL)";
                $where[] = "$sql_tbl[products].forsale='Y'";
                $where[] = "($sql_tbl[product_memberships].membershipid = '$membershipid' OR $sql_tbl[product_memberships].membershipid IS NULL)";
        }

        #
        # Generate products availability condition
        #
        if ($config["General"]["unlimited_products"]=="N" && (($current_area == "C" || $current_area == "B") && $config["General"]["disable_outofstock_products"] == "Y"))
                $where[] = "$sql_tbl[products].avail > 0";

        $from_tbls[] = "pricing";
        $inner_joins = array(
                "products_categories" => array(
                        "on" => "$sql_tbl[products_categories].productid = $sql_tbl[products].productid",
                ),
                "categories" => array(
                        "on" => "$sql_tbl[categories].categoryid = $sql_tbl[products_categories].categoryid AND $sql_tbl[categories].avail = 'Y'",
                )
        );
        $left_joins = array();

        $fields[] = "$sql_tbl[products].productid";
        if ($current_area == "C") {
                $left_joins["products_lng"] = array(
                        "on" => "$sql_tbl[products].productid = $sql_tbl[products_lng].productid AND code = '$store_language'"
                );
                $fields[] = "IF($sql_tbl[products_lng].productid != '', $sql_tbl[products_lng].product, $sql_tbl[products].product) as product";

        } else {
                $fields[] = "$sql_tbl[products].product";
        }

        $fields[] = "$sql_tbl[products].productcode";
        $fields[] = "$sql_tbl[products].avail";
        $fields[] = "$sql_tbl[products].descr";
        $fields[] = "$sql_tbl[products].min_amount";
        $fields[] = "$sql_tbl[products].list_price";

        if ($current_area != 'C') {
                $fields[] = "MIN($sql_tbl[pricing].price) as price";

        } else {
                $fields[] = "$sql_tbl[pricing].price";

                $left_joins['quick_prices'] = array(
                        "on" => "$sql_tbl[quick_prices].productid = $sql_tbl[products].productid AND $sql_tbl[quick_prices].membershipid $membershipid_string"
                );
                $where[] = "$sql_tbl[quick_prices].priceid = $sql_tbl[pricing].priceid";
        }

        if ($current_area == "C" && !$single_mode) {
                $inner_joins["ACHECK"] = array(
                        "tblname" => "customers",
                        "on" => "$sql_tbl[products].provider=ACHECK.login AND ACHECK.activity='Y'",
                );
        }

        $left_joins['category_memberships'] = array(
                "on" => "$sql_tbl[category_memberships].categoryid = $sql_tbl[categories].categoryid",
                "parent" => "categories"
        );
        $left_joins['product_memberships'] = array(
                "on" => "$sql_tbl[product_memberships].productid = $sql_tbl[products].productid"
        );

        $where[] = "$sql_tbl[products].productid = $sql_tbl[products_categories].productid";
        $where[] = "$sql_tbl[products_categories].categoryid = $sql_tbl[categories].categoryid";
        $where[] = "$sql_tbl[products].productid = $sql_tbl[pricing].productid";
        $where[] = "$sql_tbl[pricing].quantity = '1'";
        if (empty($membershipid)) {
                $where[] = "$sql_tbl[pricing].membershipid = 0";
        } else {
                $where[] = "$sql_tbl[pricing].membershipid IN ('$membershipid', 0)";
        }

        if ($current_area == 'C' && empty($active_modules['Product_Configurator'])) {
                $where[] = "$sql_tbl[products].product_type <> 'C'";
                $where[] = "$sql_tbl[products].forsale <> 'B'";
        }

        if ($current_area == 'C' && !empty($active_modules['Product_Options'])) {
                $where[] = "($sql_tbl[pricing].variantid = 0 OR ($sql_tbl[variants].variantid = $sql_tbl[pricing].variantid".(($config["General"]["disable_outofstock_products"] == "Y" && $config["General"]["unlimited_products"] != "Y")?" AND $sql_tbl[variants].avail > 0":"")."))";
        }
        else {
                $where[] = "$sql_tbl[pricing].variantid = '0'";
        }

        $groupbys[] = "$sql_tbl[products].productid";

        #
        # Check if product have prodyct class (Feature comparison)
        #
        if (!empty($active_modules['Feature_Comparison']) && $current_area == "C") {
                global $comparison_list_ids;

                $left_joins['product_features'] = array(
                        "on" => "$sql_tbl[product_features].productid = $sql_tbl[products].productid"
                );
                $fields[] = "$sql_tbl[product_features].fclassid";
                if (($config['Feature_Comparison']['fcomparison_show_product_list'] == 'Y') && $config['Feature_Comparison']['fcomparison_max_product_list'] > @count((array)$comparison_list_ids)) {
                        $fields[] = "IF($sql_tbl[product_features].fclassid IS NULL || $sql_tbl[product_features].productid IN ('".@implode("','",@array_keys((array)$comparison_list_ids))."'),'','Y') as is_clist";
                }
        }

        #
        # Check if product have product options (Product options)
        #
        if (!empty($active_modules['Product_Options'])) {
                $left_joins['classes'] = array(
                        "on" => "$sql_tbl[classes].productid = $sql_tbl[products].productid"
                );
                if ($current_area == 'C') {
                        $left_joins['variants'] = array(
                                "on" => "$sql_tbl[variants].productid = $sql_tbl[products].productid AND $sql_tbl[quick_prices].variantid = $sql_tbl[variants].variantid",
                        );
                        $fields[] = "$sql_tbl[quick_prices].variantid";
                        global $variant_properties;
                        foreach ($variant_properties as $property) {
                                $fields[] = "IFNULL($sql_tbl[variants].$property, $sql_tbl[products].$property) as ".$property;
                        }

                } else {
                        $left_joins['variants'] = array(
                                "on" => "$sql_tbl[variants].productid = $sql_tbl[products].productid",
                        );
                }

                $fields[] = "IF($sql_tbl[classes].classid IS NULL,'','Y') as is_product_options";
                $fields[] = "IF($sql_tbl[variants].variantid IS NULL,'','Y') as is_variant";
        }

        if ($config['setup_images']['T']['location'] == "FS") {
                $left_joins['images_T'] = array(
                        "on" => "$sql_tbl[images_T].id = $sql_tbl[products].productid"
                );
                $fields[] = "IF($sql_tbl[images_T].id IS NULL, '', 'Y') as is_thumbnail";
                $fields[] = "$sql_tbl[images_T].image_path";
        }

        if ($current_area == "C") {
                $left_joins['product_taxes'] = array(
                        "on" => "$sql_tbl[product_taxes].productid = $sql_tbl[products].productid"
                );
                $fields[] = "$sql_tbl[product_taxes].taxid";
        }

        #
        # Generate search query
        #
        foreach ($inner_joins as $j) {
                if (!empty($j['fields']) && is_array($j['fields']))
                        $fields = func_array_merge($fields, $j['fields']);
        }
        foreach ($left_joins as $j) {
                if (!empty($j['fields']) && is_array($j['fields']))
                        $fields = func_array_merge($fields, $j['fields']);
        }

        $search_query = "SELECT ".implode(", ", $fields)." FROM ";
        if (!empty($from_tbls)) {
                foreach ($from_tbls as $k => $v) {
                        $from_tbls[$k] = $sql_tbl[$v];
                }
                $search_query .= implode(", ", $from_tbls).", ";
        }
        $search_query .= $sql_tbl['products'];

        foreach ($left_joins as $ljname => $lj) {
                if (!empty($lj['parent']))
                        continue;
                $search_query .= " LEFT JOIN ";
                if (!empty($lj['tblname'])) {
                        $search_query .= $sql_tbl[$lj['tblname']]." as ".$ljname;
                } else {
                        $search_query .= $sql_tbl[$ljname];
                }
                $search_query .= " ON ".$lj['on'];
        }

        foreach ($inner_joins as $ijname => $ij) {
                $search_query .= " INNER JOIN ";
                if (!empty($ij['tblname'])) {
                        $search_query .= $sql_tbl[$ij['tblname']]." as ".$ijname;
                } else {
                        $search_query .= $sql_tbl[$ijname];
                }
                $search_query .= " ON ".$ij['on'];
                foreach ($left_joins as $ljname => $lj) {
                        if ($lj['parent'] != $ijname)
                                continue;
                        $search_query .= " LEFT JOIN ";
                        if (!empty($lj['tblname'])) {
                                $search_query .= $sql_tbl[$lj['tblname']]." as ".$ljname;
                        } else {
                                $search_query .= $sql_tbl[$ljname];
                        }
                        $search_query .= " ON ".$lj['on'];
                }
        }

        $search_query .= " WHERE ".implode(" AND ", $where).$query;
        if (!empty($groupbys))
                $search_query .= " GROUP BY ".implode(", ", $groupbys);

        $limit = max(intval($limit), 0);
        if (!empty($limit))
                $search_query .= " LIMIT ".$limit;

        db_query("SET OPTION SQL_BIG_SELECTS=1");

        $result = func_query($search_query);

        $ids = array();
        if (!empty($result)) {
                foreach($result as $v) {
                        $ids[] = $v['productid'];
                }
        }

        if ($result && ($current_area=="C" || $current_area=="B") ) {
                #
                # Post-process the result products array
                #

                if (!empty($active_modules['Extra_Fields'])) {
                        $tmp = func_query("SELECT *, IF($sql_tbl[extra_fields_lng].field != '', $sql_tbl[extra_fields_lng].field, $sql_tbl[extra_fields].field) as field FROM $sql_tbl[extra_field_values], $sql_tbl[extra_fields] LEFT JOIN $sql_tbl[extra_fields_lng] ON $sql_tbl[extra_fields].fieldid = $sql_tbl[extra_fields_lng].fieldid AND $sql_tbl[extra_fields_lng].code = '$shop_language' WHERE $sql_tbl[extra_fields].fieldid = $sql_tbl[extra_field_values].fieldid AND $sql_tbl[extra_field_values].productid IN ('".implode("','", $ids)."') AND $sql_tbl[extra_fields].active = 'Y'");
                        $products_ef = array();
                        if (!empty($tmp) && is_array($tmp)) {
                                foreach($tmp as $v) {
                                        $products_ef[$v['productid']][] = $v;
                                }
                        }
                }

                if (!empty($active_modules['Product_Options']) && !empty($ids)) {
                        $options_markups = func_get_default_options_markup_list($ids);
                }


mltriebe 05-16-2009 08:30 AM

Re: Ramdom Best Sellers on Home Page
 
And the rest of the func.php file

[code]
Code:

                foreach ($result as $key => $value) {

                        $value['taxed_price'] = $result[$key]['taxed_price'] = $value['price'];
                        if (!empty($active_modules['Product_Options']) && !empty($options_markups[$value['productid']])) {
                                # Add product options markup
                                $result[$key]['price'] += $options_markups[$value['productid']];
                                $result[$key]['taxed_price'] = $products[$key]['price'];
                                $value = $result[$key];
                        }

                        if (!empty($cart) && !empty($cart["products"]) && $current_area=="C") {
                                #
                                # Update quantity for products that already placed into the cart
                                #
                                $in_cart = 0;
                                foreach ($cart["products"] as $cart_item) {
                                        if ($cart_item["productid"] == $value["productid"] && $cart_item["variantid"] == $variant_def[$value["productid"]]['variantid'])
                                                $in_cart += $cart_item["amount"];
                                }
                                $result[$key]["avail"] -= $in_cart;
                        }

                        if (!empty($active_modules['Extra_Fields'])) {
                                if (isset($products_ef[$v['productid']])) {
                                        $result[$key]['extra_fields'] = $products_ef[$v['productid']];
                                }
                        }

                        #
                        # Get thumbnail's URL (uses only if images stored in FS)
                        #
                        $value['is_thumbnail'] = ($value['is_thumbnail'] == 'Y');
                        if ($value['is_thumbnail'] && !empty($value['image_path']))
                                $result[$key]["tmbn_url"] = func_get_image_url($value['productid'], 'T', $value['image_path']);

                        unset($result[$key]['image_path']);

                        if ($current_area == "C" && $value['taxid'] > 0) {
                                $result[$key]["taxes"] = func_get_product_taxes($result[$key], $login);
                        }
                }
        }

        return $result;
}

?>



All times are GMT -8. The time now is 07:14 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.