Jump to content

Shopping Cart postage charges


grucker

Recommended Posts

I have attached theshopping cart class file. How do I charge postage based on the number of items purchased rather than the price and how do I add more items from the order page?

I have had a bash and got some strange results.

Regards

David

 

public function GetShippingCost()

{

$total = 0;

foreach ($this->items as $product_id => $quantity )

$total = $total + $this->GetItemShippingCost($product_id);

 

return $total;

}

public function GetItemShippingCost($product_id)

{

return $this->items[$product_id] * $this->getShippingCostFor(get_item_cost($product_id));

}

public function GetSubTotal()

{

$total = 0;

 

foreach ($this->items as $product_id => $quantity)

$total = $total + $this->GetItemCost($product_id);

 

return $total;

 

}

public function GetTotal()

{

// add tax here..

return $this->GetSubTotal() + $this->GetShippingCost();

}

private function getShippingCostFor($total)

{

if ($total < 20)

return 2.50;

 

elseif ($total < 30)

return 3.5;

 

 

else

return 4.50;

}

Link to comment
Share on other sites

As well as the previous file this function is in the template file. I only want postage added as follows

Orders under ?50.00 ?3.50 postage

Orders ?50.00 and over Free postage and packaging.

 

function render_shopping_cart()

{

$shopping_cart = get_shopping_cart();

 

 

$output = "

Product ID

Quantity

Amount

";

$line_item_counter = 1;

foreach ($shopping_cart->GetItems() as $product_id)

{

$output .= render_shopping_cart_row($shopping_cart , $product_id, $line_item_counter);

$line_item_counter++;

}

 

 

$output .= render_shopping_cart_shipping_row($shopping_cart);

 

$output .= render_shopping_cart_total_row($shopping_cart);

 

 

$output .="

";

 

return $output;

}

function render_shopping_cart_total_row(ShoppingCart $shopping_cart)

{

return "

Total:

£".$shopping_cart->GetTotal()."

";

 

}

function render_shopping_cart_shipping_row(ShoppingCart $shopping_cart)

{

return "

Postage and Packing

£".$shopping_cart->GetShippingCost()."

";

 

}

function render_shopping_cart_row(ShoppingCart $shopping_cart , $product_id, $line_item_counter)

{

$quantity = $shopping_cart->GetItemQuantity($product_id);

$amount = $shopping_cart->GetItemCost($product_id);

$unit_cost = get_item_cost($product_id);

$shipping_amount = $shopping_cart->GetItemShippingCost($product_id);

 

return "

$product_id

$quantity

£$amount

";

}

Link to comment
Share on other sites

I'm a bit confused... In your first post, you specify that you want to charge shipping by the number of items in the cart. In your second post, you say you want to charge shipping based on total price.

 

Assuming you want to charge based on total price, you should be able to do something like this... (not tested). This replaces the block of code from your first post in this topic.

 

   public function GetShippingCost($total)
   {
       if ($total         {
           return 3.5;
       }
       else
       {
           return 0;
       }
   }
   public function GetSubTotal()
   {
       $total = 0;

       foreach ($this->items as $product_id => $quantity)
         $total = $total + $this->GetItemCost($product_id);   

       return $total;

   }
   public function GetTotal()
   {
       // add tax here..
       return $this->GetSubTotal() + $this->GetShippingCost($this->GetSubTotal());
   }

Link to comment
Share on other sites

You'll probably need to update this function as well to remove the shipping amount per product:

 

function render_shopping_cart_row(ShoppingCart $shopping_cart , $product_id, $line_item_counter)
{
   $quantity = $shopping_cart->GetItemQuantity($product_id);
   $amount = $shopping_cart->GetItemCost($product_id);
   $unit_cost = get_item_cost($product_id);

   return "


                    $product_id



                    $quantity



                  £$amount



    ";   
}

 

With shipping per item removed, you may potentially run into an error or two in case the page that captures all the hidden fields requires that shipping field. I'd suggest putting these changes in place and then reporting back if you have any errors you can't figure out on your own.

Link to comment
Share on other sites

Close, It leaves the postage already entered even though the amount is over 50. If that cannot be solved they will have to live with it. The warnings refer to

things in both files and I have tried some arbitrary deletions without success.

Thanks for your attention. I find I am learning just cant always read what I am seeing.

Thanks

David

 

Warning: Missing argument 1 for ShoppingCart::GetShippingCost(), called in E:\domains\s\sites4eyes.co.uk\user\htdocs\Paypalwebstore\htdocs\mulberry\functions\templates.php on line 145 and defined in E:\domains\s\sites4eyes.co.uk\user\htdocs\Paypalwebstore\htdocs\mulberry\classes\ShoppingCart.php on line 37

 

Notice: Undefined variable: total in E:\domains\s\sites4eyes.co.uk\user\htdocs\Paypalwebstore\htdocs\mulberry\classes\ShoppingCart.php on line 39

Product ID Quantity Amount

Chocolate_Body_Butter_60 2 ?25.9

Rejuvenating_Serum 2 ?45

Sandalwood_Lemon_Shower_Gel 1 ?6.99

Postage and Packing ?3.5

Total: ?77.89

Link to comment
Share on other sites

Close, It leaves the postage already entered even though the amount is over 50.

I'm not completely sure what you mean here...

 

The two errors above occur because:

 

-- there is no longer a getShippingCost($product_id) function, so you'll need to find that line and remove it, or, if it is important, change it (I really can't tell you how though)

 

-- you are trying to access a $total variable that apparently doesn't exist in ShoppingCart.php. You'll need to find the appropriate line, figure out what it is intended to do, and either remove the line or change it as necessary.

 

I would avoid arbitrarily deleting lines -- if you delete the wrong lines, you might delete something important. Even if you randomly find the right line and delete it, it may not help you, since you won't understand why deleting that line was important and/or why it was causing an error in the first place.

Link to comment
Share on other sites

I carefully studied your comments and couldnt get any where but I was looking at the

public function GetShippingCost and I altered it to just echo 5.

This worked so I messed about and bingo it works perfectly. below is the original code and below that the new code. nothing else to delete.

 

public function GetShippingCost()

{

$total = 0;

foreach ($this->items as $product_id => $quantity )

$total = $total + $this->GetItemShippingCost($product_id);

return $total;

}

 

public function GetShippingCost()

{

$total = 0;

 

foreach ($this->items as $product_id => $quantity)

$total = $total + $this->GetItemCost($product_id);

 

if ($total < 50)

return 3.50;

if ($total > 50)

return 'Postage Free';

}

Link to comment
Share on other sites

Your way works, so congrats on finding a working solution. I glanced over your code, and it looks like the difference between your and my code was that you missed including $total as an argument in the GetShippingCost function. Rather than find the total within the function, I provided the function with the total amount. Just for future reference...

 

My code:

 

public function GetShippingCost($total)     {
       if ($total         {
           return 3.5;
       }
       else
       {
           return 0;
       }
   }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...