Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

calculate total price of product attributes in cart for shipping modukle


JSR

Recommended Posts

I've been looking for a way to exclude a shipping module based on the total price of all product attributes in the shopping cart.

If the total price of product attributes is higher than zero, only one shipping method should be available.

I have 2 flat rates and I've tried to exclude one by adding in includes/modules/shipping/flat.php:

 

if ($cart->calculate_price[$attribute_price]['total'] < 4.99){

$this->enabled = true;

}else{

$this->enabled = false;

}

 

 

This isn't working...

Does anyone know how to effectively go about this? :huh:

Link to comment
Share on other sites

Find:

 

       if ($check_flag == false) {
         $this->enabled = false;
       }
     }

 

After try this;

 

       if($this->enabled == false && $order->info['total'] >= whatever amount) $this->enabled = true;

 

Or After try this;

 

       if($this->enabled == true && $order->info['total'] < whatever amount) $this->enabled = false;

 

Or After if you like try this:

       if($order->info['total'] < 4.99){
          $this->enabled = true;
         }else{
          $this->enabled = false;
         }

 

Fooling around with $order->info['total'] should get what you want.

Link to comment
Share on other sites

Hi KDM,

 

Thanks for replying! I already tried that route before I decided to go with the shopping cart class ($cart->calculate_price)...

I tried $order->products[$i][$attributes]['total'] but that just gets you the total of attributes added to each product in the order...

What I need is the total price of all added attributes... $order->info['total'] gets you the total amount of the order (products prices+attributes prices)

You follow me?

Link to comment
Share on other sites

Hi KDM,

 

Thanks for replying! I already tried that route before I decided to go with the shopping cart class ($cart->calculate_price)...

I tried $order->products[$i][$attributes]['total'] but that just gets you the total of attributes added to each product in the order...

What I need is the total price of all added attributes... $order->info['total'] gets you the total amount of the order (products prices+attributes prices)

You follow me?

 

I'm not sure exactly what you are looking for. I imagine you have already added the $cart object as global in flat.php. If so you can call any function in $cart that will give you what you want and your idea should work.

 

Inside flat.php find:

// class constructor
   function flat() {
     global $order;

 

add the $cart class last this:

// class constructor
   function flat() {
     global $order, $cart;

 

With my products I don't use attributes. I haven't checked but your logic at that point should work.

Good Luck.

Link to comment
Share on other sites

  • 1 month later...

Hi KDM,

 

Sorry for my belated response! I still didn't get this to work, which is odd to me cause it seems pretty straight forward.

So what I was saying is: If there are attributes assigned to the products in the cart and the total amount of these attributes is more then 5 dollars, flat shipping needs to be disabled. I've tried all sorts of things to get this to work, but no luck yet. So confusing...

What would be the way to go if I need the total value of all attributes in the cart? *sigh*

Link to comment
Share on other sites

Hi KDM,

 

If there are attributes assigned to the products in the cart and the total amount of these attributes is more then 5 dollars,

I'm still not sure why you are so concerned with attributes as compared to total price. This is what I understand of attributes and their prices.

Example:

T-Shirt - product id "1"

product id "1" small size (attribute) $12

product id "1" medium size (attribute) $14

product id "1" large size (attribute) $16

product id "1" ex-large size (attribute) $19

 

Now regardless of size (attribute) the customer buys the correct price will go to the cart/order, so only if you need the attribute brought for some kind of special shipping I can not image that the attribute makes a different in the disable/enable of shipping module.

 

I have 2 flat rates and I've tried to exclude one by adding in includes/modules/shipping/flat.php:

 

This sounds like you have two flat shipping modules of which you want to disable/enable one based upon price.

I would use just one flat shipping module with logic added in to set the price by whatever your criteria is. You are basing that upon price/attribute?

 

 

Based upon the flat.php below:

// class methods
   function quote($method = '') {
     global $order;

     $this->quotes = array('id' => $this->code,
                           'module' => MODULE_SHIPPING_FLAT_TEXT_TITLE,
                           'methods' => array(array('id' => $this->code,
                                                    'title' => MODULE_SHIPPING_FLAT_TEXT_WAY,
                                                    'cost' => MODULE_SHIPPING_FLAT_COST)));

 

I would place some logic similar to below.

// class methods
   function quote($method = '') {
     global $order;

     $cost = ($order->info['subtotal'] < 4.99) ? "your set cost here" : MODULE_SHIPPING_FLAT_COST;

     $this->quotes = array('id' => $this->code,
                           'module' => MODULE_SHIPPING_FLAT_TEXT_TITLE,
                           'methods' => array(array('id' => $this->code,
                                                    'title' => MODULE_SHIPPING_FLAT_TEXT_WAY,
                                                    'cost' => $cost)));

The logic above is similar to a "if" statement. So above if subtotal was less then 4.99 it would take whatever value you set in "your set cost here" and if it is equal to or greater than 4.99 than the price you have set in the flat module in admin will be the $cost.

There are other thing you could do including not putting out a quote (this would require a little different coding).

The $order->info['subtotal'] could be replaced with $order->info['total'] or even your logic from the cart dealing with attributes.

The whole point here is why deal with two shipping modules when one could work.

 

All my comments are based upon what I understand of your needs.

If you make any changes be sure to:

 

BACK UP ANY FILES AND/OR DATABASES THAT CAN BE AFFECTED BEFORE YOU CHANGE ANYTHING

Link to comment
Share on other sites

Hi KDM,

 

Thanks for taking the time to help me figure this out. You're very kind!

The thing with these attributes is that it's a cinema coupon/ gift card starting at 5 dollars(optional with each product).

And I'd like to make absolutely sure the customer received it with their order.

So they need to sign for the package when they receive it, which results in higher shipping costs.

The cart content could look like this

Example:

Flowers - product id "1" - $35 + (attribute) $5

Balloons - product id "2" - $15 (no attribute /gift card)

Subtotal $55

 

So I need to check whether or not a coupon is in the cart, If so disable flat shipping no#1

and only offer the shipping method they need to sign for...

 

I hope you now have a better understanding of what I'm trying to accomplish...

Link to comment
Share on other sites

Oh! Also there is an attribute with a value of 0, otherwise customers would be unable to purchase a product without the cinema coupon... So this is why I need the total value of the attributes in the cart...

Link to comment
Share on other sites

There is a function in shopping_cart.php called "attributes_price" that returns the cost of the product attribute price one product at a time. Try this in the class constructor of the shipping mod:

 

REPLACE:

// class constructor
   function flat() {
     global $order;

     $this->code = 'flat';
     $this->title = MODULE_SHIPPING_FLAT_TEXT_TITLE;
     $this->description = MODULE_SHIPPING_FLAT_TEXT_DESCRIPTION;
     $this->sort_order = MODULE_SHIPPING_FLAT_SORT_ORDER;
     $this->icon = '';
     $this->tax_class = MODULE_SHIPPING_FLAT_TAX_CLASS;
     $this->enabled = ((MODULE_SHIPPING_FLAT_STATUS == 'True') ? true : false);

     if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_FLAT_ZONE > 0) ) {
       $check_flag = false;
       $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_FLAT_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
       while ($check = tep_db_fetch_array($check_query)) {
         if ($check['zone_id'] < 1) {
           $check_flag = true;
           break;
         } elseif ($check['zone_id'] == $order->delivery['zone_id']) {
           $check_flag = true;
           break;
         }
       }

       if ($check_flag == false) {
         $this->enabled = false;
       }
     }
   }

 

WITH THIS:

// class constructor
   function flat() {
     global $order, $cart;

     $this->code = 'flat';
     $this->title = MODULE_SHIPPING_FLAT_TEXT_TITLE;
     $this->description = MODULE_SHIPPING_FLAT_TEXT_DESCRIPTION;
     $this->sort_order = MODULE_SHIPPING_FLAT_SORT_ORDER;
     $this->icon = '';
     $this->tax_class = MODULE_SHIPPING_FLAT_TAX_CLASS;
     $this->enabled = ((MODULE_SHIPPING_FLAT_STATUS == 'True') ? true : false);

     if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_FLAT_ZONE > 0) ) {
       $check_flag = false;
       $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_FLAT_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
       while ($check = tep_db_fetch_array($check_query)) {
         if ($check['zone_id'] < 1) {
           $check_flag = true;
           break;
         } elseif ($check['zone_id'] == $order->delivery['zone_id']) {
           $check_flag = true;
           break;
         }
// BOF kdm attribute price test
       $attributes_price = 0;
       if($check_flag == true) {
          foreach($order->products as $k=>$v) {
             $a_price = $cart->attributes_price($v['id']);
             $attributes_price = $attributes_price + ($a_price * $v['qty']);
            }
          if($attributes_price => 4.99) $check_flag = false;
         }
// EOF kdm attribute price test
       }

       if ($check_flag == false) {
         $this->enabled = false;
       }
     }
   }

 

I don't have time to test this myself as I am upgrading my site from 2.2a to 2.3.1.

I notice that the function (attributes_price) is checking the cart content to confirm the product is in the cart. I think this is okay.

Let me know what happens.

Link to comment
Share on other sites

I can't thank u enough for helping me with this problem!

I've tried implementing the code but no change in the way the shipping methods are displayed.

I changed your hack a little bit cause I was getting an "unexpected t_arrow error" and the 2nd if statement looked a bit off to me so it looks like this

// BOF kdm attribute price test
       $attributes_price = 0;
       if($check_flag == true) {
          foreach($order->products as $k=>$v) {
             $a_price = $cart->attributes_price($v['id']);
             $attributes_price = $attributes_price + ($a_price * $v['qty']);
            }
          if($attributes_price > 4.99){ //<--CHANGED THIS
            $check_flag = false;
         }
        }

// EOF kdm attribute price test
       }


       if ($check_flag == false){
         $this->enabled = false;
       }
     }
   }

I don't quite get this piece:

 foreach($order->products as $k=>$v) {
             $a_price = $cart->attributes_price($v['id']);
             $attributes_price = $attributes_price + ($a_price * $v['qty']);
            }

What's happening there? :unsure:

Link to comment
Share on other sites

I don't quite get this piece:

 
          foreach($order->products as $k=>$v) {
             $a_price = $cart->attributes_price($v['id']);
             $attributes_price = $attributes_price + ($a_price * $v['qty']);
            }

What's happening there? :unsure:

 

As I stated the "$cart->attributes_price" function looks at the products in the cart one product at a time to set that product attribute price.

The $order->products is where the products listing is for the order, so we cycle through each product calling the function in shopping_cart.php. This gives us the attribute price for each product and just in case there is a multiple order for that product we must multiply the attribute price by the quantity.

You can check this out in both the shopping_cart.php and order.php where the function is utilized. In fact in both programs this function is used to add the attribute price to product price to arrive at the final price for product.

 

How you change the code for your purpose is up to you. In theory this should work.

Do you know how to do an "echo, print_r or var_dump" in your program to see what values are in your different variables?

If you are not familiar with this site you should become familiar with it, you can check to see what php keywords will do for you. Just enter the function (not Oscommerce functions) into the top right search box with something like examples("echo", "print_r", "foreach") and information for that function will come up.

 

PHP Manual

 

 

As far as this bit of code:

if($attributes_price > 4.99){ //<--CHANGED THIS
            $check_flag = false;
         }

 

It will work as the same as below because the $check_flag will not be set to "false" unless the if statement before it is true. Either way you write the code is correct. You only need these "{}" to surround multiple statements that need to be executed under that certain condition. Without these "{}" only the first statement after the if statement would be executed. Those statement after the first statement would execute regardless of the state of the if statement because they are not surrounded by the "{}".

 

if($attributes_price > 4.99) $check_flag = false;

 

Hopefully this helps a little more.

Good luck!

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...