Problems, need help? Have a tip or advice? Post it here.
8 posts Page 1 of 1
Hello.

I am having an issue with shipping scale and variants for a single product.

I have 1 product setup with:

Variants: Type[Clear|Pattern]
Requires Shipping: Yes
Shipping sliding scale: [ 0=2 | 1=4 | 3=6 | 6=9 | 12=12 ]


First I add 2x Clear to my cart and the shipping shows as $4, correct.

Then I leave the 2x Clear in my cart, go back to the product and add 2x Pattern to my cart.

Here is where my issue lies, the shipping shows to be $8 ($4 per 2xVariant) when I was expecting the shipping to be $6 since there are a total of 4 of these products in my cart just different variants.

Is this working as expected or is something amiss?

Thank you.
Is this working as expected ..?

Doesn't look like it is.
A scale of [ 0=2 | 1=4 | 3=6 | 6=9 | 12=12 ] would translate to the following charges -
Code: Select all
1 item = $2
2 to 3 items = $4
4 to 6 items = $6
7 to 12 items = $9
13 and more = $12

As can be seen, a charge of $8 does not figure anywhere in the list above so that amount cannot be calculated by this scale alone. Perhaps you have happened to define some other shipping charges as well in the cart config that are adding up? Please check.
Hi KK thanks for the reply.

I could not find anything and those values are what I am expecting but not getting.

I went ahead and created a fresh install with the couchified version that I downloaded from the shopping cart doc to test further and I received the same results.

In this test site I am getting the same results as my development site. The shipping value of 7 of the same product (two different variants and cart line items is totaling $8 in shipping when the expected outcome should be $9. This incorrect total of $8 makes some sense because 6 items is $6 and 1 item is $2 which is $8 total.

cart_test_screenshot_1.jpg
6 clear 1 pattern screenshot
cart_test_screenshot_1.jpg (172.72 KiB) Viewed 2773 times


In a second test I added 6 of the clear and 4 of the pattern (10 items in total), the shipping total should be $9 also but the shipping total I am getting is $12. This also make sense because to ship 6 of the clear is $6 and 4 of the pattern is $6 which in total is $12.

cart_test_screenshot_2.jpg
6 clear 4 pattern screenshot
cart_test_screenshot_2.jpg (177.53 KiB) Viewed 2773 times


With these tests I can only think that the cart is processing each product variant which it's own unique shipping scale instead of counting all of the same products in the cart in the same shipping scale.

Maybe you can confirm from the ZIP and possibly determine / fix the issue?

Please find the attached screenshots, I was unable to attach the ZIP file of the Couch test site as the limit is 2MB and the ZIP was 3MB. I am attaching the install-ex.php of the test site here and also uploaded the full test site on GoFile to download here:

https://gofile.io/d/a6jwb3

Ty KK and anyone else who takes the time to have a look.

Attachments

Ok, I see the problem now.

Couch, as its default, indeed treats each individual line in the cart as a separate product - so, the calculations it is doing would be the expected result.

As to whether or not each 'variant' should be treated as a separate line (product) is a moot question.
Industry standards (e.g. Magento) do treat each variant as a separate SKU - which, if we think about it, actually makes sense; variants need to tracked separately in inventory (and also might have distinct shipping requirements). So Couch is only following the accepted procedure.

That said, since for your use-case, you'd like to treat all variants as a single SKU, it would need a bit of custom programming in cart_ex.php (function get_shipping()) to enforce this requirement.
Ok TY KK,

I always appreciate your time, I will try to see what I can come up with.

I am thinking of trying to, while in the foreach loop add each cart item id, quantity and its sliding scale to an array, checking if that item ID exists in the array prior to adding and if it does to just add the quantity of the item to the array total for that item id and keep repeating. Then after the foreach loop is completed, calculate the shipping for each item from the array using the new item totals/shipping scale. If that makes sense?

I think in theory that should work, I just need to get the code figured out.
Here is what I have come up which seems to be working so far.

If something changes I will update the code, any input is appreciated if this could be improved.

Code: Select all
$unique_items = array();
foreach( $this->items as $key=>$item ){
    $scale_line_quantity = trim( $item['shipping_scale'] );
    if( $item['requires_shipping'] && strlen($scale_line_quantity) ){
        if( array_key_exists( $item['id'], $unique_items ) ) {
            $unique_quantity = $unique_items[$item['id']][0]['quantity'];
            $new_quantity = ($unique_quantity + $item['quantity']);
            $unique_items[$item['id']][0]['quantity'] = $new_quantity;
        } else {
            if( !array_key_exists( $item['id'], $unique_items ) ) {
                $data =  array (
                    'quantity' => $item['quantity'],
                    'shipping_scale' => $scale_line_quantity
                );
                $unique_items[$item['id']][] = $data;
            }
        }
    }
}

foreach( $unique_items as $prod_id => $itm ) {
    foreach( $itm as $k => $v ) {
        $cost += $this->_calc_charge( 1 /*count*/, $v['quantity'], $v['shipping_scale'] );
    }
}
Does exactly what is needed :)
Thanks for sharing.
KK wrote: Does exactly what is needed :)
Thanks for sharing.


Thanks for this amazing CMS and everything you do for it's community.
8 posts Page 1 of 1