OpenCart按成本对运输进行排序.json 是否正在更改我的数组的排序


OpenCart sort shipping by cost. Is json changing the sorting of my array?

我正在尝试在购物车的opencart"估计运费和税费"部分中将运费排序更改为ASC。

我使用"uasort"更改了 php 中数组的排序,一切看起来都不错,但是当 json 循环在 tpl 中打印出来时,它的顺序又错了。

/

目录/控制器/总计/运输.php

打印 php 数组时,它的顺序正确:

Array
(
    [free] => Array
    (
        [title] => Free Shipping
        [quote] => Array
            (
                [free] => Array
                    (
                        [code] => free.free
                        [title] => Free Shipping
                        [cost] => 0
                        [tax_class_id] => 0
                        [text] => $0.00
                    )
            )
        [sort_order] => 1
        [error] => 
    )
    [usps] => Array
    (
        [title] => United States Postal Service
        [quote] => Array
            (
                [00] => Array
                    (
                        [code] => usps.00
                        [title] => First-Class Mail Parcel
                        [cost] => 4.54
                        [tax_class_id] => 0
                        [text] => $4.54
                    )
                [4] => Array
                    (
                        [code] => usps.4
                        [title] => USPS Retail Ground
                        [cost] => 7.29
                        [tax_class_id] => 0
                        [text] => $7.29
                    )
                [1] => Array
                    (
                        [code] => usps.1
                        [title] => Priority Mail 2-Day
                        [cost] => 7.3
                        [tax_class_id] => 0
                        [text] => $7.30
                    )
            )
        [sort_order] => 2
        [error] => 
    )
)

以 json 格式正确打印出来

{"shipping_method":{"free":{"title":"Free Shipping","quote":{"free":{"code":"free.free","title":"Free Shipping","cost":0,"tax_class_id":0,"text":"$0.00"}},"sort_order":"3","error":false},"usps":{"title":"United States Postal Service","quote":{"00":{"code":"usps.00","title":"First-Class Mail Parcel","cost":4.54,"tax_class_id":"0","text":"$4.54"},"4":{"code":"usps.4","title":"USPS Retail Ground","cost":7.29,"tax_class_id":"0","text":"$7.29"},"1":{"code":"usps.1","title":"Priority Mail 2-Day","cost":7.3,"tax_class_id":"0","text":"$7.30"}},"sort_order":"2","error":false}}}

但是,最终结果以原始顺序显示它们

/

catalog/view/theme/default/template/total/shipping.tpl

Please select the preferred shipping method to use on this order.
Free Shipping
Free Shipping - $0.00
United States Postal Service
Priority Mail 2-Day - $7.30
USPS Retail Ground - $7.29
First-Class Mail Parcel - $4.54

也许我只是累了,但似乎我错过了一些明显的东西。我做错了什么。

原始文件可以在这里查看...https://github.com/opencart/opencart/blob/master/upload/catalog/controller/total/shipping.phphttps://github.com/opencart/opencart/blob/master/upload/catalog/view/theme/default/template/total/shipping.tpl

它不起作用的原因是因为它还将数组存储在会话中,并且表单字段必须与原始货件类型键匹配(即"usps.00"与 [00])。

<input type="radio" name="shipping_method" value="usps.00">

我只是猜测;我看起来不是很彻底。

因为我不知道在 json 中重新使用多维数组,所以我在"/catalog/controller/total/shipping.php"中复制了"$quote_data"数组,使用 usaort() 排序成本然后使用 array_values(),因此原始密钥存储在会话中,json 获取修改后的密钥。

如果你想在这里使用我的黑客方法,它是...

取代

<?php
// START EDIT - FUNCTION TO RESORT BY PRICE/COST
function sortbycost ($a, $b) {
    return $a['cost'] > $b['cost'];
}
//END EDIT
class ControllerTotalShipping extends Controller {

取代

                if ($quote) {
                    // START EDIT - FUNCTION TO SORT BY QUOTE (PRICE/COST)
                    uasort($quote['quote'], 'sortbycost');
                    // ADDED SECOND ARRAY (DUPLICATE ARRAY TO BE STORED IN SESSION)
                    $quote_data2[$result['code']] = array(
                        'title'      => $quote['title'],
                        'quote'      => $quote['quote'],
                        'sort_order' => $quote['sort_order'],
                        'error'      => $quote['error']
                    );
                    // REPLACE KEYS FOR JS LOOP
                    $quote['quote'] = array_values($quote['quote']);
                    // END EDIT
                    $quote_data[$result['code']] = array(
                        'title'      => $quote['title'],
                        'quote'      => $quote['quote'],
                        'sort_order' => $quote['sort_order'],
                        'error'      => $quote['error']
                    );
                }

取代

        //$this->session->data['shipping_methods'] = $quote_data; //  ORIGINAL
        $this->session->data['shipping_methods'] = $quote_data2; // EDIT - KEEP DUPLICATE ARRAY WITH THE ORIGINAL KEYS IN SESSION

取代

            //$json['shipping_method'] = $this->session->data['shipping_methods']; // ORIGINAL
            $json['shipping_method'] = $quote_data; // EDIT - USE THE NEW KEYS

模板文件的javascript中重新排序数据可能会更好,但就像我说的,我不知道该怎么做。