Page 1 of 1

Digital Downloads Being Charged Shipping...Sometimes

Posted: Wed Aug 15, 2012 5:18 am
by upl8t
I have both digital and shipped products on a recent opencart based site.

Shipped products are correctly charging shipping.

Digital download products, when they are the only thing in the cart, are correctly not charging shipping.

When a cart has BOTH shipped products and digital downloads in the same order the cart is incorrectly adding shipping to all products including the digital download products.

Does anyone know what's causing this and how to solve it?

Re: Digital Downloads Being Charged Shipping...Sometimes

Posted: Wed Aug 15, 2012 9:19 pm
by Johnathan
Sounds like your shipping method is not calculating correctly. What shipping method are you using? It has to be smart enough to take into account only products that require shipping.

Re: Digital Downloads Being Charged Shipping...Sometimes

Posted: Wed Aug 15, 2012 9:26 pm
by upl8t
Hi Johnathan... It's the standard Per Item shipping module.

Re: Digital Downloads Being Charged Shipping...Sometimes

Posted: Fri Aug 17, 2012 12:45 am
by upl8t
I've done some searching and it appears the shipping total is calculated using the total product count times the per unit shipping amount.

Can anyone comfirm that shipping is calculated from $this->cart->countProducts()

If this is the case it means the core cart module is missing the method to count the number of shipping products only... and accordingly is then incorrectly charging shipping on digital products when they're in an order that contains both digital and shipped products.

Any idea how to get the count of non-digital products only?

Re: Digital Downloads Being Charged Shipping...Sometimes

Posted: Fri Aug 17, 2012 2:30 am
by OpenCart Addons
The problem is the countProducts() call doesn't check if the product requires shipping or not.

countProducts() function

Code: Select all

public function countProducts() {
		$product_total = 0;
			
		$products = $this->getProducts();
			
		foreach ($products as $product) {
			$product_total += $product['quantity'];
		}		
					
		return $product_total;
	}
If you wanted to exclude downloads, in the file catalog/model/shipping/item.php

Add

Code: Select all

$shipping_quantity = 0;
foreach ($this->cart->getProducts() as $product) {
if ($product['shipping']) {
$shipping_quantity += $product['quantity'];
}
}
After

Code: Select all

if ($status) {
Then replace

Code: Select all

'cost'         => $this->config->get('item_cost') * $this->cart->countProducts(),
With

Code: Select all

'cost'         => $this->config->get('item_cost') * $shipping_quantity,

Re: Digital Downloads Being Charged Shipping...Sometimes

Posted: Fri Aug 17, 2012 6:07 am
by upl8t
Thanks for a such a detailed writeup! I'm going to put this in tonight. I'll let you know how it works out.

Re: Digital Downloads Being Charged Shipping...Sometimes

Posted: Fri Aug 17, 2012 10:44 pm
by upl8t
Thanks OpenCart Addons

Works perfectly. The only thing that's now wrong is the value on the estimate shipping popup. I need to find where that is and make the same change so it's calculating from $shipping_quantity

Re: Digital Downloads Being Charged Shipping...Sometimes

Posted: Sat Aug 18, 2012 12:55 am
by Johnathan
This really should be fixed in the shipping file, and not the system file, since the system's countProducts() function could be used elsewhere (and would result in a wrong count for non-shipping code). Try this instead:

Code: Select all

IN:
/catalog/model/shipping/item.php

REPLACE:
$quote_data['item'] = array(
    'code'         => 'item.item',
    'title'        => $this->language->get('text_description'),
    'cost'         => $this->config->get('item_cost') * $this->cart->countProducts(),

WITH:
$number_of_items = 0;
foreach ($this->cart->getProducts() as $product) {
    if ($product['shipping']) $number_of_items += $product['quantity'];
}
$quote_data['item'] = array(
    'code'         => 'item.item',
    'title'        => $this->language->get('text_description'),
    'cost'         => $this->config->get('item_cost') * $number_of_items,
Or just use my Item-Based Shipping extension, which counts them correctly. ;D

Re: Digital Downloads Being Charged Shipping...Sometimes

Posted: Sat Aug 18, 2012 2:57 am
by OpenCart Addons
The modification I suggested was mentioned to be applied to the actual shipping file and not the system file.

I would advise against changing the system files if it can be avoided as you will end up getting unwanted results elsewhere on your site.


Cheers,
Joel.

Re: Digital Downloads Being Charged Shipping...Sometimes

Posted: Sat Aug 18, 2012 5:40 am
by Johnathan
My mistake, I missed the second part of your post. Joel's second modification will work as well.