Forums / All Other Contributions/Addons / Group Pricing Per Item - sort products by price issue, suggested code to fix

Group Pricing Per Item - sort products by price issue, suggested code to fix

Views: 2,535

Results 1 to 3 of 3
2 Mar 2011, 1:41 PM
#1
swagmani avatar

swagmani

New Zenner

Join Date:
Sep 2006
Posts:
13
Plugin Contributions:
0

Group Pricing Per Item - sort products by price issue, suggested code to fix

Hi all,

Recently a client has asked me about whether the standard product column display can sort products by price asc/desc based on group prices listed rather than the default retail price for products?

From what I could see the price column sorts using the values from the field ‘products_price_sorter’ from the products table, code for this in /zc/includes/index_filters/default_filter.php , particularily this code

case  'PRODUCT_LIST_PRICE':
                          //  $listing_sql .= "final_price " . ($sort_order == 'd' ? 'desc' : '') . ",   pd.products_name";
  $listing_sql  .= "p.products_price_sorter  " .  ($sort_order == 'd' ? 'desc' : '') . ",  pd.products_name";
  break;
```From what I can see the field  ‘products_price_sorter’ value is based on a products retail price and the ‘products_price_sorter’ field value can be updated/recalculated globally anytime via  Admin->Tools->Store Manager  or individually when inserted or updated, etc
 
 Knowing all this doesn’t help the  sorting of products via the ‘price’ column when using the wholesale group price per  item module.


 
****Test Scenario: ****
 
 You may note that if the  default retail price, (which ‘products_price_sorter’ is based on), is used to  sort by price then the products in your cart will likely 'appear' sorted asc or  desc BUT what happens if you have products with pricing like  this?

  - - - - - - - - - Retail    - Group A     - Group B
 
             Product A         - - - 70         - - - - 65         - - - - 65
 
             Product B         - - - 80         - - - - 75         - - - - 75
 
             Product C          - - - 90         - - - - 85         - - - -              70
 
 
 Since Group A discounts are  respectively inline with the retail price then the returned listed products will appear to sort  ok for them
ie p.A, p.B, p.C
 
 but if my assumption is correct  about products being sorted by retail price rather than actual group price then Group B customers will also  see products sorted like p.A, p.B, p.C
whereas as they should see p.A, p.C,  p.B


So if 'sorting by price' when using module 'group pricing per item', then consider the following code in next post...
(which I tested on an old zc install v1.3.6)

2 Mar 2011, 1:57 PM
#2
swagmani avatar

swagmani

New Zenner

Join Date:
Sep 2006
Posts:
13
Plugin Contributions:
0

Re: Group Pricing Per Item - sort products by price issue, suggested code to fix

Potential method to resolve-fix sorting by group price per item...
(note: tested on an old zc install v1.3.6, so some original code referenced here maybe a little different)

After the following updates to a copy of a live site, i was able to see updates to the new group price sorter fields in dbase when 'inserting' or 'updating' a product and also when using the global price sort updater located in Admin->Tools->Store Manager

**Step 1a) ** insert new fields in PRODUCTS table after products_sort_order

```
ALTER TABLE products ADD products_group_a_price_sorter DECIMAL( 15, 4 ) NOT NULL AFTER products_price_sorter ,
ADD products_group_b_price_sorter DECIMAL( 15, 4 ) NOT NULL AFTER products_group_a_price_sorter ,
ADD products_group_c_price_sorter DECIMAL( 15, 4 ) NOT NULL AFTER products_group_b_price_sorter ,
ADD products_group_d_price_sorter DECIMAL( 15, 4 ) NOT NULL AFTER products_group_c_price_sorter ;

">

**Step 1b)    **MODIFY function zen_update_products_price_sorter($product_id)

    IN file

    a) /admin/includes/functions/general.php

    AND ALSO IN file

    b) /includes/functions/functions_prices.php

**REPLACE THIS CODE**
```php
    function zen_update_products_price_sorter($product_id) {
    global $db;

    $products_price_sorter = zen_get_products_actual_price($product_id);

    $db->Execute("update " . TABLE_PRODUCTS . " set
     products_price_sorter='" . zen_db_prepare_input($products_price_sorter) . "'
          where products_id='" . (int)$product_id . "'");
    }
```**
WITH THIS CODE
**```php
// set the products_price_sorter
  function zen_update_products_price_sorter($product_id) {
    global $db;

    $products_price_sorter = zen_get_products_actual_price($product_id);

    $db->Execute("update " . TABLE_PRODUCTS . " set
     products_price_sorter='" . zen_db_prepare_input($products_price_sorter) . "'
          where products_id='" . (int)$product_id . "'");


    // the following is to set group price per item for sorting by price in the front end

    $product_check = $db->Execute("select products_price, products_group_a_price, products_group_b_price, products_group_c_price, products_group_d_price from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");

    if ($product_check->fields['products_group_a_price'] != 0) {
        $products_group_a_price_sorter = $product_check->fields['products_group_a_price'];
    } else {
        $products_group_a_price_sorter = zen_get_products_actual_price($product_id);
    }
    if ($product_check->fields['products_group_b_price'] != 0) {
        $products_group_b_price_sorter = $product_check->fields['products_group_b_price'];
    } else {
        $products_group_b_price_sorter = zen_get_products_actual_price($product_id);
    }
    if ($product_check->fields['products_group_c_price'] != 0) {
        $products_group_c_price_sorter = $product_check->fields['products_group_c_price'];
    } else {
        $products_group_c_price_sorter = zen_get_products_actual_price($product_id);
    }
    if ($product_check->fields['products_group_d_price'] != 0) {
        $products_group_d_price_sorter = $product_check->fields['products_group_d_price'];
    } else {
        $products_group_d_price_sorter = zen_get_products_actual_price($product_id);
    }
     $db->Execute("update " . TABLE_PRODUCTS . " set
        products_group_a_price_sorter='" . zen_db_prepare_input($products_group_a_price_sorter) . "',
        products_group_b_price_sorter='" . zen_db_prepare_input($products_group_b_price_sorter) . "',
        products_group_c_price_sorter='" . zen_db_prepare_input($products_group_c_price_sorter) . "',
        products_group_d_price_sorter='" . zen_db_prepare_input($products_group_d_price_sorter) . "'
            where products_id='" . (int)$product_id . "'");
  }
```**Step 1c) **Edit file

    /includes/index_filters/default_filter.php


    MODIFY all $listing_sql  queries, include the 4 new  fields similar to `products_group_a_price_sorter`, eg```php
    approx line 28
        $listing_sql = "select " . $select_column_list . " p.products_id, p.products_type, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, if(s.status = 1, s.specials_new_products_price, NULL) AS specials_new_products_price, IF(s.status = 1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status

    approx line 43
        $listing_sql = "select " . $select_column_list . " p.products_id, p.products_type, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, IF(s.status = 1, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status = 1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status

    approx line 58
        $listing_sql = "select " . $select_column_list . " p.products_id, p.products_type, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, IF(s.status = 1, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status = 1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status

    approx line 73
        $listing_sql = "select " . $select_column_list . " p.products_id, p.products_type, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, IF(s.status = 1, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status =1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status
```**REPLACE ALL ABOVE LINES WITH CODE**
```php
$listing_sql = "select " . $select_column_list . " p.products_id, p.products_type, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, IF(s.status = 1, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status = 1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.products_group_a_price_sorter, p.products_group_b_price_sorter, p.products_group_c_price_sorter, p.products_group_d_price_sorter, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status
```AND ALSO in file  /includes/index_filters/default_filter.php

**REPLACE THIS CODE**
```php
        case 'PRODUCT_LIST_PRICE':
        //    $listing_sql .= "final_price " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
            $listing_sql .= "p.products_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
        break
```**WITH THIS CODE**
```php
    case 'PRODUCT_LIST_PRICE':
    //          $listing_sql .= "final_price " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";

        if($_SESSION['customer_id']) {
            $customer_group_query = "select gp.group_name
                         from " . TABLE_CUSTOMERS . " cu
                         left join " . TABLE_GROUP_PRICING . " gp on cu.customers_group_pricing=gp.group_id
                         where cu.customers_id = " . $_SESSION['customer_id'];
            if($customer_group = $db->Execute($customer_group_query)) {
                $customers_group=$customer_group->fields['group_name'];
            }
        }
        if($customers_group) {
            if ($customers_group == GROUP_PRICE_PER_ITEM1) {
                $listing_sql .= "p.products_group_a_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
            }
            if ($customers_group == GROUP_PRICE_PER_ITEM2) {
                $listing_sql .= "p.products_group_b_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
            }
            if ($customers_group == GROUP_PRICE_PER_ITEM3) {
                $listing_sql .= "p.products_group_c_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
            }
            if ($customers_group == GROUP_PRICE_PER_ITEM4) {
                $listing_sql .= "p.products_group_d_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
            }
        } else {
            $listing_sql .= "p.products_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
        }
      break;
```Hope this was helpful to someone...
:cool:
2 Mar 2011, 3:14 PM
#3
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Group Pricing Per Item - sort products by price issue, suggested code to fix

Thanks for posting the details on how you managed this listing for the Price sorts on Group Pricing ... this is of great help to others ... :smile:

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!