0

In Commerce Shopping cart contents checkout pane, how do you swap the position of order components? We want the Sales Tax to appear -below- the Shipping.

Swap order footer components?

3
  • Please Don't Capitalize Every Letter, Ok? And don't put tags in title, that's what tag field is for.
    – Mołot
    Dec 3 '14 at 0:21
  • Are you referring to the title?
    – jchwebdev
    Dec 3 '14 at 0:22
  • What else could I refer to? Whilst there is no hard rule about capitalization (but look at most upvoted questions and their style) there is a rule on tags in titles.
    – Mołot
    Dec 3 '14 at 0:23
1

Do it in a custom module implementing hook_commerce_price_formatted_components_alter(). See commerce_price.api.php for parameter information.

2
  • Thanks. I have a sincere question---which may sound 'trollish' but hopefully not: Is there someone on 'the team' that does documentation? Frankly, it's kinda maddening that almost -no- Drupal code comes with even a single example of usage. That would help guys like me get up to speed a -whole- lot faster. If I was more advanced, I'd gladly volunteer.
    – jchwebdev
    Dec 3 '14 at 18:42
  • There is, but he is working with me on a client project at the moment. All of the hooks are documented in .api.php files, though, and can be searched on drupalcontrib.org. Dec 4 '14 at 22:49
0

I dunno if this is kosher, but I'm answering my own question because, although Ryan's answer is 'correct', I figured others who are as baffled as me might appreciate a bit more detail. The trick is to alter the weight of each component.

Also, if this can be optimised, I'm all ears. Maybe there's an 'array_swap' function in PHP of which I'm unaware?

Finally, it would be nice if each component could somehow be labelled into categories ('tax', 'discount', 'subtotal') in case there are multiple components to be swapped so I could address the elements semantically. Eg. some tax jurisdictions in America have -4- sales taxes.

function jch_utilities_commerce_price_formatted_components_alter(&$components, $price, $entity)
{
    // if there is a tax component there will be more than 3 components and it seems to appear as the 3rd element.

    // swapping the weight member of each element automatically causes the display to resort.
    $s = sizeof($components);
    if ( $s > 3 ) {
        $x = 0;
        $w2 = 0;
        $w3 = 0;    
        foreach($components as $c) {
            if($x == 1)
                $w2 = $c['weight'];
            if($x == 2)
                $w3 = $c['weight'];
        $x++;                           
        }
        $x = 0;
        foreach($components as $c) {
            if($x == 1)
                $c['weight'] = $w3;
            if($x == 2)
                $c['weight'] = $w2;

        $x++;                           
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.