A nice feature, that will solve almost any shipping cost commission problem, will be to make all shipping cost to use a "calculation rule" component.
I have the following scenario for one of my shipping carriers:
The commissions for a shipping are:
- nr. of kg * 1.65
- "register cost" 1.65 / shippment
- insurance value = declared value (>= 20) * 1/1%
- COD (cash on delivery) value * 1.1% + 2.5 / shippment

I have another scenario at another shipping cariier:
- base rate 12
- nr. of kg * 1
- insurance value = COD * 1%

and so on...

With the current uc_quote module and also uc_weightquote I cannot calculate all those commissions. So I made a module that implements another shipping quote "Rules calculation quote" (attached to this posts).

Basically, my module passes the quote to a rules component (like the one with conditions that is already implemented by uc_quote) for future calculations and gets the returned value. I think this will be a nice feature if is implemented by the core module uc_quote. There is no change in functionality for existing modules that depends on uc_quote module.

I'll make the patch this days and post it here. This feature exist also in commerce_shipping module but there is implemented more "drupal native" because quote is an entity (for Ubercart I defined a wrapper for non-entity quote method).

Is there anyone interested in this functionality?

CAUTION: ATTACHED MODULE IS NOT READY FOR PRODUCTION!!!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DanZ’s picture

Interesting idea.

It's generally bad to post zipped modules (you can never modify them). Can you set this up as a sandbox project? See http://drupal.org/node/1011196.

SilviuChingaru’s picture

I know Dan but this is intended only to probe the concept and should not be used in production. I'll post a patch and if will get commited will be no need for sandbox project (will be useless). That's why I did it "wrong" way.... :-)

SilviuChingaru’s picture

Status: Active » Needs review
FileSize
7.71 KB

Initial patch. Is fully working.

It works like this:
After uc_quote gets quote from any quote module, it passes that data to rules and allow future calculations. This way we make sure that we don't break any functionality of existing quote modules.
After data is recieved, if rules changed the original data, quote is updated. If same values are returned from rules, quoate remain the same.
In other words rules calculations are optional for any user. If user don't use them, the original quote is received like before, if they use them, they can override from rules any data returned by quote module.

SilviuChingaru’s picture

Just to restart postponed test... I don't understand why was "Postponed"?!

mandreato’s picture

@fiftyz your idea is good !
I've applied patch #4 but receive the following error on cart/checkout:

Fatal error: Class 'UcQuoteRulesWrapper' not found in ...\sites\all\modules\rules\rules.module on line 406

SilviuChingaru’s picture

@mandreato Because uc_quote.info file is changed by the #4 patch in order to include custom non entity field wrapper for rules and those files are cached by Drupal you nead to "Clear cache" after applying the patch.

Steps are like this:
1. Apply the patch.
2. Go to /admin/config/development/performance and press "Clear Cache".
3. Then go to /admin/store/settings/quotes and select "calculation rules" for any quote you desire to change it's calculation via rules.

Let me know if it works like this (it should work I also tested the patch at checkout and at create / edit order admin page).

mandreato’s picture

I already did cache clear, but the problem persists. I reach it even browsing /cart page.
Maybe it depends on other modules I have ?

This is the list of other Ubercart modules I use:
uc_addresses
uc_bank_transfer
uc_coupon
uc_extra_fields_pane
uc_out_of_stock
uc_tracking_links

SilviuChingaru’s picture

Try disable and enable uc_quote module from admin > modules. Post back if it works after this operation pls!

mandreato’s picture

uc_quote (Shipping quotes module) was required by the following other modules:
Flat rate, UC Tracking Links, Weight quote, Shipping
So I disabled them first, then disabled uc_quote; finally re-enabled them all and flushed cache again.
The error is gone (thanks) !

mandreato’s picture

Hi fiftyz, I'm trying to use patch #4 but I'm uncertain of the steps to do.
Basically I've 2 shipping methods (normail mail and courier) both divided into ranges:
- normal mail 0..49g
- normal mail 50.99g
- normal mail 100..
- courier 0..29.99€
- courier 30..69.99€
... and so on...

I need to add to every shipping method an additional fee when payment method is COD or an additional fee if shipping destination is on a "black list".

So, I've added the shipping methods on admin/store/settings/quotes (each with its conditions).
Now I'm adding calculation sets for additional fee implementations /admin/store/settings/quotes/manage/calculate_quote_from_flatrate_13/edit/2:
- add an action element "Set a data value"
- selected data: "method:rate"
- PHP evaluation: "return $value +..." (additional fee)

But I cannot understand where to decide if this calculation has to be done (eg when payment method is COD).

Could you describe a detailed example on how to use the patch please ?
Thanks In Advance !

SilviuChingaru’s picture

In fact that part of rules are actions rules so to check for condition should be done in shipping conditions but you could also accomplish it there like this:
- Add action: Execute custom PHP code
- Enter the code:

if ($order->payment_method == 'cod') {
  // do the calculations here and then assign them to $method->rate
  // Something like:
  $method->rate += 10; // Method rate will have initial value + 10
}

That should be it...

mandreato’s picture

Thanks for clarification !

SilviuChingaru’s picture

You're welcome. Don't forget to post you feedback after you test this patch.

longwave’s picture

#1953752: Allow modules to alter shipping quotes proposes something similar, but using a hook instead so contrib modules can alter the shipping rates. It would be nice to combine both ideas and allow both modules and Rules to alter shipping quotes in a similar way.

mandreato’s picture

Status: Needs review » Needs work

Spent some time with patch #4.
Here is a simplified scenario:

  • "Calculate shipping cost" panel contains two shipping method. Let's call them A and B
  • "Payment method" panel contains two payment method: bank transfer and COD
  • COD payment method has a condition to show only when shipping A is selected
  • shipping method A has a calculations rule (provided by the patch) with PHP to increase the rate when payment method is COD as suggested in #11

Behaviours:

  1. shifting the payment radio options from method A to B, automatically hides the COD payment method. Setting it back to A, makes COD to compare again. This is good Ubercart core behaviour
  2. setting shipping A and choosing payment COD doesn't automatically changes che amount of shipping quote
  3. instead, button "click to calculate shipping" has to be pressed every time, in order to reflect changes. After pressing the button, the $method->rate changes correctly
  4. choose shipping A, payment COD, then press the button and have the shipping A rate update. Then switch to shipping B, COD disappears, but shipping A quote remains updated. Again you have to press the button to have the (unchecked) original A quote displayed
  5. similarly switching from COD to bank transfer doesn't automatically changes che amount of shipping quote
  6. other method's properties don't get updated via PHP as the rate does ($method->description, $method->title, etc.)

So, whilst this is a good start, I think that other work is needed to reach a complete solution. The biggest issue is the need of button press upon every options change.

mandreato’s picture

Further tests: I've enabled "Ubercart Ajax Administration" sub module then added the "Payment: Payment method" triggering form element to update panes "Calculate shipping cost" and "Payment method".
Now, when I switch from bank transfer payment method to COD, nothing occours the first time, but it changes on the next switch (without needing to press the button)...
It seems like the value set by the rule takes a delay.

longwave’s picture

mandreato’s picture

I drop a testcase here then follow up on #1453306: Selecting an address from the drop down at checkout doesn't trigger shipping costs or triggers incorrect shipping costs

  1. apply patch #4
  2. install and enable Bank Transfer (or try with another payment method)
  3. enable Ubercart Ajax Administration module
  4. on admin/store/settings/checkout/ajax
    • remove all triggering form element
    • add a new one: "Payment: Payment method" updates both "Payment method" and "Calculate shipping cost" panes
  5. on admin/store/settings/quotes/methods add two flat rate quotes:
    • A --> 10€ with no conditions
    • B --> 20€ with no conditions
  6. on admin/store/settings/payment enable just two payment method:
    • Bank Transfer
    • COD
      • add a condition "Order has a shipping quote from a particular method"
      • shipping method: A
  7. on admin/store/settings/quotes/methods add a calculations rule for shipping method A
    • action: "Execute custom PHP code"
    • value (without php tags):
      if ($order->payment_method == 'cod') {
        $method->rate += 5;
      }
      
      
  8. try to checkout a cart
    • in "Calculate shipping cost" panel switch from A to B and see that the "Payment method" pane changes according: when A then both "Bank transfer" and "Cash on delivery" are present, while when B then just "Bank transfer" remains
    • after choosing A shipping quote, try to set COD: it does not increase the shipping rate
    • then choose "Bank transfer" --> the A shipping quote changes from 10€ to 15€, which is wrong
  9. on admin/store/settings/checkout invert the weights of panes: put "Payment method" before "Calculate shipping cost"
  10. repeat the experiment of the checkout (step 8): now the COD payment method correctly increases the A shipping quote

So it seems a matter of panes weight.

SilviuChingaru’s picture

I'll try to test it with patch #13 from #1453306: Selecting an address from the drop down at checkout doesn't trigger shipping costs or triggers incorrect shipping costs.
Please, if you have time, test it with patch above also.

mandreato’s picture

I just started to replicate the #18 testcase.
As first step, I applied both patch #4 of this thread and patch #13 of #1453306: Selecting an address from the drop down at checkout doesn't trigger shipping costs or triggers incorrect shipping costs.
On step number 4 I navigate to admin/store/settings/checkout/ajax and get a list of errors:

Notice: Undefined property: stdClass::$weight_units in uc_order_condition_products_weight() (linea 597 di ...\sites\all\modules\ubercart\uc_order\uc_order.rules.inc).

Warning: array_pop() expects parameter 1 to be array, boolean given in uc_quote_assemble_quotes() (linea 59 di ...\sites\all\modules\ubercart\shipping\uc_quote\uc_quote.pages.inc).

Maybe i need to generate a fresh new clone of production and clean every shipping method before testing.
This will take a while... I hope to retry during the weekend.

SilviuChingaru’s picture

Status: Needs work » Needs review
FileSize
478 bytes
6.83 KB

Fixed and tested #18 scenario and is working fine with patch #13 from #1453306: Selecting an address from the drop down at checkout doesn't trigger shipping costs or triggers incorrect shipping costs and this attached patch.

There is still an issue:
If we calculate a new quote rate like the one in #18 based on condition. Reproduce like this:
- select A quote method
-- select COD payment method (the rate is increased)
-- select Bank Transfer (the rate is decreased to default value 10)
-- select COD payment method (the rate is increased)
- select B quote method (Bank Transfer is auto selected because COD is no longer available for this quote method but A rate is not reset to 10 default value but it remains to 15 - this is not a problem at this point because we could not care about an unselected method)
- select A quote method (the values is still 15 but selected payment method is Bank Transfer which remained selected from B quote and that is NOT TRUE).

We change the status to needs review only to force QA to test this patch, will move back to needs work after the test until this issue is fixed.

Status: Needs review » Needs work

The last submitted patch, uc_quote-rules_calculation-1902622-2-3.diff, failed testing.

SilviuChingaru’s picture

Status: Needs work » Needs review

#21 issue seems not to be a bug because in my test environment I forgot to set a checkout ajax action on Quotes to Calculate shipping cost. If I do so the scenario now is like this:
- select A quote method
-- select COD payment method (the rate is increased)
-- select Bank Transfer (the rate is decreased to default value 10)
-- select COD payment method (the rate is increased)
- select B quote method (Bank Transfer is auto selected because COD is no longer available for this quote method but A rate is not reset to 10 default value but it remains to 15 - this is not a problem at this point because we should not care about an unselected method)
- select A quote method (the value is now recalculated to 10 with selected payment method Bank Transfer which remained selected from B quote and that is TRUE).

So, the patch from #21 is fully working and needs a project maintainer review.

mandreato’s picture

I've tested patch #21 after applying #13 of #1453306: Selecting an address from the drop down at checkout doesn't trigger shipping costs or triggers incorrect shipping costs.

I also had to add one triggering form element in /admin/store/settings/checkout/ajax in addition of what I described in #18: "Quotes: panes][quotes][quotes][quote_option" which updates "Calculate shipping cost" pane.
(I don't know why under the Quotes group I have two elements: "Click to calculate shipping" and panes][quotes][quotes][quote_option" which is a strange name).

Now the behaviour is exactly what fiftyz described in #23. This is fine !

The only thing which prevents me to set status to "reviewed and tested" is that navigation to /admin/store/settings/checkout/ajax raise the following:

Notice: Undefined property: stdClass::$weight_units in uc_quote_assemble_quotes() (line 53 of ...\sites\all\modules\ubercart\shipping\uc_quote\uc_quote.pages.inc).

longwave’s picture

"panes][quotes][quotes][quote_option" is the name given for the set of shipping quote radio buttons, as these don't have a single label describing them. We may be able to improve this by adding a #title and #title_display => 'invisible', this would probably be better for accessibility as well.

longwave’s picture

Issue summary: View changes

1

SilviuChingaru’s picture