Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

i a littlle change to shopping cart class


tedbooks

Recommended Posts

hello i am trying to modify the shopping cart class to give the first n number of the attributes for free.I have made some modification in classes/shopping_cart.php now i get the correct price in the shopping cart box but not in the shopping cart or confirmation page

i need to change the line 418 and makae the changes necessary to $this->attributes_price($products_id to give me the free attributes look at my changes in line 347 and 373 i start my changes with //look here

<?php
 $Id: shopping_cart.php,v 1.35 2003/06/25 21:14:33 hpdl Exp $

 osCommerce, Open Source E-Commerce Solutions
 http://www.oscommerce.com

 Copyright (c) 2003 osCommerce

 Released under the GNU General Public License
*/

 class shoppingCart {
   var $contents, $total, $weight, $cartID, $content_type;

   function shoppingCart() {
     $this->reset();
   }

   function restore_contents() {
     global $customer_id, $gv_id, $REMOTE_ADDR; //rmh M-S_ccgv

     if (!tep_session_is_registered('customer_id')) return false;

// insert current cart contents in database
     if (is_array($this->contents)) {
       reset($this->contents);
       while (list($products_id, ) = each($this->contents)) {
         $qty = $this->contents[$products_id]['qty'];
//rmh M-S_multi-stores edited next line
         $product_query = tep_db_query("select cb.products_id from " . TABLE_CUSTOMERS_BASKET . " cb INNER JOIN " . TABLE_PRODUCTS_TO_STORES . " p2s ON cb.products_id = p2s.products_id AND cb.customers_basket_stores_id = p2s.stores_id where cb.customers_id = '" . (int)$customer_id . "' and cb.products_id = '" . tep_db_input($products_id) . "' and cb.customers_basket_stores_id = '" . STORES_ID . "'");
         if (!tep_db_num_rows($product_query)) {
//rmh M-S_multi-stores edited next line
           tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added, customers_basket_stores_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . $qty . "', '" . date('Ymd') . "', '" . STORES_ID . "')");
           if (isset($this->contents[$products_id]['attributes'])) {
             reset($this->contents[$products_id]['attributes']);
             while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
//rmh M-S_multi-stores edited next line
//CLR 020606 update query to pull attribute value_text. This is needed for text attributes.
      $attr_value = $this->contents[$products_id]['attributes_values'][$option];
      
               tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, customers_basket_stores_id, products_id, products_options_id, products_options_value_id, products_options_value_text) values ('" . (int)$customer_id . "', '" . STORES_ID . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value ."', '" . tep_db_input($attr_value) . "')");
             }
           }
         } else {
//rmh M-S_multi-stores edited next line
           tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $qty . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "' and customers_basket_stores_id = '" . STORES_ID . "'");
         }
       }
//rmh M-S_ccgv begin
       if (tep_session_is_registered('gv_id')) {
         $gv_query = tep_db_query("insert into  " . TABLE_COUPON_REDEEM_TRACK . " (coupon_id, customer_id, redeem_date, redeem_ip) values ('" . $gv_id . "', '" . (int)$customer_id . "', now(),'" . $REMOTE_ADDR . "')");
         $gv_update = tep_db_query("update " . TABLE_COUPONS . " set coupon_active = 'N' where coupon_id = '" . $gv_id . "'");
         tep_gv_account_update($customer_id, $gv_id);
         tep_session_unregister('gv_id');
       }
//rmh M-S_ccgv end
     }

// reset per-session cart contents, but not the database contents
     $this->reset(false);

//rmh M-S_multi-stores edited next line
     $products_query = tep_db_query("select cb.products_id, cb.customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET . " cb INNER JOIN " . TABLE_PRODUCTS_TO_STORES . " p2s ON cb.products_id = p2s.products_id AND cb.customers_basket_stores_id = p2s.stores_id where cb.customers_id = '" . (int)$customer_id . "' and cb.customers_basket_stores_id = '" . STORES_ID . "'");
     while ($products = tep_db_fetch_array($products_query)) {
       $this->contents[$products['products_id']] = array('qty' => $products['customers_basket_quantity']);
// attributes
//rmh M-S_multi-stores edited next line
       $attributes_query = tep_db_query("select products_options_id, products_options_value_id, products_options_value_text from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products['products_id']) . "' and customers_basket_stores_id = '" . STORES_ID . "'");
       while ($attributes = tep_db_fetch_array($attributes_query)) {
         $this->contents[$products['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id'];
        //CLR 020606 if text attribute, then set additional information
         if ($attributes['products_options_value_id'] == PRODUCTS_OPTIONS_VALUE_TEXT_ID) {
           $this->contents[$products['products_id']]['attributes_values'][$attributes['products_options_id']] = $attributes['products_options_value_text'];
         }
 }
     }

     $this->cleanup();
   }

   function reset($reset_database = false) {
     global $customer_id;

     $this->contents = array();
     $this->total = 0;
     $this->weight = 0;
     $this->content_type = false;

     if (tep_session_is_registered('customer_id') && ($reset_database == true)) {
//rmh M-S_multi-stores edited next 2 lines
       tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and customers_basket_stores_id = '" . STORES_ID . "'");
       tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and customers_basket_stores_id = '" . STORES_ID . "'");
     }

     unset($this->cartID);
     if (tep_session_is_registered('cartID')) tep_session_unregister('cartID');
   }

   function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
     global $new_products_id_in_cart, $customer_id;

//rmh M-S_pricing begin
     $pf = new PriceFormatter;
     $pf->loadProduct($products_id);
     $qty = $pf->adjustQty($qty);
//rmh M-S_pricing end
//rmh M-S_fixes bug_1617 begin
     $products_id_string = tep_get_uprid($products_id, $attributes);
     $products_id = tep_get_prid($products_id_string);

     if (is_numeric($products_id) && is_numeric($qty)) {
       $check_product_query = tep_db_query("select products_status from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
       $check_product = tep_db_fetch_array($check_product_query);

       if (($check_product !== false) && ($check_product['products_status'] == '1')) {
         if ($notify == true) {
           $new_products_id_in_cart = $products_id;
           tep_session_register('new_products_id_in_cart');
         }

         if ($this->in_cart($products_id_string)) {
           $this->update_quantity($products_id_string, $qty, $attributes);
         } else {
           $this->contents[] = array($products_id_string);
           $this->contents[$products_id_string] = array('qty' => $qty);
// insert into database //rmh M-S_multi-stores edited next line
           if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added, customers_basket_stores_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$qty . "', '" . date('Ymd') . "', '" . STORES_ID . "')");

           if (is_array($attributes)) {
             reset($attributes);
             while (list($option, $value) = each($attributes)) {
    //CLR 020606 check if input was from text box.  If so, store additional attribute information
           //CLR 020708 check if text input is blank, if so do not add to attribute lists
           //CLR 030228 add htmlspecialchars processing.  This handles quotes and other special chars in the user input.
           $attr_value = NULL;
           $blank_value = FALSE;
           if (strstr($option, TEXT_PREFIX)) {
             if (trim($value) == NULL)
             {
               $blank_value = TRUE;
             } else {
               $option = substr($option, strlen(TEXT_PREFIX));
               $attr_value = htmlspecialchars(stripslashes($value), ENT_QUOTES);
               $value = PRODUCTS_OPTIONS_VALUE_TEXT_ID;
               $this->contents[$products_id]['attributes_values'][$option] = $attr_value;
             }
           }

           if (!$blank_value)
           {
               $this->contents[$products_id_string]['attributes'][$option] = $value;}
// insert into database //rmh M-S_multi-stores edited next line
               if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, customers_basket_stores_id, products_id, products_options_id, products_options_value_id, products_options_value_text) values ('" . (int)$customer_id . "', '" . STORES_ID . "', '" . tep_db_input($products_id_string) . "', '" . (int)$option . "', '" . (int)$value . "', '" . tep_db_input($attr_value) . "')");
             }
           }
         }

         $this->cleanup();

// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
         $this->cartID = $this->generate_cart_id();
       }
     }
   }

   function update_quantity($products_id, $quantity = '', $attributes = '') {
     global $customer_id;
     $products_id_string = tep_get_uprid($products_id, $attributes);
     $products_id = tep_get_prid($products_id_string);

     if (is_numeric($products_id) && isset($this->contents[$products_id_string]) && is_numeric($quantity)) {
       $this->contents[$products_id_string] = array('qty' => $quantity);
// update database
//rmh M-S_multi-stores edited next line
       if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . (int)$quantity . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "' and customers_basket_stores_id = '" . STORES_ID . "'");

       if (is_array($attributes)) {
         reset($attributes);
         while (list($option, $value) = each($attributes)) {
   //CLR 020606 check if input was from text box.  If so, store additional attribute information
         //CLR 030108 check if text input is blank, if so do not update attribute lists
         //CLR 030228 add htmlspecialchars processing.  This handles quotes and other special chars in the user input.
         $attr_value = NULL;
         $blank_value = FALSE;
         if (strstr($option, TEXT_PREFIX)) {
           if (trim($value) == NULL)
           {
             $blank_value = TRUE;
           } else {
             $option = substr($option, strlen(TEXT_PREFIX));
             $attr_value = htmlspecialchars(stripslashes($value), ENT_QUOTES);
             $value = PRODUCTS_OPTIONS_VALUE_TEXT_ID;
             $this->contents[$products_id]['attributes_values'][$option] = $attr_value;
           }
         }

         if (!$blank_value)
         {
           $this->contents[$products_id_string]['attributes'][$option] = $value;}
// update database
//rmh M-S_multi-stores edited next line
           if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "', products_options_value_text = '" . tep_db_input($attr_value) . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "' and products_options_id = '" . (int)$option . "' and customers_basket_stores_id = '" . STORES_ID . "'");
         }
       }
     }
   }
//rmh M-S_fixes bug_1617 end

   function cleanup() {
     global $customer_id;

     reset($this->contents);
     while (list($key,) = each($this->contents)) {
       if ($this->contents[$key]['qty'] < 1) {
         unset($this->contents[$key]);
// remove from database
         if (tep_session_is_registered('customer_id')) {
//rmh M-S_multi-stores edited next 2 lines
           tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "' and customers_basket_stores_id = '" . STORES_ID . "'");
           tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "' and customers_basket_stores_id = '" . STORES_ID . "'");
         }
       }
     }
   }

   function count_contents() {  // get total number of items in cart
     $total_items = 0;
     if (is_array($this->contents)) {
       reset($this->contents);
       while (list($products_id, ) = each($this->contents)) {
         $total_items += $this->get_quantity($products_id);
       }
     }

     return $total_items;
   }

   function get_quantity($products_id) {
     if (isset($this->contents[$products_id])) {
       return $this->contents[$products_id]['qty'];
     } else {
       return 0;
     }
   }

   function in_cart($products_id) {
     if (isset($this->contents[$products_id])) {
       return true;
     } else {
       return false;
     }
   }

   function remove($products_id) {
     global $customer_id;
//CLR 030228 add call tep_get_uprid to correctly format product ids containing quotes
     $products_id = tep_get_uprid($products_id, $attributes);

     unset($this->contents[$products_id]);
// remove from database
     if (tep_session_is_registered('customer_id')) {
//rmh M-S_multi-stores edited next 2 lines
       tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "' and customers_basket_stores_id = '" . STORES_ID . "'");
       tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "' and customers_basket_stores_id = '" . STORES_ID . "'");
     }

// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
     $this->cartID = $this->generate_cart_id();
   }

   function remove_all() {
     $this->reset();
   }

   function get_product_id_list() {
     $product_id_list = '';
     if (is_array($this->contents)) {
       reset($this->contents);
       while (list($products_id, ) = each($this->contents)) {
         $product_id_list .= ', ' . $products_id;
       }
     }

     return substr($product_id_list, 2);
   }

   function calculate() {
     $this->total_virtual = 0; //rmh M-S_ccgv
     $this->total = 0;
     $this->weight = 0;
     if (!is_array($this->contents)) return 0;

     $pf = new PriceFormatter; //rmh M-S_pricing
     reset($this->contents);
     while (list($products_id, ) = each($this->contents)) {
       $qty = $this->contents[$products_id]['qty'];
//rmh M-S_pricing begin
// products price
//        $product_query = tep_db_query("select p.products_id, p.products_price, p.products_tax_class_id, p.products_weight from " . TABLE_PRODUCTS . " p INNER JOIN " . TABLE_PRODUCTS_TO_STORES . " p2s ON p.products_id = p2s.products_id where p.products_id = '" . (int)$products_id . "' AND p2s.stores_id = '" . STORES_ID . "'");
//        if ($product = tep_db_fetch_array($product_query)) {
       if ($product = $pf->loadProduct($products_id)){
//rmh M-S_ccgv begin
         $no_count = 1;
         $gv_query = tep_db_query("select products_model,products_free_options from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
         $gv_result = tep_db_fetch_array($gv_query);
         if (ereg('^GIFT', $gv_result['products_model'])) {
           $no_count = 0;
         }
//rmh M-S_ccgv end
         $prid = $product['products_id'];
         $products_tax = tep_get_tax_rate($product['products_tax_class_id']);
//          $products_price = $product['products_price'];
         $products_price = $pf->computePrice($qty);
         $products_weight = $product['products_weight'];

/* Handled in computePrice()
//rmh M-S_multi-stores edited next line
         $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " s where products_id = '" . (int)$prid . "' and status = '1' AND (s.stores_id = '" . STORES_ID . "')");
         if (tep_db_num_rows ($specials_query)) {
           $specials = tep_db_fetch_array($specials_query);
           $products_price = $specials['specials_new_products_price'];
         }
*/
//rmh M-S_pricing end
         $this->total_virtual += tep_add_tax($products_price, $products_tax) * $qty * $no_count; //rmh M-S_ccgv
         $this->weight_virtual += ($qty * $products_weight) * $no_count; //rmh M-S_ccgv
         $this->total += tep_add_tax($products_price, $products_tax) * $qty;
         $this->weight += ($qty * $products_weight);
       }
   $free_query = tep_db_query("select products_model,products_free_options from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
         $free_result = tep_db_fetch_array($free_query);
       

// attributes price
$z=0;
       if (isset($this->contents[$products_id]['attributes'])) {
         reset($this->contents[$products_id]['attributes']);
         while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
$z+=1;
//rmh M-S_attrib edited next line

           $attribute_price_query = tep_db_query("select options_values_price, price_prefix, options_values_weight from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$prid . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
           $attribute_price = tep_db_fetch_array($attribute_price_query);
           if ($attribute_price['price_prefix'] == '+') {
//look here i added the if statment to give the free options
       if($free_result['products_free_options']<$z)      $this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
           } else {
             $this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
           }
//rmh M-S_attrib begin
           if(!empty($attribute_price['options_values_weight'])) {
          	 $this->weight += ($qty * $attribute_price['options_values_weight']);
           }
//rmh M-S_attrib end
         }
       }
     }
   }

   function attributes_price($products_id) {
     $attributes_price = 0;
  $z=0;
  $z+=1;

     if (isset($this->contents[$products_id]['attributes'])) {
       reset($this->contents[$products_id]['attributes']);
       while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
         $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" .(int)$value  . "'");
         $attribute_price = tep_db_fetch_array($attribute_price_query);
         if ($attribute_price['price_prefix'] == '+') {
      //look here i made this condition to give the free options  
      if($free_result['products_free_options']<$z)  $attributes_price += $attribute_price['options_values_price'];
         } else {
           $attributes_price -= $attribute_price['options_values_price'];
         }
       }
     }

     return $attributes_price;
   }

   function get_products() {
     global $languages_id;

     if (!is_array($this->contents)) return false;

     $pf = new PriceFormatter; //rmh M-S_pricing
     $products_array = array();
     reset($this->contents);
     while (list($products_id, ) = each($this->contents)) {
//rmh M-S_pricing begin
/* Handled in PriceFormatter
//rmh M-S_multi-stores edited next line
       $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_image, p.products_price, p.products_weight, p.products_tax_class_id, p.distributors_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd INNER JOIN " . TABLE_PRODUCTS_TO_STORES . " p2s ON p.products_id = p2s.products_id where p.products_id = '" . (int)$products_id . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'AND p2s.stores_id = '" . STORES_ID . "'");
       if ($products = tep_db_fetch_array($products_query)) {
         $prid = $products['products_id'];
         $products_price = $products['products_price'];

//rmh M-S_multi-stores edited next line
         $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " s where products_id = '" . (int)$prid . "' and status = '1' AND (s.stores_id = '" . STORES_ID . "')");
         if (tep_db_num_rows($specials_query)) {
           $specials = tep_db_fetch_array($specials_query);
           $products_price = $specials['specials_new_products_price'];
         }
*/
       if ($products = $pf->loadProduct($products_id, $languages_id)) {
         $products_price = $pf->computePrice($this->contents[$products_id]['qty']);
//rmh M-S_pricing end
         $products_array[] = array('id' => $products_id,
                                   'name' => $products['products_name'],
                                   'model' => $products['products_model'],
                                   'image' => $products['products_image'],
                                   'price' => $products_price,
                                   'quantity' => $this->contents[$products_id]['qty'],
                                   'weight' => $products['products_weight'],
                                   'distributors_id' => $products['distributors_id'],  //rmh M-S_multi-stores
                                  //look here in the next line the price of the attributes is added but my changes doesn't change its value'
           'final_price' => ($products_price + $this->attributes_price($products_id)),
                                   'tax_class_id' => $products['products_tax_class_id'],
                              'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''),
                                   'attributes_values' => (isset($this->contents[$products_id]['attributes_values']) ? $this->contents[$products_id]['attributes_values'] : ''));
}
     }
?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...