woocommerce-table-rate-shipping.php 163 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627
<?php
/*
  Plugin Name: WooCommerce Table Rate Shipping
  Plugin URI: http://bolderelements.net/plugins/table-rate-shipping-woocommerce/
  Description: WooCommerce custom plugin designed to calculate shipping costs and add one or more rates based on a table of rules
  Author: Bolder Elements
  Author URI: http://www.bolderelements.net/
  Version: 3.6.5

  Copyright: © 2012-2015 Bolder Elements (email : info@bolderelements.net)
  License: GNU General Public License v3.0
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */

add_action('plugins_loaded', 'woocommerce_table_rate_shipping_init', 0);

function woocommerce_table_rate_shipping_init() {
// Current version
    if (!defined('BE_WooTableShipping_VERSION'))
        define('BE_WooTableShipping_VERSION', '3.6.5');

    /**
     * Check if WooCommerce is active
     */
    if (class_exists('Woocommerce') || class_exists('WooCommerce')) {

        if (!class_exists('WC_Shipping_Method'))
            return;

        if (!class_exists('BE_Table_Rate_Shipping')) {

// setup internationalization support
            load_plugin_textdomain('be-table-ship', false, dirname(plugin_basename(__FILE__)) . '/languages');

            require('inc/woocommerce-shipping-zones.php');
            add_action('admin_init', array('BE_Table_Rate_Shipping', 'import_table_csv'), 10);

            class BE_Table_Rate_Shipping extends WC_Shipping_Method {

                public static $version = '3.6.5';

                /**
                 * __construct function.
                 *
                 * @access public
                 * @return void
                 */
                function __construct() {
                    $this->id = 'table_rate_shipping';
                    $this->method_title = __('Table Rate', 'be-table-ship');
                    $this->admin_page_heading = __('Table Rates', 'be-table-ship');
                    $this->admin_page_description = __('Table rate shipping allows you to set numerous rates based on location and specified conditions. Click the headlines below to expand or hide additional settings.', 'be-table-ship');
                    $this->table_rate_options = 'woocommerce_table_rates';
                    $this->class_priorities_options = 'woocommerce_class_priorities';
                    $this->handling_rates_options = 'woocommerce_handling_rates';
                    $this->title_order_options = 'woocommerce_trshipping_title_orders';

// Remove PHP Notices in WP_DEBUG mode
                    $this->handling = 0;
//$this->availability = $this->countries = '';

                    add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_admin_options'));
                    add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_table_rates'));

                    add_action('admin_enqueue_scripts', array(&$this, 'register_plugin_styles'));
                    add_filter('woocommerce_package_rates', array(&$this, 'wpml_translate_titles'), 10, 2);
                    add_filter('woocommerce_package_rates', array(&$this, 'hide_shipping_when_free_is_available'), 10, 2);
                    add_filter('woocommerce_shipping_chosen_method', array($this, 'select_default_rate'), 10, 2);

                    $this->init();
                }

                /**
                 * init function.
                 * initialize variables to be used
                 *
                 * @access public
                 * @return void
                 */
                function init() {
// Load the form fields.
                    $this->init_form_fields();

// Load the settings.
                    $this->init_settings();

// Define user set variables
                    $this->enabled = $this->get_option('enabled');
                    $this->title = $this->get_option('title');
                    $this->tax_status = $this->get_option('tax_status');
                    $this->condition = $this->get_option('condition');
                    $this->ship_free = $this->get_option('ship_free');
                    $this->ship_free_label = $this->get_option('ship_free_label');
                    $this->ship_free_option = $this->get_option('ship_free_option');
                    $this->includetax = $this->get_option('includetax');
                    $this->volumetric_enable = $this->get_option('volumetric_enable');
                    $this->volumetric_divisor = $this->get_option('volumetric_divisor');
                    $this->include_coupons = $this->get_option('include_coupons');
                    $this->highest_class = $this->get_option('highest_class');
                    $this->highest_costing_class = $this->get_option('highest_costing_class');
                    $this->no_fee_free_ship = $this->get_option('no_fee_free_ship');
                    $this->round_weight = $this->get_option('round_weight');
                    $this->hide_method = $this->get_option('hide_method');
                    $this->cost_min = $this->get_option('cost_min');
                    $this->cost_max = $this->get_option('cost_max');

// Load Cart rates
                    $this->get_table_rates();

// Load Shipping Classes
                    $this->get_class_priorities();

// Load Shipping Classes
                    $this->get_handling_rates();

// Load Shipping Classes
                    $this->get_title_order();

// Setup empty array for selecting defaults
                    $this->default_rates = array();

// Add filter to translate shipping classes id's on ajax update
                    $this->localize_table_rates_shipping_classes();
                }

                /**
                 * Filter Table Rates on Ajax request
                 *
                 * @access public
                 * @return void
                 */
                function localize_table_rates_shipping_classes() {
                    global $woocommerce;

                    if (is_ajax() && isset($_POST['action']) && $_POST['action'] == 'woocommerce_update_order_review') {
                        add_filter('option_woocommerce_table_rates', array($this, 'translate_shipping_classes_ids'));
                    }
                }

                /**
                 * Translate Shipping Classes id's
                 *
                 * @access public
                 * @return array
                 */
                function translate_shipping_classes_ids($rates) {
                    foreach ($rates as $key => $rate) {
                        $tr_class_id = $this->translate_shipping_class_id($rate['class']);
                        $rates[$key]['class'] = $tr_class_id;
                    }
                    return $rates;
                }

                /**
                 * Get the translated shipping class ID if WPML is active. Otherwise return the original ID
                 *
                 * @access public
                 * @return integer
                 */
                function translate_shipping_class_id($id) {

                    if (function_exists('icl_object_id'))
                        return icl_object_id($id, 'product_shipping_class', true);
                    else
                        return $id;
                }

                /**
                 * Initialise Gateway Settings Form Fields
                 *
                 * @access public
                 * @return void
                 */
                function init_form_fields() {
                    global $woocommerce;

                    $this->form_fields = array(
                        'general_settings_title' => array(
                            'title' => __('General Settings', 'be-table-ship'),
                            'type' => 'title',
                            'class' => 'title_drop title_h4 general_settings_title active',
                        ),
                        'enabled' => array(
                            'title' => __('Enable &#47; Disable', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('Enable this shipping method', 'be-table-ship'),
                            'default' => 'no',
                        ),
                        'title' => array(
                            'title' => __('Method Title', 'be-table-ship'),
                            'type' => 'text',
                            'description' => '',
                            'default' => __('Shipping', 'be-table-ship'),
                        ),
                        'tax_status' => array(
                            'title' => __('Tax Status', 'woocommerce'),
                            'type' => 'select',
                            'default' => 'taxable',
                            'options' => array(
                                'taxable' => __('Taxable', 'woocommerce'),
                                'none' => __('None', 'woocommerce'),
                            ),
                        ),
                        'condition' => array(
                            'title' => __('Condition', 'be-table-ship'),
                            'type' => 'select',
                            'default' => 'per-order',
                            'options' => array(
                                'per-order' => __('Per Order', 'be-table-ship'),
                                'per-item' => __('Per Item', 'be-table-ship'),
                                'per-class' => __('Per Class', 'be-table-ship'),
                            )
                        ),
                        'ship_free_title' => array(
                            'title' => __('Free Shipping Override', 'be-table-ship'),
                            'type' => 'title',
                            'class' => 'title_drop title_h4 ship_free_title',
                        ),
                        'ship_free' => array(
                            'title' => __('Free Shipping at', 'be-table-ship') . '(' . get_woocommerce_currency_symbol() . ')',
                            'type' => 'text',
                            'label' => __('Minimum cost of ALL cart items to be eligible for free shipping. Leave blank to disable free shipping option', 'be-table-ship'),
                            'default' => '',
                            'css' => 'width:100px;'
                        ),
                        'ship_free_label' => array(
                            'title' => __('Free Shipping Label', 'be-table-ship'),
                            'type' => 'text',
                            'description' => __('Label to appear next to Free Shipping Option in cart/checkout pages', 'be-table-ship'),
                            'default' => '',
                        ),
                        'ship_free_option' => array(
                            'title' => __('Add Free Shipping as Option', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('When checked, free shipping will be the only shipping cost, otherwise free shipping will be added as an option in addition to the table below', 'be-table-ship'),
                            'default' => 'no',
                        ),
                        'tax_options_title' => array(
                            'title' => __('Tax Options', 'be-table-ship'),
                            'type' => 'title',
                            'class' => 'title_drop title_h4 tax_options_title',
                        ),
                        'includetax' => array(
                            'title' => __('Include Tax', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('Calculate shipping based on prices AFTER tax', 'be-table-ship'),
                            'default' => 'no',
                        ),
                        'volumetric_shipping_title' => array(
                            'title' => __('Volumetric Shipping', 'be-table-ship'),
                            'type' => 'title',
                            'class' => 'title_drop title_h4 volumetric_shipping_title',
                        ),
                        'volumetric_enable' => array(
                            'title' => __('Enable Volumetric Comparison', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('When activated, plugin will determine if volumetric weight is heavier than weight given and charge shipping based on the higher amount', 'be-table-ship'),
                            'default' => 'no',
                        ),
                        'volumetric_divisor' => array(
                            'title' => __('Volumetric Divisor', 'be-table-ship'),
                            'type' => 'text',
                            'description' => __('This number can be found through your carrier\'s website', 'be-table-ship'),
                            'default' => '',
                        ),
                        'misc_settings_title' => array(
                            'title' => __('Miscellaneous Settings', 'be-table-ship'),
                            'type' => 'title',
                            'class' => 'title_drop title_h4 misc_settings_title',
                        ),
                        'include_coupons' => array(
                            'title' => __('Include Coupons', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('Subtotal is calculated based on cart value after coupons', 'be-table-ship'),
                            'default' => 'no',
                        ),
                        'highest_class' => array(
                            'title' => __('Single Class Only', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('When enabled, only items of the highest priority shipping class will be counted towards the shipping cost', 'be-table-ship') . ' <b>(' . __('Per Class Method Only', 'be-table-ship') . ')</b>',
                            'default' => 'no',
                        ),
                        'highest_costing_class' => array(
                            'title' => __('Highest Costing Class', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('When enabled, the highest shipping cost from the per class calculations will be charged', 'be-table-ship') . ' <b>(' . __('Per Class Method Only', 'be-table-ship') . ')</b>',
                            'default' => 'no',
                        ),
                        'no_fee_free_ship' => array(
                            'title' => __('No Fees on Free Shipping', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('Do not add fees from Handling Fees table when shipping cost is Free', 'be-table-ship'),
                            'default' => 'no',
                        ),
                        'round_weight' => array(
                            'title' => __('Round Weight', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('Rounds weight value up to the next whole number', 'be-table-ship'),
                            'default' => 'no',
                        ),
                        'hide_method' => array(
                            'title' => __('Hide This Method', 'be-table-ship'),
                            'type' => 'checkbox',
                            'label' => __('Hide This Shipping Method When the Free Shipping Method is Available', 'be-table-ship'),
                            'default' => 'no',
                        ),
                        'cost_min' => array(
                            'title' => __('Minimum Shipping Cost', 'be-table-ship'),
                            'type' => 'text',
                            'description' => __('The minimum shipping price a customer pays no matter what the table returns', 'be-table-ship'),
                            'default' => '',
                        ),
                        'cost_max' => array(
                            'title' => __('Maximum Shipping Cost', 'be-table-ship'),
                            'type' => 'text',
                            'description' => __('The maximum shipping price a customer pays no matter what the table returns', 'be-table-ship'),
                            'default' => '',
                        ),
                        'table_settings_title' => array(
                            'title' => __('Shipping Cost Tables', 'be-table-ship'),
                            'type' => 'title',
                            'class' => 'title_drop active title_h4 table_settings_title',
                        ),
                    );
                }

                /**
                 * calculate_shipping function.
                 *
                 * @access public
                 * @param array $package (default: array())
                 * @return void
                 */
                function calculate_shipping($package = array()) {
                    global $woocommerce;

                    $this->rates = array();
                    $shippingClasses = array();
                    $itemsByClass = array();
                    $fee_handling = $this->handling;
                    $condition = $this->condition;
                    $shipForFreeOption = false;
                    $cart_subtotal = ($this->includetax == 'yes') ? 0 : $this->calculate_subtotal($package['contents']);
                    $cart_dimensions = 0;
                    $shipping_country = $package['destination']['country'];
                    $shipping_state = $package['destination']['state'];
                    $shipping_zipcode = $package['destination']['postcode'];
                    $excludedClasses = array();
                    $shipping_classes = $this->class_priorities;
                    $handling_rates = $this->handling_rates;
                    $wc_attributes = wc_get_attribute_taxonomies();

// get excluded shipping classes
                    foreach ($shipping_classes as $key => $value) {
                        if ($value['excluded'] == 'on')
                            $excludedClasses[$value['term_id']] = $value;
                    }

// count number of items in cart and accumulated weights
                    $cart_item_count = 0;
                    $cart_weight_total = 0;
                    if (sizeof($package['contents']) > 0) {
                        foreach ($package['contents'] as $item_id => $values) {
                            if ($values['data']->needs_shipping()) {
// get class information
                                $item_class_id = $values['data']->get_shipping_class_id();
                                $item_class_name = $values['data']->get_shipping_class();
                                if ($item_class_name == '')
                                    $item_class_name = '*';

                                if ($this->includetax == 'yes') {
                                    if ($values['data']->product_type == 'bundle' && $values['data']->is_priced_per_product())
                                        $cart_subtotal += $values['data']->get_min_bundle_price_incl_tax() * $values['quantity'];
                                    elseif ($values['data']->product_type == 'bundle')
                                        $cart_subtotal += $values['data']->get_price_including_tax($values['quantity']) * $values['quantity'];
                                    else
                                        $cart_subtotal += $values['data']->get_price_including_tax() * $values['quantity'];
                                }

                                $shippingClasses[] = $item_class_id;
                                if ($condition != 'per-order') {
                                    $cart_item_count += $values['quantity'];
                                    $cart_weight_total += $values['data']->get_weight();
                                } else {
                                    if (!array_key_exists($item_class_id, $excludedClasses)) {
                                        $cart_item_count += $values['quantity'];
                                        $p_length = ($values['data']->length) ? $values['data']->length : 1;
                                        $p_width = ($values['data']->width) ? $values['data']->width : 1;
                                        $p_height = ($values['data']->height) ? $values['data']->height : 1;
                                        $cart_dimensions += ($p_length * $p_width * $p_height) * $values['quantity'];
//calculate volumetric weight
                                        if ($this->volumetric_enable == 'yes') {
                                            $v_weight = ($p_length * $p_width * $p_height) / $this->volumetric_divisor;
                                            $cart_weight_total += ($v_weight > $values['data']->get_weight()) ? $v_weight * $values['quantity'] : $values['data']->get_weight() * $values['quantity'];
                                        } else
                                            $cart_weight_total += $values['data']->get_weight() * $values['quantity'];
                                    } elseif ($this->includetax == 'no') {
                                        $cart_subtotal -= ($values['data']->get_price() * $values['quantity']);
                                    }
                                }

// group items by class for per-class method
                                $new_array = array(
                                    'class_name' => $item_class_name,
                                    'product_data' => $values,
                                );
                                if (!array_key_exists($item_class_name, $itemsByClass))
                                    $itemsByClass[$item_class_name] = array('class_id' => $item_class_id, 'products' => array());
                                array_push($itemsByClass[$item_class_name]['products'], $values);
                            }
                        }
                    }
                    $shippingClasses = array_unique($shippingClasses);

// WPML translate shipping classes
                    if (function_exists('icl_object_id')) {
                        $default_language = wpml_get_default_language();
                        foreach ($shippingClasses as $key => $value)
                            $shippingClasses[$key] = icl_object_id($value, 'product_shipping_class', true, $default_language);
                    }

// Coupon Settings Adjustment
                    if ($this->include_coupons == 'yes') :

                        if ($this->includetax == 'yes')
                            $cart_subtotal -= WC()->cart->discount_cart + array_sum(WC()->cart->coupon_discount_tax_amounts);
                        else
                            $cart_subtotal -= WC()->cart->discount_cart;

                    endif;

// check for free shipping
                    if ($this->ship_free != '') {
                        if ($cart_subtotal >= wc_format_decimal(apply_filters('wcml_raw_price_amount', $this->ship_free))) {
                            $free_rate_label = ($this->ship_free_label == "") ? $this->title : $this->ship_free_label;
                            $free_rate = array('id' => sanitize_title($free_rate_label), 'label' => $free_rate_label, 'cost' => floatval(0));
                            $this->add_rate($free_rate);

                            if ($this->ship_free_option == 'yes')
                                return;
                        }
                    }

                    if ($condition == 'per-order') {
                        $cart_weight_total = apply_filters('be_table_shipping_per_order_weight', $cart_weight_total, $package, $this->volumetric_divisor);
                        if ($this->round_weight == 'yes')
                            $cart_weight_total = ceil($cart_weight_total);
                        $order_data = array(
                            "condition" => $condition,
                            "fee_handling" => $fee_handling,
                            "subtotal" => apply_filters('be_table_shipping_per_order_subtotal', $cart_subtotal, $package),
                            "totalweight" => $cart_weight_total,
                            "itemcount" => apply_filters('be_table_shipping_per_order_item_count', $cart_item_count, $package),
                            "dimensions" => $cart_dimensions,
                            "volumetric" => ($this->volumetric_enable == 'yes') ? ($cart_dimensions / $this->volumetric_divisor) : 0,
                            "shipping_country" => $shipping_country,
                            "shipping_state" => $shipping_state,
                            "shipping_zipcode" => $shipping_zipcode,
                            "shipping_classes" => $shippingClasses,
                            "excluded_classes" => $excludedClasses,
                        );
                        $rate = $this->calculate_shipping_perorder($order_data);
                    } elseif ($condition == 'per-item') {
                        $rate_temp = array();
// cycle through all cart items
                        if (sizeof($package['contents']) > 0) {
                            $p_dimensions = 0;
                            $denied_rates = array();

// get excluded shipping classes
                            $charge_shipping = FALSE;
                            foreach ($shippingClasses as $key => $value) {
                                if (!array_key_exists($value, $excludedClasses))
                                    $charge_shipping = TRUE;
                            }

                            if (!$charge_shipping) {
                                $newAr = array('id' => sanitize_title($this->title),
                                    'label' => $this->title,
                                    'cost' => '0',
                                    'default' => 'no',
                                    'shiptype' => get_woocommerce_currency_symbol(),
                                    'zone' => 0);
                                array_push($rate_temp, array($newAr));
                            }

                            foreach ($package['contents'] as $item_id => $values) {
                                if ($values['data']->needs_shipping() && !array_key_exists($values['data']->get_shipping_class_id(), $excludedClasses)) {
                                    $product_weight = $values['data']->get_weight();
//calculate volumetric weight
                                    $p_length = ($values['data']->length) ? $values['data']->length : 1;
                                    $p_width = ($values['data']->width) ? $values['data']->width : 1;
                                    $p_height = ($values['data']->height) ? $values['data']->height : 1;
                                    if ($this->volumetric_enable == 'yes') {
                                        $v_weight = ($p_length * $p_width * $p_height) / $this->volumetric_divisor;
                                        if ($v_weight > $values['data']->get_weight())
                                            $product_weight = $v_weight;
                                    }
                                    if ($this->round_weight == 'yes')
                                        $product_weight = ceil($product_weight);
                                    $item_cost = ($this->includetax == 'no') ? $values['data']->get_price() : $values['data']->get_price_including_tax();

                                    $p_length = ($values['data']->length) ? $values['data']->length : 1;
                                    $p_width = ($values['data']->width) ? $values['data']->width : 1;
                                    $p_height = ($values['data']->height) ? $values['data']->height : 1;
                                    $p_dimensions = ($p_length * $p_width * $p_height);

// Get attributes for this product
                                    $product_attributes = array();
                                    foreach ($wc_attributes as $wc_ak => $wc_av) {
                                        $product_attributes[$wc_av->attribute_name] = $values['data']->get_attribute($wc_av->attribute_name);
                                    }

                                    $order_data = array(
                                        "condition" => $condition,
                                        "fee_handling" => $fee_handling,
                                        "price" => $item_cost, //sale_price,
                                        "quantity" => $values['quantity'],
                                        "class" => $values['data']->get_shipping_class_id(),
                                        "subtotal" => $cart_subtotal,
                                        "totalweight" => $product_weight,
                                        "dimensions" => $p_dimensions,
                                        "volumetric" => ($this->volumetric_enable == 'yes') ? ($p_dimensions / $this->volumetric_divisor) : 0,
                                        "shipping_country" => $shipping_country,
                                        "shipping_state" => $shipping_state,
                                        "shipping_zipcode" => $shipping_zipcode,
                                        "attributes" => $product_attributes,
                                    );
                                    $returnedRate = $this->calculate_shipping_peritem($order_data, $denied_rates);
                                    $rate_temp[] = $returnedRate['rate'];
                                    $denied_rates = $returnedRate['denied_rates'];
                                }
                            }
                        }

                        $total_tmp = array();
                        if (isset($rate_temp) && count($rate_temp)) {
// ensure all products are accounted for
                            $shippingTitles = array();
                            foreach ($rate_temp as $rt) {
                                if (count($rt)) {
                                    foreach ($rt as $key => $rtd) {
                                        $shippingTitles[] = $key;
                                    }
                                }
                            }
                            foreach ($shippingTitles as $skey => $title) {
                                foreach ($rate_temp as $rkey => $rate) {
                                    if (!array_key_exists($title, $rate)) {
//if( isset( $rate_temp[ $rkey ][$title] ) ) unset($rate_temp[$rkey][ $title ]);
                                        $denied_rates[] = $title;
                                    }
                                }
                            }

// setup rates
                            foreach ($rate_temp as $item) {
                                foreach ($item as $key => $rate) {
// check if rate has been denied and should not be added
                                    if (!in_array($key, $denied_rates)) {
                                        if (!isset($total_tmp[$rate['id']]['default']) || $total_tmp[$rate['id']]['default'] == 0)
                                            $total_tmp[$rate['id']]['default'] = 'off';
                                        if (!isset($total_tmp[$rate['id']]['cost']) || $total_tmp[$rate['id']]['cost'] == '')
                                            $total_tmp[$rate['id']]['cost'] = 0;
                                        $total_tmp[$rate['id']]['id'] = sanitize_title($rate['id']);
                                        $total_tmp[$rate['id']]['label'] = $rate['label'];
                                        $total_tmp[$rate['id']]['cost'] += (array_key_exists('cost', $rate)) ? floatval($rate['cost']) : '';
                                        $total_tmp[$rate['id']]['shiptype'] = $rate['shiptype'];
                                        $total_tmp[$rate['id']]['zone'] = $rate['zone'];
                                        if ($rate['default'] === 'on')
                                            $total_tmp[$rate['id']]['default'] = 'on';
                                    }
                                }
                            }
                        }
                        $rate = $total_tmp;
                    } elseif ($condition == 'per-class') {
                        if ($this->round_weight == 'yes')
                            $cart_weight_total = ceil($cart_weight_total);
                        $order_data = array(
                            "items" => $itemsByClass,
                            "condition" => $condition,
                            "fee_handling" => $fee_handling,
                            "subtotal" => $cart_subtotal,
                            "totalweight" => $cart_weight_total,
                            "itemcount" => $cart_item_count,
                            "shipping_country" => $shipping_country,
                            "shipping_state" => $shipping_state,
                            "shipping_zipcode" => $shipping_zipcode,
                            "shipping_classes" => $shippingClasses,
                            "excluded_classes" => $excludedClasses,
                        );
                        $rate = $this->calculate_shipping_perclass($order_data);
                    }

                    /* add handling fee */
                    if (count($rate) > 0) {
                        $handlingFee = 0;
                        if (count($this->handling_rates) > 0) {
                            foreach ($this->handling_rates as $value) {
                                foreach ($rate as $rkey => $rvalue) {
                                    if ($value['zone'] == $rvalue['zone']) {
                                        $handlingFee = (float) $value['fee'];
                                        $handlingPer = (float) $value['percent'];
                                        if ($this->no_fee_free_ship != 'yes' || ( $this->no_fee_free_ship == 'yes' && $rvalue['cost'] > 0 )) {
                                            if (isset($handlingPer))
                                                $rate[$rkey]['cost'] += ( (float) $handlingPer / 100) * $cart_subtotal;
                                            if (isset($handlingFee))
                                                $rate[$rkey]['cost'] += (float) $handlingFee;
                                        }
                                    }
                                }
                            }
                        }

//sort array by user chosen order
                        $properOrderedArray = array();
                        if (count($this->title_order)) {
                            $title_order = array_map('sanitize_title', $this->title_order);

                            foreach ($title_order as $key => $title)
                                if (isset($rate[$title]))
                                    $properOrderedArray[$title] = $rate[$title];

                            $properOrderedArray = array_merge($properOrderedArray, $rate);
                        } else
                            $properOrderedArray = $rate;

// Get all chosen methods
                        $shipping_pkgs = $woocommerce->cart->get_shipping_packages();
                        $package_copy = $package;
                        unset($package_copy['rates']);

                        $chosen_methods = WC()->session->get('chosen_shipping_methods');
                        $method_counts = WC()->session->get('shipping_method_counts');
                        $_available_methods = $package['rates'] + $properOrderedArray;
                        $pkg_id = array_search($package_copy, $shipping_pkgs);

                        $rate = apply_filters('be_table_shipping_ordered_rates', $properOrderedArray);

                        foreach ($properOrderedArray as $value) {
                            if (is_array($value)) {

                                $value['label'] = (!isset($value['label']) || $value['label'] == "") ? $this->title : $value['label'];
                                $value['id'] = sanitize_title($value['id']);
                                $value['cost'] = (!empty($this->cost_max) && (double) $this->cost_max > 0 && $value['cost'] > $this->cost_max ) ? $this->cost_max : $value['cost'];
                                $value['cost'] = (!empty($this->cost_min) && (double) $this->cost_min > 0 && $value['cost'] < $this->cost_min ) ? $this->cost_min : $value['cost'];

// Register the rates
                                $this->add_rate($value);

// Set default rate if one is not already chosen
                                if (isset($value['default']) && $value['default'] === 'on') {

                                    $this->default_rates[$value['id']] = $value;
                                }
                            }
                        }
                    }
                }

                /**
                 * calculate_shipping_perorder function.
                 *
                 * @access public
                 * @param array $package (default: array())
                 * @return array
                 */
                function calculate_shipping_perorder($data = array()) {
                    global $woocommerce;

                    $shipping_options = $this->table_rates;
                    $rate = array();
                    $shipping_costs = array();
                    $charge_shipping = FALSE;
                    $denied_rates = array();

// get excluded shipping classes
                    foreach ($data['shipping_classes'] as $key => $value) {
                        if (!array_key_exists($value, $data['excluded_classes']))
                            $charge_shipping = TRUE;
                    }

                    if (!$charge_shipping)
                        array_push($shipping_costs, array($this->title, '0', get_woocommerce_currency_symbol(), 'no', sanitize_title($this->title), 0));

                    /**
                     * Author: Phuong An
                     */
                    $zones = get_option('be_woocommerce_shipping_zones');
                    foreach ($shipping_options as $key => $value) {
                        if (!be_in_zone($value['zone'], $data['shipping_country'], $data['shipping_state'], $data['shipping_zipcode'], $zones))
                            unset($shipping_options[$key]);
                    }

                    if (count($shipping_options) <= 0)
                        return $rate;

// cycle through all shipping options
                    foreach ($shipping_options as $shipping_id => $values) {
                        $shipping_total = 0;
                        if ($values['identifier'] == '')
                            $values['identifier'] = sanitize_title($values['title']);
                        if ($values['class'] == '*' || in_array($values['class'], $data['shipping_classes'])) {
                            switch ($values['cond']) {
                                case 'price':
                                    $min = apply_filters('wcml_raw_price_amount', $values['min']);
                                    $max = apply_filters('wcml_raw_price_amount', $values['max']);
                                    if (($min == '*' || $data['subtotal'] >= $min) && ($max == '*' || $data['subtotal'] <= $max)) {
                                        if ($values['bundle_qty'] > 1) {
                                            for ($i = 1; $i <= $data['itemcount']; $i++) {
                                                if ($i < $values['bundle_qty'])
                                                    $shipping_total += $values['cost'];
                                                else
                                                    $shipping_total += $values['bundle_cost'];
                                            }
                                        } else {
                                            if ($values['shiptype'] == '%') {
                                                $shipping_total = $data['subtotal'] * ($values['cost'] / 100);
                                            } elseif ($values['shiptype'] == 'x') {
                                                $shipping_total = $values['cost'] * $data['itemcount'];
                                            } elseif ($values['shiptype'] == 'w') {
                                                $shipping_total = $values['cost'] * $data['totalweight'];
                                            } elseif ($values['shiptype'] == 'v') {
                                                $shipping_total = $values['cost'] * $data['dimensions'];
                                            } else {
                                                $shipping_total = $values['cost'];
                                            }
                                        }
                                        if ($values['shiptype'] == 'D') {
                                            if (isset($shipping_costs[$values['identifier']]))
                                                unset($shipping_costs[$values['identifier']]);
                                            $denied_rates[] = $values['identifier'];
                                        } elseif (!in_array($values['identifier'], $denied_rates)) {
                                            $shipping_costs[sanitize_title($values['identifier'])] = array($values['title'], $shipping_total, $values['default'], $values['shiptype'], $values['identifier'], $values['zone']);
                                        }
                                    }
                                    break;
                                case 'weight':
                                    if (($values['min'] == '*' || $data['totalweight'] >= $values['min']) && ($values['max'] == '*' || $data['totalweight'] <= $values['max'])) {
                                        if ($values['bundle_qty'] > 1) {
                                            for ($i = 1; $i <= $data['itemcount']; $i++) {
                                                if ($i < $values['bundle_qty'])
                                                    $shipping_total += $values['cost'];
                                                else
                                                    $shipping_total += $values['bundle_cost'];
                                            }
                                        } else {
                                            if ($values['shiptype'] == '%') {
                                                $shipping_total = $data['subtotal'] * ($values['cost'] / 100);
                                            } elseif ($values['shiptype'] == 'x') {
                                                $shipping_total = $values['cost'] * $data['itemcount'];
                                            } elseif ($values['shiptype'] == 'w') {
                                                $shipping_total = $values['cost'] * $data['totalweight'];
                                            } elseif ($values['shiptype'] == 'v') {
                                                $shipping_total = $values['cost'] * $data['dimensions'];
                                            } else {
                                                $shipping_total = $values['cost'];
                                            }
                                        }
                                        if ($values['shiptype'] == 'D') {
                                            if (isset($shipping_costs[$values['identifier']]))
                                                unset($shipping_costs[$values['identifier']]);
                                            $denied_rates[] = $values['identifier'];
                                        } elseif (!in_array($values['identifier'], $denied_rates)) {
                                            $shipping_costs[sanitize_title($values['identifier'])] = array($values['title'], $shipping_total, $values['default'], $values['shiptype'], $values['identifier'], $values['zone']);
                                        }
                                    }
                                    break;
                                case 'item-count':
                                    if (($values['min'] == '*' || $data['itemcount'] >= $values['min']) && ($values['max'] == '*' || $data['itemcount'] <= $values['max'])) {
                                        if ($values['bundle_qty'] > 1) {
                                            for ($i = 1; $i <= $data['itemcount']; $i++) {
                                                if ($i < $values['bundle_qty'])
                                                    $shipping_total += $values['cost'];
                                                else
                                                    $shipping_total += $values['bundle_cost'];
                                            }
                                        } else {
                                            if ($values['shiptype'] == '%') {
                                                $shipping_total = $data['subtotal'] * ($values['cost'] / 100);
                                            } elseif ($values['shiptype'] == 'x') {
                                                $shipping_total = $values['cost'] * $data['itemcount'];
                                            } elseif ($values['shiptype'] == 'w') {
                                                $shipping_total = $values['cost'] * $data['totalweight'];
                                            } elseif ($values['shiptype'] == 'v') {
                                                $shipping_total = $values['cost'] * $data['dimensions'];
                                            } else {
                                                $shipping_total = $values['cost'];
                                            }
                                        }
                                        if ($values['shiptype'] == 'D') {
                                            if (isset($shipping_costs[$values['identifier']]))
                                                unset($shipping_costs[$values['identifier']]);
                                            $denied_rates[] = $values['identifier'];
                                        } elseif (!in_array($values['identifier'], $denied_rates)) {
                                            $shipping_costs[sanitize_title($values['identifier'])] = array($values['title'], $shipping_total, $values['default'], $values['shiptype'], $values['identifier'], $values['zone']);
                                        }
                                    }
                                    break;
                                case 'dimensions':
                                    if (($values['min'] == '*' || $data['dimensions'] >= $values['min']) && ($values['max'] == '*' || $data['dimensions'] <= $values['max'])) {
                                        if ($values['bundle_qty'] > 1) {
                                            for ($i = 1; $i <= $data['itemcount']; $i++) {
                                                if ($i < $values['bundle_qty'])
                                                    $shipping_total += $values['cost'];
                                                else
                                                    $shipping_total += $values['bundle_cost'];
                                            }
                                        } else {
                                            if ($values['shiptype'] == '%') {
                                                $shipping_total = $data['subtotal'] * ($values['cost'] / 100);
                                            } elseif ($values['shiptype'] == 'x') {
                                                $shipping_total = $values['cost'] * $data['itemcount'];
                                            } elseif ($values['shiptype'] == 'w') {
                                                $shipping_total = $values['cost'] * $data['totalweight'];
                                            } elseif ($values['shiptype'] == 'v') {
                                                $shipping_total = $values['cost'] * $data['dimensions'];
                                            } else {
                                                $shipping_total = $values['cost'];
                                            }
                                        }
                                        if ($values['shiptype'] == 'D') {
                                            if (isset($shipping_costs[$values['identifier']]))
                                                unset($shipping_costs[$values['identifier']]);
                                            $denied_rates[] = $values['identifier'];
                                        } elseif (!in_array($values['identifier'], $denied_rates)) {
                                            $shipping_costs[sanitize_title($values['identifier'])] = array($values['title'], $shipping_total, $values['default'], $values['shiptype'], $values['identifier'], $values['zone']);
                                        }
                                    }
                                    break;
                            }
                        }
                    }

                    /* create rate(s) */
                    foreach ($shipping_costs as $value) {
                        if (is_array($value) && isset($value[4])) {
                            $rate[sanitize_title($value[4])] = array(
                                'id' => sanitize_title($this->id . "_" . $value[4]),
                                'label' => $value[0],
                                'cost' => $value[1],
                                'default' => $value[2],
                                'shiptype' => $value[3],
                                'zone' => $value[5],
                            );
                        }
                    }

                    return $rate;
                }

                /**
                 * calculate_shipping_peritem function.
                 *
                 * @access public
                 * @param array $package (default: array())
                 * @return array
                 */
                function calculate_shipping_peritem($data = array(), $denied_rates = array()) {
                    $shipping_options = $this->table_rates;
                    $rate = array('rate' => array(), 'denied_rates' => $denied_rates);
                    $shipping_costs = array();

                    /**
                     * Start remove
                     */
//                    foreach ($shipping_options as $key => $value) {
//                        if ($value['zone'] != '*' && !be_in_zone($value['zone'], $data['shipping_country'], $data['shipping_state'], $data['shipping_zipcode'])) {
//                            unset($shipping_options[$key]);
//                        }
//                    }
//                    if (count($shipping_options) <= 0)
//                        return $rate;
                    /**
                     * End remove
                     */
                    /**
                     * 25-Oct-2016
                     * Author: Phuong An
                     * Optimize speed load ajax
                     */
                    /**
                     * Start
                     */
                    $temp_shipping_options = array();
                    $count_shipping_options = 0;
                    $zones = get_option('be_woocommerce_shipping_zones');
                    foreach ($shipping_options as $key => $value) {
                        if (be_in_zone($value['zone'], $data['shipping_country'], $data['shipping_state'], $data['shipping_zipcode'], $zones)) {
                            $temp_shipping_options[$key] = $value;
                            $count_shipping_options++;
                        }
                        if ($count_shipping_options > 6) {
                            break;
                        }
                    }

                    $shipping_options = $temp_shipping_options;
                    if (count($shipping_options) <= 0)
                        return $rate;
                    /**
                     * End
                     */
                    // cycle through all shipping options
                    foreach ($shipping_options as $shipping_id => $values) {
                        $shipping_total = 0;
                        if ($values['identifier'] == '')
                            $values['identifier'] = sanitize_title($values['title']);
                        if ($data['class'] == $values['class'] || $values['class'] == '*') {
                            switch ($values['cond']) {
                                case 'price':
                                    $min = apply_filters('wcml_raw_price_amount', $values['min']);
                                    $max = apply_filters('wcml_raw_price_amount', $values['max']);
                                    if (($min == '0' || $data['price'] >= $min) && ($max == '*' || $data['price'] <= $max)) {
                                        if ($values['bundle_qty'] > 1) {
                                            for ($i = 1; $i <= $data['quantity']; $i++) {
                                                if ($i < $values['bundle_qty'])
                                                    $shipping_total += $values['cost'];
                                                else
                                                    $shipping_total += $values['bundle_cost'];
                                            }
                                        } else {
                                            if ($values['shiptype'] == '%') {
                                                $shipping_total = ($data['quantity'] * $data['price']) * ($values['cost'] / 100);
                                            } elseif ($values['shiptype'] == 'w') {
                                                $shipping_total = $values['cost'] * $data['totalweight'] * $data['quantity'];
                                            } elseif ($values['shiptype'] == 'v') {
                                                $shipping_total = $values['cost'] * $data['dimensions'] * $data['quantity'];
                                            } else {
                                                $shipping_total = $values['cost'] * $data['quantity'];
                                            }
                                        }
                                        if ($values['shiptype'] == 'D') {
                                            $denied_rates[] = $values['identifier'];
                                        } elseif (!in_array($values['identifier'], $denied_rates)) {
                                            $shipping_costs[sanitize_title($values['identifier'])] = array($values['title'], $shipping_total, $values['default'], $values['shiptype'], $values['identifier'], $values['zone']);
                                        }
                                    }
                                    break;
                                case 'weight':
                                    if (($values['min'] == '0' || $data['totalweight'] >= $values['min']) && ($values['max'] == '*' || $data['totalweight'] <= $values['max'])) {
                                        if ($values['bundle_qty'] > 1) {
                                            for ($i = 1; $i <= $data['quantity']; $i++) {
                                                if ($i < $values['bundle_qty'])
                                                    $shipping_total += $values['cost'];
                                                else
                                                    $shipping_total += $values['bundle_cost'];
                                            }
                                        } else {
                                            if ($values['shiptype'] == '%') {
                                                $shipping_total = $data['price'] * ($values['cost'] / 100);
                                            } elseif ($values['shiptype'] == 'w') {
                                                $shipping_total = $values['cost'] * $data['totalweight'] * $data['quantity'];
                                            } elseif ($values['shiptype'] == 'v') {
                                                $shipping_total = $values['cost'] * $data['dimensions'] * $data['quantity'];
                                            } else {
                                                $shipping_total = $values['cost'] * $data['quantity'];
                                            }
                                        }
                                        if ($values['shiptype'] == 'D') {
                                            $denied_rates[] = $values['identifier'];
                                        } elseif (!in_array($values['identifier'], $denied_rates)) {
                                            $shipping_costs[sanitize_title($values['identifier'])] = array($values['title'], $shipping_total, $values['default'], $values['shiptype'], $values['identifier'], $values['zone']);
                                        }
                                    }
                                    break;
                                case 'item-count':
                                    if (($values['min'] == '0' || $data['quantity'] >= $values['min']) && ($values['max'] == '*' || $data['quantity'] <= $values['max'])) {
                                        if ($values['bundle_qty'] > 1) {
                                            for ($i = 1; $i <= $data['quantity']; $i++) {
                                                if ($i < $values['bundle_qty'])
                                                    $shipping_total += $values['cost'];
                                                else
                                                    $shipping_total += $values['bundle_cost'];
                                            }
                                        } else {
                                            if ($values['shiptype'] == '%') {
                                                $shipping_total = ($data['quantity'] * $data['price']) * ($values['cost'] / 100);
                                            } elseif ($values['shiptype'] == 'w') {
                                                $shipping_total = $values['cost'] * $data['totalweight'] * $data['quantity'];
                                            } elseif ($values['shiptype'] == 'v') {
                                                $shipping_total = $values['cost'] * $data['dimensions'] * $data['quantity'];
                                            } else {
                                                $shipping_total = $values['cost'] * $data['quantity'];
                                            }
                                        }
                                        if ($values['shiptype'] == 'D') {
                                            $denied_rates[] = $values['identifier'];
                                        } elseif (!in_array($values['identifier'], $denied_rates)) {
                                            $shipping_costs[sanitize_title($values['identifier'])] = array($values['title'], $shipping_total, $values['default'], $values['shiptype'], $values['identifier'], $values['zone']);
                                        }
                                    }
                                    break;
                                case 'dimensions':
                                    if (($values['min'] == '0' || $data['dimensions'] >= $values['min']) && ($values['max'] == '*' || $data['dimensions'] <= $values['max'])) {
                                        if ($values['bundle_qty'] > 1) {
                                            for ($i = 1; $i <= $data['quantity']; $i++) {
                                                if ($i < $values['bundle_qty'])
                                                    $shipping_total += $values['cost'];
                                                else
                                                    $shipping_total += $values['bundle_cost'];
                                            }
                                        } else {
                                            if ($values['shiptype'] == '%') {
                                                $shipping_total = ($data['quantity'] * $data['price']) * ($values['cost'] / 100);
                                            } elseif ($values['shiptype'] == 'w') {
                                                $shipping_total = $values['cost'] * $data['totalweight'] * $data['quantity'];
                                            } elseif ($values['shiptype'] == 'v') {
                                                $shipping_total = $values['cost'] * $data['dimensions'] * $data['quantity'];
                                            } else {
                                                $shipping_total = $values['cost'] * $data['quantity'];
                                            }
                                        }
                                        if ($values['shiptype'] == 'D') {
                                            $denied_rates[] = $values['identifier'];
                                        } elseif (!in_array($values['identifier'], $denied_rates)) {
                                            $shipping_costs[sanitize_title($values['identifier'])] = array($values['title'], $shipping_total, $values['default'], $values['shiptype'], $values['identifier'], $values['zone']);
                                        }
                                    }
                                    break;
                                default:
                                    foreach ($data['attributes'] as $ak => $attr) :
                                        if ($ak == $values['cond']) :
                                            if (($values['min'] == '0' || $attr >= $values['min']) && ($values['max'] == '*' || $attr <= $values['max'])) {
                                                if ($values['bundle_qty'] > 1) {
                                                    for ($i = 1; $i <= $data['quantity']; $i++) {
                                                        if ($i < $values['bundle_qty'])
                                                            $shipping_total += $values['cost'];
                                                        else
                                                            $shipping_total += $values['bundle_cost'];
                                                    }
                                                } else {
                                                    if ($values['shiptype'] == '%') {
                                                        $shipping_total = ($data['quantity'] * $data['price']) * ($values['cost'] / 100);
                                                    } elseif ($values['shiptype'] == 'w') {
                                                        $shipping_total = $values['cost'] * $data['totalweight'] * $data['quantity'];
                                                    } elseif ($values['shiptype'] == 'v') {
                                                        $shipping_total = $values['cost'] * $data['dimensions'] * $data['quantity'];
                                                    } else {
                                                        $shipping_total = $values['cost'] * $data['quantity'];
                                                    }
                                                }
                                                if ($values['shiptype'] == 'D') {
                                                    $denied_rates[] = $values['identifier'];
                                                } elseif (!in_array($values['identifier'], $denied_rates)) {
                                                    $shipping_costs[sanitize_title($values['identifier'])] = array($values['title'], $shipping_total, $values['default'], $values['shiptype'], $values['identifier'], $values['zone']);
                                                }
                                            }
                                        endif;
                                    endforeach;
                                    break;
                            }
                        }
                    }
                    /* create rate(s) */
                    $rate['denied_rates'] = $denied_rates;
                    foreach ($shipping_costs as $value) {
                        $rate['rate'][sanitize_title($value[4])] = array(
                            'id' => sanitize_title($this->id . "_" . $value[4]),
                            'label' => $value[0],
                            'cost' => $value[1],
                            'default' => $value[2],
                            'shiptype' => $value[3],
                            'zone' => $value[5],
                        );
                    }

                    return $rate;
                }

                /**
                 * calculate_shipping_perclass function.
                 *
                 * @access public
                 * @param array $package (default: array())
                 * @return array
                 */
                function calculate_shipping_perclass($data = array()) {
                    $shipping_options = $this->table_rates;
                    $rate = array();
                    $shipping_costs = array();
                    $denied_rates = array();
                    $charge_shipping = FALSE;

                    foreach ($shipping_options as $key => $value) {
                        if ($value['zone'] != '*' && !be_in_zone($value['zone'], $data['shipping_country'], $data['shipping_state'], $data['shipping_zipcode']))
                            unset($shipping_options[$key]);
                    }

                    if (count($shipping_options) <= 0)
                        return $rate;

// get excluded shipping classes
                    foreach ($data['shipping_classes'] as $key => $value) {
                        if (!array_key_exists($value, $data['excluded_classes']))
                            $charge_shipping = TRUE;
                    }

                    if (!$charge_shipping) {
                        $newAr[sanitize_title($this->title)] = array('title' => $this->title,
                            'cost' => '0',
                            'shiptype' => get_woocommerce_currency_symbol(),
                            'default' => 'no',
                            'zone' => 0);
                        array_push($shipping_costs, $newAr);
                    } else {
// cycle through each shipping class
                        foreach ($data['items'] as $class => $cval) {
// setup new array for price options
                            $shipping_costs[$class] = array();

// get total counts
                            $class_total_price = $class_total_count = $class_total_weight = $class_total_dimensions = 0;
                            if (sizeof($cval['products']) > 0) {
                                foreach ($cval['products'] as $values) {
                                    $class_total_price += ($this->includetax == 'yes') ? $values['data']->get_price_including_tax() * $values['quantity'] : $values['data']->price * $values['quantity'];
                                    $class_total_count += $values['quantity'];

                                    $product_weight = $values['data']->get_weight();
//calculate volumetric weight
                                    $p_length = ($values['data']->length) ? $values['data']->length : 1;
                                    $p_width = ($values['data']->width) ? $values['data']->width : 1;
                                    $p_height = ($values['data']->height) ? $values['data']->height : 1;
                                    if ($this->volumetric_enable == 'yes') {
                                        $v_weight = ($p_length * $p_width * $p_height) / $this->volumetric_divisor;
                                        if ($v_weight > $values['data']->get_weight())
                                            $product_weight = $v_weight;
                                    }
                                    $class_total_dimensions += ($p_length * $p_width * $p_height) * $values['quantity'];
                                    $class_total_weight += $product_weight * $values['quantity'];
                                }
                                $class_total_weight = apply_filters('be_table_shipping_per_order_weight', $class_total_weight, $cval['products'], $this->volumetric_divisor);
                                if ($this->round_weight == 'yes')
                                    $class_total_weight = ceil($class_total_weight);
                            }

// cycle through all shipping options
                            foreach ($shipping_options as $shipping_id => $values) {
                                $shipping_total = 0;
                                $rate_found = false;

                                if ($values['identifier'] == '')
                                    $values['identifier'] = sanitize_title($values['title']);
                                if ($values['class'] == '*' || $values['class'] == $cval['class_id']) {
                                    switch ($values['cond']) {
                                        case 'price':
                                            $min = apply_filters('wcml_raw_price_amount', $values['min']);
                                            $max = apply_filters('wcml_raw_price_amount', $values['max']);
                                            if (($min == '*' || $class_total_price >= $min) && ($max == '*' || $class_total_price <= $max)) {
                                                if ($values['bundle_qty'] > 1) {
                                                    for ($i = 1; $i <= $class_total_count; $i++) {
                                                        if ($i < $values['bundle_qty'])
                                                            $shipping_total += $values['cost'];
                                                        else
                                                            $shipping_total += $values['bundle_cost'];
                                                    }
                                                } else {
                                                    if ($values['shiptype'] == '%') {
                                                        $shipping_total = $class_total_price * ($values['cost'] / 100);
                                                    } elseif ($values['shiptype'] == 'x') {
                                                        $shipping_total = $values['cost'] * $class_total_count;
                                                    } elseif ($values['shiptype'] == 'w') {
                                                        $shipping_total = $values['cost'] * $class_total_weight;
                                                    } elseif ($values['shiptype'] == 'v') {
                                                        $shipping_total = $values['cost'] * $class_total_dimensions;
                                                    } else {
                                                        $shipping_total = $values['cost'];
                                                    }
                                                }
                                                if ($values['shiptype'] == 'D') {
                                                    $denied_rates[] = $values['identifier'];
                                                    foreach ($shipping_costs as $k => $c)
                                                        if (isset($c[$values['identifier']]))
                                                            unset($shipping_costs[$k][$values['identifier']]);
                                                } elseif (!in_array($values['identifier'], $denied_rates)) {
                                                    $rate_found = true;
                                                    $shipping_costs[$class][$values['identifier']]['title'] = $values['title'];
                                                    $shipping_costs[$class][$values['identifier']]['cost'] = $shipping_total;
                                                    $shipping_costs[$class][$values['identifier']]['default'] = $values['default'];
                                                    $shipping_costs[$class][$values['identifier']]['shiptype'] = $values['shiptype'];
                                                    $shipping_costs[$class][$values['identifier']]['zone'] = $values['zone'];
                                                }
                                            }
                                            break;
                                        case 'weight':
                                            if (($values['min'] == '*' || $class_total_weight >= $values['min']) && ($values['max'] == '*' || $class_total_weight <= $values['max'])) {
                                                if ($values['bundle_qty'] > 1) {
                                                    for ($i = 1; $i <= $class_total_count; $i++) {
                                                        if ($i < $values['bundle_qty'])
                                                            $shipping_total += $values['cost'];
                                                        else
                                                            $shipping_total += $values['bundle_cost'];
                                                    }
                                                } else {
                                                    if ($values['shiptype'] == '%') {
                                                        $shipping_total = $class_total_price * ($values['cost'] / 100);
                                                    } elseif ($values['shiptype'] == 'x') {
                                                        $shipping_total = $values['cost'] * $class_total_count;
                                                    } elseif ($values['shiptype'] == 'w') {
                                                        $shipping_total = $values['cost'] * $class_total_weight;
                                                    } elseif ($values['shiptype'] == 'v') {
                                                        $shipping_total = $values['cost'] * $class_total_dimensions;
                                                    } else {
                                                        $shipping_total = $values['cost'];
                                                    }
                                                }
                                                if ($values['shiptype'] == 'D') {
                                                    $denied_rates[] = $values['identifier'];
                                                    foreach ($shipping_costs as $k => $c)
                                                        if (isset($c[$values['identifier']]))
                                                            unset($shipping_costs[$k][$values['identifier']]);
                                                } elseif (!in_array($values['identifier'], $denied_rates)) {
                                                    $rate_found = true;
                                                    $shipping_costs[$class][$values['identifier']]['title'] = $values['title'];
                                                    $shipping_costs[$class][$values['identifier']]['cost'] = $shipping_total;
                                                    $shipping_costs[$class][$values['identifier']]['default'] = $values['default'];
                                                    $shipping_costs[$class][$values['identifier']]['shiptype'] = $values['shiptype'];
                                                    $shipping_costs[$class][$values['identifier']]['zone'] = $values['zone'];
                                                }
                                            }
                                            break;
                                        case 'item-count':
                                            if (($values['min'] == '*' || $class_total_count >= $values['min']) && ($values['max'] == '*' || $class_total_count <= $values['max'])) {
                                                if ($values['bundle_qty'] > 1) {
                                                    for ($i = 1; $i <= $class_total_count; $i++) {
                                                        if ($i < $values['bundle_qty'])
                                                            $shipping_total += $values['cost'];
                                                        else
                                                            $shipping_total += $values['bundle_cost'];
                                                    }
                                                } else {
                                                    if ($values['shiptype'] == '%') {
                                                        $shipping_total = $class_total_price * ($values['cost'] / 100);
                                                    } elseif ($values['shiptype'] == 'x') {
                                                        $shipping_total = $values['cost'] * $class_total_count;
                                                    } elseif ($values['shiptype'] == 'w') {
                                                        $shipping_total = $values['cost'] * $class_total_weight;
                                                    } elseif ($values['shiptype'] == 'v') {
                                                        $shipping_total = $values['cost'] * $class_total_dimensions;
                                                    } else {
                                                        $shipping_total = $values['cost'];
                                                    }
                                                }
                                                if ($values['shiptype'] == 'D') {
                                                    $denied_rates[] = $values['identifier'];
                                                    foreach ($shipping_costs as $k => $c)
                                                        if (isset($c[$values['identifier']]))
                                                            unset($shipping_costs[$k][$values['identifier']]);
                                                } elseif (!in_array($values['identifier'], $denied_rates)) {
                                                    $rate_found = true;
                                                    $shipping_costs[$class][$values['identifier']]['title'] = $values['title'];
                                                    $shipping_costs[$class][$values['identifier']]['cost'] = $shipping_total;
                                                    $shipping_costs[$class][$values['identifier']]['default'] = $values['default'];
                                                    $shipping_costs[$class][$values['identifier']]['shiptype'] = $values['shiptype'];
                                                    $shipping_costs[$class][$values['identifier']]['zone'] = $values['zone'];
                                                }
                                            }
                                            break;
                                        case 'dimensions':
                                            if (($values['min'] == '*' || $class_total_dimensions >= $values['min']) && ($values['max'] == '*' || $class_total_dimensions <= $values['max'])) {
                                                if ($values['bundle_qty'] > 1) {
                                                    for ($i = 1; $i <= $class_total_count; $i++) {
                                                        if ($i < $values['bundle_qty'])
                                                            $shipping_total += $values['cost'];
                                                        else
                                                            $shipping_total += $values['bundle_cost'];
                                                    }
                                                } else {
                                                    if ($values['shiptype'] == '%') {
                                                        $shipping_total = $class_total_price * ($values['cost'] / 100);
                                                    } elseif ($values['shiptype'] == 'x') {
                                                        $shipping_total = $values['cost'] * $class_total_count;
                                                    } elseif ($values['shiptype'] == 'w') {
                                                        $shipping_total = $values['cost'] * $class_total_weight;
                                                    } elseif ($values['shiptype'] == 'v') {
                                                        $shipping_total = $values['cost'] * $class_total_dimensions;
                                                    } else {
                                                        $shipping_total = $values['cost'];
                                                    }
                                                }
                                                if ($values['shiptype'] == 'D') {
                                                    $denied_rates[] = $values['identifier'];
                                                    foreach ($shipping_costs as $k => $c)
                                                        if (isset($c[$values['identifier']]))
                                                            unset($shipping_costs[$k][$values['identifier']]);
                                                } elseif (!in_array($values['identifier'], $denied_rates)) {
                                                    $rate_found = true;
                                                    $shipping_costs[$class][$values['identifier']]['title'] = $values['title'];
                                                    $shipping_costs[$class][$values['identifier']]['cost'] = $shipping_total;
                                                    $shipping_costs[$class][$values['identifier']]['default'] = $values['default'];
                                                    $shipping_costs[$class][$values['identifier']]['shiptype'] = $values['shiptype'];
                                                    $shipping_costs[$class][$values['identifier']]['zone'] = $values['zone'];
                                                }
                                            }
                                            break;
                                    }
                                }
                            }

                            if (!$rate_found && array_key_exists($cval['class_id'], $data['excluded_classes'])) {
                                $shipping_costs[$class][$values['identifier']]['title'] = $this->method_title;
                                $shipping_costs[$class][$values['identifier']]['cost'] = 0;
                                $shipping_costs[$class][$values['identifier']]['default'] = 'no';
                                $shipping_costs[$class][$values['identifier']]['zone'] = 0;
                            }
                        }
                    }

// ensure all products are accounted for
                    $shippingTitles = array();
                    $productClasses = array_keys($shipping_costs);
                    if (!empty($shipping_costs)) {
                        foreach ($shipping_costs as $cKey => $cVal) {
                            foreach ($cVal as $iKey => $iVal) {
                                if (!isset($shippingTitles))
                                    $shippingTitles[$iKey] = array();

                                $shippingTitles[$iKey][] = $cKey;
                            }
                        }
                    }

                    foreach ($shippingTitles as $key => $value) {
                        if (count(array_intersect($value, $productClasses)) != count($productClasses))
                            foreach ($value as $vKey => $classID)
                                if (isset($shipping_costs[$classID][$key]))
                                    unset($shipping_costs[$classID][$key]);
                    }

// adjust array if single class only is enabled
                    if ($this->highest_class == 'yes') {
                        $classesUsed = array_keys($shipping_costs);
                        $highestClass = $this->get_highest_priority_class($classesUsed);
                        $highestClassTerm = get_term_by('id', $highestClass, 'product_shipping_class', ARRAY_A);

                        if (isset($highestClassTerm['slug'])) {
                            $shipping_costs_new[$highestClassTerm['slug']] = $shipping_costs[$highestClassTerm['slug']];
                            $shipping_costs = $shipping_costs_new;
                        }
                    } elseif ($this->highest_costing_class == 'yes') {
                        $classesUsed = array_keys($shipping_costs);
                        $highestCosting = $this->get_highest_costing_class($shipping_costs, $classesUsed);
                        $shipping_costs = $highestCosting;
                    }

// structure shipping costs for final output
                    $new_rates_array = array();
                    if (isset($shipping_costs))
                        foreach ($shipping_costs as $class => $val) {
                            foreach ($val as $key => $val2) {
                                if (!isset($new_rates_array[$key]['cost']))
                                    $new_rates_array[$key]['cost'] = 0;
                                $new_rates_array[$key]['title'] = $val2['title'];
                                $new_rates_array[$key]['cost'] += (float) $val2['cost'];
                                $new_rates_array[$key]['default'] = $val2['default'];
                                $new_rates_array[$key]['zone'] = $val2['zone'];
                            }
                        }

                    /* create rate(s) */
                    foreach ($new_rates_array as $identifier => $value) {
                        $rate[sanitize_title($identifier)] = array(
                            'id' => sanitize_title($this->id . "_" . $identifier),
                            'label' => $value['title'],
                            'cost' => $value['cost'],
                            'default' => $value['default'],
                            'shiptype' => (isset($value['shiptype'])) ? $value['shiptype'] : "",
                            'zone' => $value['zone'],
                        );
                        if (isset($value['default']))
                            $rate[sanitize_title($identifier)]['default'] = $value['default'];
                    }

                    return $rate;
                }

                /**
                 * Admin Panel Options
                 * - Options for the cart based portion
                 *
                 * @since 1.0.0
                 * @access public
                 * @return void
                 */
                public function admin_options() {
                    global $woocommerce;

                    $cur_symbol = get_woocommerce_currency_symbol();
                    $condOpsG = $classOpsG = $zoneOpsJS = "";
                    $shippingClasses = $woocommerce->shipping->get_shipping_classes();
                    if (count($shippingClasses) > 0)
                        foreach ($shippingClasses as $key => $val)
                            $classOpsG .= "<option value=\"" . $val->term_id . "\">" . $val->name . "</option>";
                    $conds = array("price" => "Price", "weight" => "Weight", "item-count" => "Item Count", "dimensions" => "Dimensions");
                    $countries = $woocommerce->countries->get_allowed_countries();
                    $zones = be_get_zones();
                    if (count($zones))
                        foreach ($zones as $val)
                            $zoneOpsJS .= "<option value=\"" . $val['zone_id'] . "\">" . $val['zone_title'] . "</option>";
                    $attributes = wc_get_attribute_taxonomies();
                    if (!empty($attributes))
                        foreach ($attributes as $ak => $attr)
                            $conds[$attr->attribute_name] = ( isset($attr->attribute_label) ) ? $attr->attribute_label : $attr->attribute_name;
                    foreach ($conds as $key => $val)
                        $condOpsG .= "<option value=\"" . $key . "\">" . $val . "</option>";
                    ?>
                    <style>.check-column input{margin-left:8px;} .check-column {margin: 0;padding: 0;}</style>
                    <?php if (isset($_GET['betrswc-import-status']) && $_GET['betrswc-import-status'] == 'error') : ?>
                        <div class="error">
                            <p>
                                <?php
                                switch ($_GET['error_code']) {
                                    case 'nonce':
                                        _e('Import requests must be come the proper form within the admin dashboard', 'be-table-ship');
                                        break;
                                    case 'file_type':
                                        _e('Only CSV files are accepted for importing', 'be-table-ship');
                                        break;
                                    case 'file_upload':
                                        _e('An error has occurred and the file could not be uploaded', 'be-table-ship');
                                        break;
                                    default:
                                        _e('An unknown error has occurred and the file could not be imported', 'be-table-ship');
                                        break;
                                }
                                ?>
                            </p>
                        </div>
                        <? elseif( isset( $_GET['betrswc-import-status'] ) && $_GET['betrswc-import-status'] == 'success' ) : ?>
                        <div class="updated">
                            <p><?php _e('Your rates have been successfully imported', 'be-table-ship'); ?></p>
                        </div>
                    <?php endif; ?>
                    <h3><?php echo $this->admin_page_heading; ?></h3>
                    <p><?php echo $this->admin_page_description; ?></p>
                    <table class="form-table">
                        <?php
                        // Generate the HTML For the settings form.
                        $this->generate_settings_html();
                        ?>
                        <tr valign="top" id="shipping_handling_rates">
                            <th scope="row" class="titledesc"><?php _e('Handling / Base Rates', 'be-table-ship'); ?>:</th>
                            <td class="forminp" id="<?php echo $this->id; ?>_handling_rates">
                                <table class="shippingrows widefat" style="width: 60%;min-width:550px;" cellspacing="0">
                                    <thead>
                                        <tr>
                                            <th class="check-column"><input type="checkbox"></th>
                                            <th><?php _e('Zone', 'be-table-ship'); ?> <a class="tips" data-tip="<?php _e('Setup and review zones under the Shipping Zones tab', 'be-table-ship'); ?>">[?]</a></th>
                                            <th><?php _e('Fee', 'be-table-ship'); ?> <a class="tips" data-tip="<?php _e('Adds the specified percentage of purchase total followed by the fixed fee', 'be-table-ship'); ?>">[?]</a></th>
                                        </tr>
                                    </thead>
                                    <tfoot>
                                        <tr>
                                            <th colspan="2"><a href="#" class="add button"><?php _e('Add Handling Fee', 'be-table-ship'); ?></a></th>
                                            <th colspan="1" style="text-align:right;"><a href="#" class="remove button"><?php _e('Delete selected fees', 'be-table-ship'); ?></a></th>
                                        </tr>
                                    </tfoot>
                                    <tbody class="class_priorities">
                                        <?php
                                        $i = -1;
                                        if (count($this->handling_rates) > 0) {
                                            foreach ($this->handling_rates as $id => $arr) {
                                                $countryOps = "";
                                                $i++;
                                                foreach ($zones as $val) {
                                                    $countryOps .= '<option value="' . $val['zone_id'] . '" ' . selected($val['zone_id'], $arr['zone'], false) . '>' . $val['zone_title'] . '</option>';
                                                }
                                                echo '<tr class="handling_fees">
			                		    <td class="check-column"><input type="checkbox" name="select" /></td>
					                    <td><select name="' . $this->id . '_handling_country[' . $i . ']">' . $countryOps . '</select></td>
			                		    <td>' . $cur_symbol . '<input type="text" value="' . $arr['fee'] . '" name="' . $this->id . '_handling_fee[' . $i . ']" size="5" /> &nbsp; % <input type="text" value="' . $arr['percent'] . '" name="' . $this->id . '_handling_percent[' . $i . ']" size="5" /></td></tr>';
                                            }
                                        } echo '<tr colspan="3">' . _e('Set different handling rates or base fees for different countries. These prices will be added to all qualifying orders.', 'be-table-ship') . '</tr>';
                                        ?>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        <tr valign="top" id="table_rate_based">
                            <th scope="row" class="titledesc"><?php _e('Shipping Table Rates', 'be-table-ship'); ?>:</th>
                            <td class="forminp" id="<?php echo $this->id; ?>_table_rates">
                                <table class="shippingrows widefat" cellspacing="0">
                                    <thead>
                                        <tr>
                                            <th class="check-column"><input type="checkbox"></th>
                                            <th class="shipping_class"><?php _e('Title', 'be-table-ship'); ?>* <a class="tips" data-tip="<?php _e('This controls the title which the user sees during checkout', 'be-table-ship'); ?>">[?]</a></th>
                                            <th class="shipping_class"><?php _e('Identifier', 'be-table-ship'); ?> <a class="tips" data-tip="<?php _e('Separates which rates are combined and which become different options. If left blank, one will be generated.', 'be-table-ship'); ?>">[?]</a></th>
                                            <th><?php _e('Zone', 'be-table-ship'); ?>* <a class="tips" data-tip="<?php _e('Setup and review zones under the Shipping Zones tab', 'be-table-ship'); ?>">[?]</a></th>
                                            <th><?php _e('Shipping Class', 'be-table-ship'); ?></th>
                                            <th><?php _e('Based On', 'be-table-ship'); ?></th>
                                            <th><?php _e('Min', 'be-table-ship'); ?></th>
                                            <th><?php _e('Max', 'be-table-ship'); ?></th>
                                            <th><?php _e('Cost', 'be-table-ship'); ?> <a class="tips" data-tip="<?php
                    echo $cur_symbol . ' - ';
                    echo __('Fixed Price', 'be-table-ship') . '&lt;br /&gt;% - ' . __('Percentage of Subtotal', 'be-table-ship') . '&lt;br /&gt;x - ' . __('Multiply cost by quantity', 'be-table-ship') . '&lt;br /&gt;w - ' . __('Multiply cost by weight', 'be-table-ship') . '&lt;br /&gt;D - ' . __('Deny: the titled shipping rate will be removed', 'be-table-ship');
                                        ?>">[?]</a></th>
                                            <th><?php _e('Bundle', 'be-table-ship'); ?> <a class="tips" data-tip="<?php _e('If supplied, charges cost up until quantity given. Then charges second price for this and every item after.', 'be-table-ship'); ?>">[?]</a></th>
                                            <th><?php _e('Default', 'be-table-ship'); ?> <a class="tips" data-tip="<?php _e('Check the box to set this option as the default selected choice on the cart page', 'be-table-ship'); ?>">[?]</a></th>
                                        </tr>
                                    </thead>
                                    <tfoot>
                                        <tr>
                                            <th colspan="3">
                                                <a href="#" class="add button"><?php _e('Add Table Rate', 'be-table-ship'); ?></a>
                                                <a href="#" class="button" id="import-table-rates"><?php _e('Import from CSV', 'be-table-ship'); ?></a></th>
                                            <th colspan="8" style="text-align:right;">
                                                <a href="#" class="double button"><?php _e('Duplicate selected rates', 'be-table-ship'); ?></a>
                                                <a href="#" class="remove button"><?php _e('Delete selected rates', 'be-table-ship'); ?></a></th>
                                        </tr>
                                    </tfoot>
                                    <tbody class="table_rates">
                                        <?php
                                        $i = -1;
                                        if ($this->table_rates) {
                                            foreach ($this->table_rates as $class => $rate) {
                                                $i++;
                                                $selType = "<select name=\"" . $this->id . "_shiptype[" . $i . "]\" class=\"shiptype\">
										<option>" . $cur_symbol . "</option>
										<option";
                                                if ($rate['shiptype'] == "%")
                                                    $selType .= " selected=\"selected\"";
                                                $selType .= ">%</option>
										<option";
                                                if ($rate['shiptype'] == "x")
                                                    $selType .= " selected=\"selected\"";
                                                $selType .= ">x</option>
										<option";
                                                if ($rate['shiptype'] == "w")
                                                    $selType .= " selected=\"selected\"";
                                                $selType .= ">w</option>
										<option";
                                                if ($rate['shiptype'] == "v")
                                                    $selType .= " selected=\"selected\"";
                                                $selType .= ">v</option>
										<option";
                                                if ($rate['shiptype'] == "D")
                                                    $selType .= " selected=\"selected\"";
                                                $selType .= ">D</option></select>";
                                                $condOps = "";
                                                foreach ($conds as $key => $val) {
                                                    $condOps .= '<option value="' . $key . '" ' . selected($rate['cond'], $key, false) . '>' . $val . '</option>';
                                                }
                                                $zoneOps = "";
                                                foreach ($zones as $value) {
                                                    $zoneOps .= '<option value="' . $value['zone_id'] . '" ' . selected($rate['zone'], $value['zone_id'], false) . '>' . $value['zone_title'] . '</option>';
                                                }

                                                echo '<tr class="cart_rate">
			                		    <td class="check-column"><input type="checkbox" name="select" /></td>
			                		    <td><input type="text" value="' . stripslashes($rate['title']) . '" name="' . $this->id . '_title[' . $i . ']" class="title" size="25" /></td>
			                		    <td><input type="text" value="' . $rate['identifier'] . '" name="' . $this->id . '_identifier[' . $i . ']" class="identifier" size="25" /></td>
					                    <td><select name="' . $this->id . '_zone[' . $i . ']" class="zone">' . $zoneOps . '</select></td>
					                    <td><select name="' . $this->id . '_class[' . $i . ']" class="class"><option>*</option>';
                                                foreach ($shippingClasses as $key => $val)
                                                    echo '<option value="' . $val->term_id . '" ' . selected($rate['class'], $val->term_id, false) . '>' . $val->name . '</option>';
                                                echo '</select></td><td><select name="' . $this->id . '_cond[' . $i . ']" class="condition">' . $condOps . '</select></td>
					                    <td><input type="text" value="' . $rate['min'] . '" name="' . $this->id . '_min[' . $i . ']" class="min" placeholder="' . __('n/a', 'be-table-ship') . '" size="6" /></td>
					                    <td><input type="text" value="' . $rate['max'] . '" name="' . $this->id . '_max[' . $i . ']" class="max" placeholder="' . __('n/a', 'be-table-ship') . '" size="6" /></td>
					                    <td>' . $selType . ' <input type="text" value="' . $rate['cost'] . '" name="' . $this->id . '_cost[' . $i . ']" class="cost" placeholder="' . __('0.00', 'be-table-ship') . '" size="6" /></td>
					                    <td>qty >= <input type="text" value="' . $rate['bundle_qty'] . '" name="' . $this->id . '_bundle_qty[' . $i . ']" class="bundle_qty" placeholder="0" size="3" /><br />' . $cur_symbol . '
					                    	<input type="text" value="' . $rate['bundle_cost'] . '" name="' . $this->id . '_bundle_cost[' . $i . ']" class="bundle_cost" placeholder="' . __('0.00', 'be-table-ship') . '" size="6" /></td>
					                    <td><input type="checkbox" name="' . $this->id . '_default[' . $i . ']" class="default" ' . checked($rate['default'], 'on', false) . ' /></td>
				                    </tr>';
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        <tr valign="top" id="shipping_class_priorities">
                            <th scope="row" class="titledesc"><?php _e('Shipping Class Priorities', 'be-table-ship'); ?>:</th>
                            <td class="forminp" id="<?php echo $this->id; ?>_class_priorities">
                                <table class="shippingrows widefat" cellspacing="0">
                                    <thead>
                                        <tr>
                                            <th class="shipping_class"><?php _e('Shipping Class', 'be-table-ship'); ?></th>
                                            <th><?php _e('Priority', 'be-table-ship'); ?> <a class="tips" data-tip="Enter any whole number, largest number is highest priority">[?]</a></th>
                                            <th><?php _e('Exclude', 'be-table-ship'); ?> <a class="tips" data-tip="If shipping is free for items with this class, check the box to exclude these cart items from the per-order method">[?]</a></th>
                                        </tr>
                                    </thead>
                                    <tfoot>
                                        <tr>
                                            <th colspan="3"><i><?php _e('These priorities will be used to calculate the appropriate shipping price in the table above. When an order has items of different shipping classes, the one with the highest priority will be used.', 'be-table-ship'); ?></i></th>
                                        </tr>
                                    </tfoot>
                                    <tbody class="class_priorities">
                                        <?php
                                        $class_priorities_array = array();
                                        if (count($shippingClasses) > 0) {
                                            foreach ($shippingClasses as $key => $val) {
                                                $class_priorities_array[$val->term_id] = array("term_id" => $val->term_id, "name" => $val->name, "priority" => (float) 10, "exclude" => '0');
                                            }
                                        }
                                        if (count($this->class_priorities) > 0) {
                                            foreach ($this->class_priorities as $key => $val) {
                                                if (!array_key_exists($val['term_id'], $class_priorities_array))
                                                    unset($this->class_priorities[$val['term_id']]);
                                                elseif ($class_priorities_array[$key]['name'] != $val['name'])
                                                    $this->class_priorities[$key]['name'] = $class_priorities_array[$key]['name'];
                                            }
                                        }
                                        $class_priorities_array = $this->class_priorities + $class_priorities_array;

                                        // Sort Array by Priority
                                        if (count($class_priorities_array) > 0) {
                                            foreach ($class_priorities_array as $key => $row) {
                                                $name[$key] = $row['name'];
                                                $priority[$key] = $row['priority'];
                                            }
                                            array_multisort($priority, SORT_DESC, $name, SORT_ASC, $class_priorities_array);
                                        }

                                        $i = -1;
                                        if (count($class_priorities_array) > 0) {
                                            foreach ($class_priorities_array as $id => $arr) {
                                                $i++;
                                                $checked = ($arr['excluded'] == 'on') ? ' checked="checked"' : '';
                                                echo '<tr class="shipping_class">
			                			<input type="hidden" name="' . $this->id . '_scpid[' . $i . ']" value="' . $arr['term_id'] . '" />
			                			<input type="hidden" name="' . $this->id . '_scp[' . $i . ']" value="' . $id . '" />
			                			<input type="hidden" name="' . $this->id . '_sname[' . $i . ']" value="' . $arr['name'] . '" />
			                			<td>' . $arr['name'] . '</td>
			                		    <td><input type="text" value="' . $arr['priority'] . '" name="' . $this->id . '_priority[' . $i . ']" size="5" /></td>
			                		    <td><input type="checkbox" ' . $checked . '" name="' . $this->id . '_excluded[' . $i . ']" size="5" /></td>';
                                            }
                                        } else
                                            echo '<tr colspan="3"><td>You have no shipping classes available</td></tr>'
                                            ?>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </table><!--/.form-table-->
                    <h3 class="title_drop title_h4 ship_free_title"><?php _e('Set the Order Shipping Options Will Appear', 'be-table-ship'); ?></h3>
                    <table class="form-table">
                        <tr valign="top" id="shipping_title_order">
                            <th scope="row" class="titledesc"><?php _e('Shipping Cost Order', 'be-table-ship'); ?>:</th>
                            <td class="forminp" id="<?php echo $this->id; ?>_order_titles">
                                <table class="shippingrows widefat" cellspacing="0">
                                    <tbody>
                                        <?php
                                        if (count($this->title_order) > 0) {
                                            foreach ($this->title_order as $tor) {
                                                ?>
                                                <tr><td class="title"><input type="hidden" name="<?php echo $this->id; ?>_title_order[]" value="<?php echo $tor; ?>"><span><?php echo $tor; ?></span></td></tr>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                                <p><?php _e('Not seeing all of your options', 'be-table-ship'); ?>? <a href="#" id="refresh_list"><?php _e('Refresh List', 'be-table-ship'); ?></a></p>
                            </td>
                        </tr>
                    </table>
                    <script type="text/javascript">
                        jQuery(function () {
                            if (jQuery('h4.title_drop').length != 0)
                                settings_headline = jQuery('h4.title_drop');
                            else
                                settings_headline = jQuery('h3.title_drop')

                            settings_headline.next('.form-table').css('display', 'none');
                            jQuery('.title_drop.general_settings_title').next('.form-table').css('display', 'table');
                            jQuery('.title_drop.table_settings_title').next('.form-table').css('display', 'table');
                            settings_headline.live('click', function () {
                                if (jQuery(this).next('.form-table').is(":hidden")) {
                                    jQuery(this).next('.form-table').show("slow", "linear");
                                    jQuery(this).addClass('active');
                                } else {
                                    jQuery(this).next('.form-table').hide("slow", "linear");
                                    jQuery(this).removeClass('active');
                                }
                                //jQuery(this).next('.form-table').slideToggle("slow");
                            });

                            jQuery('#<?php echo $this->id; ?>_table_rates a.add').live('click', function () {
                                var size = jQuery('#<?php echo $this->id; ?>_table_rates tbody .cart_rate').size();

                                jQuery('<tr class="cart_rate">\
                                                <td class="check-column"><input type="checkbox" name="select" /></td>\
                                                <td><input type="text" name="<?php echo $this->id; ?>_title[' + size + ']" class="title" size="25" /></td>\
                                                <td><input type="text" name="<?php echo $this->id; ?>_identifier[' + size + ']" class="identifier" size="25" /></td>\                                                 <td><select name="<?php echo $this->id; ?>_zone[' + size + ']" class="zone"><?php echo addslashes($zoneOpsJS); ?></select></td>\
                                                <td><select name="<?php echo $this->id; ?>_class[' + size + ']" class="class"><option>*</option><?php echo addslashes($classOpsG); ?></select></td>\
                                                <td><select name="<?php echo $this->id; ?>_cond[' + size + ']" class="condition"><?php echo addslashes($condOpsG); ?></select></td>\
                                                <td><input type="text" name="<?php echo $this->id; ?>_min[' + size + ']" class="min" placeholder="0" size="6" /></td>\
                                                <td><input type="text" name="<?php echo $this->id; ?>_max[' + size + ']" class="max" placeholder="*" size="6" /></td>\
                                                <td><select name="<?php echo $this->id; ?>_shiptype[' + size + ']" class="shiptype"><option><?php echo $cur_symbol; ?></option><option>%</option><option>x</option><option>w</option><option>v</option><option>D</option></select>\
                                                    <input type="text" name="<?php echo $this->id; ?>_cost[' + size + ']" class="cost" placeholder="0.00" size="6" /></td>\
                                                        <td>qty >= <input type="text" name="<?php echo $this->id; ?>_bundle_qty[' + size + ']" class="bundle_qty" placeholder="0" size="3" /><br />\
                    <?php echo $cur_symbol; ?> <input type="text" name="<?php echo $this->id; ?>_bundle_cost[' + size + ']" class="bundle_cost" placeholder="0.00" size="6" /></td>\
                                                                <td><input type="checkbox" name="<?php echo $this->id; ?>_default[' + size + ']" class="default" /></td>\
                                                                            </tr>').appendTo('#<?php echo $this->id; ?>_table_rates table tbody');

                                return false;
                            });

                            // Duplicate row
                            jQuery('#<?php echo $this->id; ?>_table_rates a.double').live('click', function () {
                                var size = jQuery('#<?php echo $this->id; ?>_table_rates tbody .cart_rate').size();

                                jQuery('#<?php echo $this->id; ?>_table_rates table tbody tr td.check-column input:checked').each(function (i, el) {

                                    jQuery('<tr class="cart_rate">\
                                                        <td class="check-column"><input type="checkbox" name="select" /></td>\
                                                        <td><input type="text" name="<?php echo $this->id; ?>_title[' + size + ']" class="title" size="25" value="' + jQuery(el).closest('tr').find('.title').val() + '" /></td>\
                                                    <td><input type="text" name="<?php echo $this->id; ?>_identifier[' + size + ']" class="identifier" size="25" /></td>\                                                         <td><select name="<?php echo $this->id; ?>_zone[' + size + ']" class="zone"><?php echo addslashes($zoneOpsJS); ?></select></td>\
                                                        <td><select name="<?php echo $this->id; ?>_class[' + size + ']" class="class"><option>*</option><?php echo addslashes($classOpsG); ?></select></td>\
                                                        <td><select name="<?php echo $this->id; ?>_cond[' + size + ']" class="condition"><?php echo addslashes($condOpsG); ?></select></td>\
                                                        <td><input type="text" name="<?php echo $this->id; ?>_min[' + size + ']" class="min" value="' + jQuery(el).closest('tr').find('.min').val() + '" placeholder="0" size="6" /></td>\
                                                        <td><input type="text" name="<?php echo $this->id; ?>_max[' + size + ']" class="max" value="' + jQuery(el).closest('tr').find('.max').val() + '" placeholder="*" size="6" /></td>\
                                                        <td><select name="<?php echo $this->id; ?>_shiptype[' + size + ']" class="shiptype"><option><?php echo $cur_symbol; ?></option><option>%</option><option>x</option><option>w</option><option>D</option></select>\
                                                            <input type="text" name="<?php echo $this->id; ?>_cost[' + size + ']" class="cost" value="' + jQuery(el).closest('tr').find('.cost').val() + '" placeholder="0.00" size="6" /></td>\                                                                 <td>qty >= <input type="text" name="<?php echo $this->id; ?>_bundle_qty[' + size + ']" placeholder="0" value="' + jQuery(el).closest('tr').find('.bundle_qty').val() + '" size="3" /><br />\
                    <?php echo $cur_symbol; ?> <input type="text" name="<?php echo $this->id; ?>_bundle_cost[' + size + ']" value="' + jQuery(el).closest('tr').find('.bundle_cost').val() + '" class="bundle_cost" placeholder="0.00" size="6" /></td>\
                                                                <td><input type="checkbox" name="<?php echo $this->id; ?>_default[' + size + ']" class="default" /></td>\
                                                                                </tr>').appendTo('#<?php echo $this->id; ?>_table_rates table tbody');

                                    jQuery('#<?php echo $this->id; ?>_table_rates table tbody tr').last().find('select.zone').val(jQuery(el).closest('tr').find('select.zone').val())
                                    jQuery('#<?php echo $this->id; ?>_table_rates table tbody tr').last().find('select.class').val(jQuery(el).closest('tr').find('select.class').val())
                                    jQuery('#<?php echo $this->id; ?>_table_rates table tbody tr').last().find('select.condition').val(jQuery(el).closest('tr').find('select.condition').val())
                                    jQuery('#<?php echo $this->id; ?>_table_rates table tbody tr').last().find('select.shiptype').val(jQuery(el).closest('tr').find('select.shiptype').val())
                                    if (jQuery(el).closest('tr').find('.default').attr('checked') == 'checked')
                                        jQuery('#<?php echo $this->id; ?>_table_rates table tbody tr').last().find('.default').attr('checked', 'checked');

                                    size = size + 1;
                                });
                                return false;
                            });

                            // Remove row
                            jQuery('#<?php echo $this->id; ?>_table_rates a.remove').live('click', function () {
                                var answer = confirm("<?php _e('Delete the selected rates', 'be-table-ship'); ?>?")
                                if (answer) {
                                    jQuery('#<?php echo $this->id; ?>_table_rates table tbody tr td.check-column input:checked').each(function (i, el) {
                                        jQuery(el).closest('tr').remove();
                                    });
                                }
                                return false;
                            });

                            jQuery('#<?php echo $this->id; ?>_handling_rates a.add').live('click', function () {

                                var size = jQuery('#<?php echo $this->id; ?>_handling_rates tbody .handling_fees').size();
                                jQuery('<tr class="handling_fees">\
                                                <td class="check-column"><input type="checkbox" name="select" /></td>\
                                                <td><select name="<?php echo $this->id; ?>_handling_country[' + size + ']"><?php echo addslashes($zoneOpsJS); ?></select></td>\
                                                <td><?php echo $cur_symbol; ?> <input type="text" name="<?php echo $this->id; ?>_handling_fee[' + size + ']" placeholder="0.00" size="5" /> &nbsp; % <input type="text" name="<?php echo $this->id; ?>_handling_percent[' + size + ']" placeholder="0.00" size="5" /></td>\
                                                            </tr>').appendTo('#<?php echo $this->id; ?>_handling_rates table tbody');
                                return false;
                            });

                            // Remove row
                            jQuery('#<?php echo $this->id; ?>_handling_rates a.remove').live('click', function () {
                                var answer = confirm("<?php _e('Delete the selected rates', 'be-table-ship'); ?>?")
                                if (answer) {
                                    jQuery('#<?php echo $this->id; ?>_handling_rates table tbody tr td.check-column input:checked').each(function (i, el) {
                                        jQuery(el).closest('tr').remove();
                                    });
                                }
                                return false;
                            });

                            jQuery('#refresh_list').live('click', function () {
                                var tableAr = new Array();
                                var titlesAr = new Array();
                                jQuery('#<?php echo $this->id; ?>_order_titles table tbody tr').each(function (i, el) {
                                    titlesAr.push(jQuery(el).closest('tr').find('td.title span').html());
                                });
                                jQuery('#<?php echo $this->id; ?>_table_rates table tbody tr').each(function (i, el) {
                                    tableAr.push(jQuery(el).closest('tr').find('input.identifier').val());
                                });

                                for (x = 0; x < tableAr.length; x++) {
                                    if (jQuery.inArray(tableAr[x], titlesAr) == -1) {
                                        titlesAr.push(tableAr[x]);
                                        jQuery('<tr><td class="title ' + tableAr[x] + '"><input type="hidden" name="<?php echo $this->id; ?>_title_order[]" value="' + tableAr[x] + '"><span>' + tableAr[x] + '</span></td></tr>').appendTo('#<?php echo $this->id; ?>_order_titles table tbody');
                                    }
                                }

                                for (y = 0; y < titlesAr.length; y++) {
                                    if (jQuery.inArray(titlesAr[y], tableAr) == -1) {
                                        jQuery('#<?php echo $this->id; ?>_order_titles table tbody tr:contains("' + titlesAr[y] + '")').remove();
                                    }
                                }

                                return false;
                            });

                            jQuery(function () {
                                var fixHelperModified = function (e, tr) {
                                    var $originals = tr.children();
                                    var $helper = tr.clone();
                                    $helper.children().each(function (index)
                                    {
                                        jQuery(this).width($originals.eq(index).width())
                                    });
                                    return $helper;
                                };
                                jQuery("#<?php echo $this->id; ?>_order_titles table tbody").sortable({
                                    helper: fixHelperModified
                                }).disableSelection();
                            });

                            // Hide popup window on cancel
                            jQuery('.be-popup-container .cancel').live('click', function () {
                                jQuery('.be-compare-popup').remove();
                                return false;
                            });

                            // Create new category button
                            jQuery(document).on('click', '#import-table-rates', function (e) {

                                //prevent default action (hyperlink)
                                e.preventDefault();

                                var create_form =
                                        '<div id="be-table-rates-import" class="be-compare-popup">' +
                                        '<div class="be-popup-container add_form" id="be-table-rates-import-form">' +
                                        '<form method="post" enctype="multipart/form-data">' +
                                        '<?php wp_nonce_field('be-table-import-nonce', 'betrs-import-nonce'); ?>' +
                                        '<p><label for="cat_id"><?php _e('Import Table Rates', 'be-table-ship'); ?></label></p>' +
                                        '<p><span><?php _e('Select a CSV file', 'be-table-ship'); ?></span></p>' +
                                        '<p><input type="file" name="file_csv" id="file_csv" /></p>' +
                                        '<p><input type="submit" name="be_table_import" value="<?php _e('Upload', 'be-table-ship'); ?>" class="form_submit" /> <a href="#" class="cancel"><?php _e('Cancel', 'be-table-ship'); ?></a></p>' +
                                        '</form>' +
                                        '</div>' +
                                        '</div>';

                                //insert lightbox HTML into page
                                jQuery('body').append(create_form);
                                betrswc_doBoxSize();

                            });

                            function betrswc_doBoxSize() {
                                // set max height for popup box
                                var window_height = jQuery(window).height();
                                var box_height = window_height - 180;
                                jQuery('.be-popup-container').css('max-height', box_height + 'px');
                            }
                            jQuery(window).on('resize', function () {
                                betrswc_doBoxSize();
                            });

                            // add block after submitting
                            jQuery('#be-table-rates-import-form').live('submit', function (e) {

                                jQuery(this).block({message: null, overlayCSS: {background: '#fff', backgroundSize: '32px 32px', opacity: 0.6}});

                            });
                        });
                    </script>
                    <?php
                }

// End admin_options()

                /**
                 * import rows to table function.
                 *
                 * @access public
                 * @return void
                 */
                static function import_table_csv() {
// check if form is submitted
                    if (!isset($_POST['be_table_import']))
                        return;

                    if (!wp_verify_nonce($_POST['betrs-import-nonce'], 'be-table-import-nonce'))
                        $redirect = add_query_arg(array('betrswc-import-status' => 'error', 'error_code' => 'nonce'));

                    else {
// verify csv file type
                        if (pathinfo($_FILES['file_csv']['name'], PATHINFO_EXTENSION) != 'csv')
                            $redirect = add_query_arg(array('betrswc-import-status' => 'error', 'error_code' => 'file_type'));

                        else {
// handle temporary file upload
                            $upload_dir = wp_upload_dir();
                            $tmp_file = $upload_dir['basedir'] . '/import-table-rates.csv';
                            if (move_uploaded_file($_FILES['file_csv']['tmp_name'], $tmp_file)) {
// success uploading, open file
                                $file = $_FILES[csv][tmp_name];
                                $handle = fopen($tmp_file, "r");
//$file_contents = fgets( $handle );
                                $i = 0;
                                $table_rates = get_option('woocommerce_table_rates');

// determine zone orders
                                $zone_query = get_option('be_woocommerce_shipping_zones');
                                $zone_orders = array();
                                $n = 1;
                                if (count($zone_query)) {
                                    foreach ($zone_query as $value) {
                                        $zone_orders[$value['zone_id']] = $n;
                                        $n++;
                                    }
                                }

// determine class priorities
                                $class_priorities = get_option('woocommerce_class_priorities');

// cycle through line by line for data
                                while (( $data = fgetcsv($handle, 0, ",") ) !== FALSE) :
                                    $num = count($data);
                                    if ($num >= 9) :
                                        $title = sanitize_text_field($data[0]);
                                        $id = sanitize_title($data[1]);
//                                        $zone = (int) $data[2];
                                        $zone = '';
                                        foreach ($zone_query as $fgc_zone) {
                                            if ($fgc_zone['zone_title'] == $data[2]) {
                                                $zone = $fgc_zone['zone_id'];
                                            }
                                        }
                                        if (strtolower($data[3]) == 'shipping') {
                                            $class = 26;
                                        } else {
                                            $class = (int) $data[3];
                                        }
                                        $class_priority = ( isset($class_priorities[$class]['priority']) ) ? (int) $class_priorities[$class]['priority'] : 10;
                                        $cond = sanitize_title($data[4]);
                                        $min = (float) $data[5];
                                        $max = ( $data[6] == '*' ) ? '*' : (float) $data[6];
                                        switch ($data[7]) {
                                            case '%':
                                            case 'x':
                                            case 'w':
                                            case 'D':
                                                $ctype = $data[7];
                                            default:
                                                $ctype = get_woocommerce_currency_symbol();
                                        }
                                        $cost = (float) $data[8];
                                        $bundle1 = ( isset($data[9]) ) ? (int) $data[9] : 0;
                                        $bundle2 = ( isset($data[10]) ) ? (float) $data[10] : 0;
                                        $default = ( isset($data[11]) && $data[11] == "yes" ) ? "on" : 0;

// add to array of rates
                                        $fgc_is_exist = false;
                                        foreach ($table_rates as $value) {
                                            if ($value['identifier'] == $id) {
                                                $fgc_is_exist = true;
                                            }
                                        }
                                        if (!$fgc_is_exist && $data[2] != "Cote d'Ivoire" && $data[2] != "Kosovo") {
                                            $table_rates[] = array(
                                                'title' => $title,
                                                'identifier' => $id,
                                                'zone' => $zone,
                                                'zone_order' => $zone_orders[$zone],
                                                'class' => $class,
                                                'class_priority' => $class_priority,
                                                'cond' => $cond,
                                                'min' => $min,
                                                'max' => $max,
                                                'shiptype' => $ctype,
                                                'cost' => $cost,
                                                'bundle_qty' => $bundle1,
                                                'bundle_cost' => $bundle2,
                                                'default' => $default
                                            );
                                        }
                                    endif;
                                    $i++;
                                endwhile;
                                fclose($handle);

// Obtain a list of columns
                                $zone_order = $class_priority = $min = $title = $cost = array();
                                if (count($table_rates)) {
                                    foreach ($table_rates as $key => $row) {
                                        $zone_order[$key] = $row['zone_order'];
                                        $class_priority[$key] = $row['class_priority'];
                                        $min[$key] = $row['min'];
                                        $title[$key] = $row['title'];
                                        $cost[$key] = $row['cost'];
                                    }
// Sort the rates based on the 5 columns
                                    array_multisort($zone_order, SORT_ASC, $class_priority, SORT_ASC, $min, SORT_ASC, $title, SORT_ASC, $cost, SORT_ASC, $table_rates);
                                }
//                                exit();
// save imported rows in addition to existing rates
                                update_option('woocommerce_table_rates', $table_rates);
                                $redirect = add_query_arg(array('betrswc-import-status' => 'success'));
                                unlink($tmp_file);
                            } else {
// error uploading file
                                $redirect = add_query_arg(array('betrswc-import-status' => 'error', 'error_code' => 'file_upload'));
                            }
                        }
                    }

                    wp_safe_redirect($redirect);

                    die();
                }

                /**
                 * process_cart_rates function.
                 *
                 * @access public
                 * @return void
                 */
                function process_table_rates() {
                    global $wpdb;

// Initialize blank arrays & save variables
                    $table_rate_title = $table_rate_zone = $table_rate_class = $table_rate_cond = $table_rate_min = $table_rate_max = $table_rate_cost = $table_rate_bundle_qty = $table_rate_bundle_cost = $table_rate_default = $table_rates = $table_rate_priority = $class_scpid = $class_scp = $class_sname = $class_priorities = $class_excluded = $handling_country = $handling_fee = $handling_percent = $title_order = array();
                    $saveNames = array('_title', '_identifier', '_zone', '_class', '_cond', '_min', '_max', '_shiptype', '_cost', '_bundle_qty', '_bundle_cost', '_default', '_title_order');

// Clean table rate data
                    foreach ($saveNames as $sn) {
                        $save_name = 'table_rate' . $sn;
                        if (isset($_POST[$this->id . $sn]))
                            $$save_name = array_map('woocommerce_clean', $_POST[$this->id . $sn]);
                    }
                    if (isset($table_rate_title_order) && count($table_rate_title_order))
                        $table_rate_title_order = array_map('sanitize_title', $table_rate_title_order);
                    else
                        $table_rate_title_order = array();
// Clean handling data
                    $saveNames = array('_country', '_fee', '_percent');
                    foreach ($saveNames as $sn) {
                        $save_name = 'handling' . $sn;
                        if (isset($_POST[$this->id . '_handling' . $sn]))
                            $$save_name = array_map('woocommerce_clean', $_POST[$this->id . '_handling' . $sn]);
                    }
// Clean classes data
                    $saveNames = array('_scpid', '_scp', '_sname', '_priority', '_excluded');
                    foreach ($saveNames as $sn) {
                        $save_name = 'class' . $sn;
                        if (isset($_POST[$this->id . $sn]))
                            $$save_name = array_map('woocommerce_clean', $_POST[$this->id . $sn]);
                    }

// Get max key
                    $values = $class_scp;
                    ksort($values);
                    $value = end($values);
                    $key = key($values);

                    for ($i = 0; $i <= $key; $i++) {
                        if (isset($class_scp[$i])) {
                            if ($class_priority[$i] == '' || !is_numeric($class_priority[$i]))
                                $class_priority[$i] = '10';

// Add priorities to class priorities array
                            $class_priorities[sanitize_title($class_scpid[$i])] = array(
                                "term_id" => $class_scpid[$i],
                                "name" => $class_sname[$i],
                                'priority' => ceil($class_priority[$i]),
                                'excluded' => ( isset($class_excluded[$i]) ) ? $class_excluded[$i] : FALSE
                            );
                        }
                    }

                    $zone_query = get_option('be_woocommerce_shipping_zones');
                    $zone_orders = array();
                    $n = 1;
                    if (count($zone_query)) {
                        foreach ($zone_query as $value) {
                            $zone_orders[$value['zone_id']] = $n;
                            $n++;
                        }
                    }

// Get max key
                    $values = $handling_country;
                    ksort($values);
                    $value = end($values);
                    $key = key($values);

                    for ($i = 0; $i <= $key; $i++) {
                        if (isset($handling_country[$i])) {
//if($handling_fee[$i] == '' || !is_numeric($handling_fee[$i])) $handling_fee[$i] = '0';
//if($handling_percent[$i] == '' || !is_numeric($handling_percent[$i])) $handling_percent[$i] = '0';
//$handling_fee[$i] = number_format($handling_fee[$i], 2,  '.', '');
//$handling_percent[$i] = number_format($handling_percent[$i], 2,  '.', '');
// Add priorities to class priorities array
                            $handling_rates[sanitize_title($handling_country[$i])] = array(
                                "zone" => $handling_country[$i],
                                'zone_order' => $zone_orders[$handling_country[$i]],
                                'fee' => $handling_fee[$i],
                                'percent' => $handling_percent[$i]
                            );
                        }
                    }

// Get max key
                    $values = $table_rate_title;
                    ksort($values);
                    $value = end($values);
                    $key = key($values);

                    for ($i = 0; $i <= $key; $i++) {
                        if (isset($table_rate_title[$i]) && isset($table_rate_zone[$i]) && isset($table_rate_cond[$i])) {

                            if ($table_rate_min[$i] == '')
                                $table_rate_min[$i] = '0';
                            if ($table_rate_max[$i] == '')
                                $table_rate_max[$i] = '*';
                            if ($table_rate_bundle_qty[$i] == '')
                                $table_rate_bundle_qty[$i] = '0';
                            if ($table_rate_bundle_cost[$i] == '' && $table_rate_shiptype[$i] != 'C')
                                $table_rate_bundle_cost[$i] = '0';
                            if ($table_rate_identifier[$i] == '')
                                $table_rate_identifier[$i] = $table_rate_title[$i];
                            $table_rate_identifier[$i] = sanitize_title($table_rate_identifier[$i]);

                            $table_rate_priority_ind = ( isset($table_rate_class[$i]) && $table_rate_class[$i] != '' && $table_rate_class[$i] != '*' ) ? $class_priorities[sanitize_title($table_rate_class[$i])]['priority'] : '';

// Register title with WPML
                            if (function_exists('icl_register_string'))
                                icl_register_string('be-table-ship', 'shipping-title-' . sanitize_title($table_rate_title[$i]), $table_rate_title[$i]);

// Add to cart rates array
                            $table_rates[$i] = array(
                                'title' => $table_rate_title[$i],
                                'identifier' => $table_rate_identifier[$i],
                                'zone' => $table_rate_zone[$i],
                                'zone_order' => $zone_orders[$table_rate_zone[$i]],
                                'class' => $table_rate_class[$i],
                                'class_priority' => $table_rate_priority_ind,
                                'cond' => $table_rate_cond[$i],
                                'min' => $table_rate_min[$i],
                                'max' => $table_rate_max[$i],
                                'shiptype' => $table_rate_shiptype[$i],
                                'cost' => $table_rate_cost[$i],
                                'bundle_qty' => $table_rate_bundle_qty[$i],
                                'bundle_cost' => $table_rate_bundle_cost[$i],
                                'default' => ( isset($table_rate_default[$i]) ) ? $table_rate_default[$i] : 0,
                            );
                        }
                    }

                    $table_rates = $this->sort_table_rates($table_rates);

                    update_option($this->table_rate_options, $table_rates);

                    update_option($this->class_priorities_options, $class_priorities);

// Obtain a list of columns
                    $zone_order = $fee = $percent = array();
                    if (isset($handling_rates) && count($handling_rates)) {
                        foreach ($handling_rates as $key => $row) {
                            $zone_order[$key] = $row['zone_order'];
                            $fee[$key] = $row['fee'];
                            $percent[$key] = $row['percent'];
                        }

// Sort the base fees based on the 3 columns
                        array_multisort($zone_order, SORT_ASC, $fee, SORT_ASC, $percent, SORT_ASC, $handling_rates);
                    } else
                        $handling_rates = array();

                    update_option($this->handling_rates_options, $handling_rates);

                    update_option($this->title_order_options, $table_rate_title_order);

                    $this->get_table_rates();
                    $this->get_class_priorities();
                    $this->get_handling_rates();
                    $this->get_title_order();
                }

                /**
                 * sort_table_rates function.
                 * sorts a multi-dimensional array by secondary value
                 *
                 * @access public
                 * @return string
                 */
                static function sort_table_rates($table_rates = array()) {
// Obtain a list of columns
                    $zone_order = $class_priority = $min = $title = $cost = array();
                    if (count($table_rates)) {
                        foreach ($table_rates as $key => $row) {
                            $zone_order[$key] = $row['zone_order'];
                            $class_priority[$key] = $row['class_priority'];
                            $min[$key] = $row['min'];
                            $title[$key] = $row['title'];
                            $cost[$key] = $row['cost'];
                        }
// Sort the rates based on the 5 columns
                        array_multisort($zone_order, SORT_ASC, $class_priority, SORT_ASC, $min, SORT_ASC, $title, SORT_ASC, $cost, SORT_ASC, $table_rates);
                    }

                    return $table_rates;
                }

                /**
                 * get_highest_priority_class function.
                 * sorts a multi-dimensional array by secondary value
                 *
                 * @access public
                 * @return string
                 */
                function get_highest_priority_class($classes = array()) {
                    $classTerm = $classHigh = 0;
                    $class_priorities = $this->class_priorities;

                    if (isset($classes) && is_array($classes) && count($classes)) {
                        $new_class_priorities = array();
                        foreach ($classes as $class) {
                            $term = get_term_by('slug', $class, 'product_shipping_class', ARRAY_A);
                            if (isset($class_priorities[$term['term_id']]))
                                $new_class_priorities[$term['term_id']] = $class_priorities[$term['term_id']];
                        }
                        $class_priorities = $new_class_priorities;
                    }

                    foreach ($class_priorities as $key => $cls) {
                        if ($cls['excluded'] != 'on' && $cls['priority'] >= $classHigh) {
                            $classTerm = $key;
                            $classHigh = $cls['priority'];
                        }
                    }

                    return $classTerm;
                }

                /**
                 * get_highest_priority_class function.
                 * sorts a multi-dimensional array by secondary value
                 *
                 * @access public
                 * @return string
                 */
                function get_highest_costing_class($shipping_rates, $classes = array()) {
                    $classTerm = $costHigh = 0;
                    $temp = $return = array();
                    $class_priorities = $this->class_priorities;

                    if (isset($classes) && is_array($classes) && count($classes)) {
                        $new_class_priorities = array();
                        foreach ($classes as $class) {
                            $term = get_term_by('slug', $class, 'product_shipping_class', ARRAY_A);
                            if (isset($class_priorities[$term['term_id']]))
                                $new_class_priorities[$term['term_id']] = $class_priorities[$term['term_id']];
                        }
                        $class_priorities = $new_class_priorities;
                    }

                    foreach ($shipping_rates as $sc_slug => $rt) {
                        foreach ($rt as $rate_id => $rtv) {
                            if (!array_key_exists($rate_id, $temp))
                                $temp[$rate_id] = array();
                            if (!empty($temp[$rate_id])) {
                                if ($rtv['cost'] > $temp[$rate_id]['cost']) {
                                    $temp[$rate_id] = $rtv;
                                    $temp[$rate_id]['class'] = $sc_slug;
                                }
                            } else {
                                $temp[$rate_id] = $rtv;
                                $temp[$rate_id]['class'] = $sc_slug;
                            }
                        }
                    }

                    foreach ($temp as $key => $value) {
                        if (!array_key_exists($value['class'], $return))
                            $return[$value['class']] = array();
                        $return[$value['class']][$key] = $value;
                    }

                    return $return;
                }

                /**
                 * get_cart_rates function.
                 *
                 * @access public
                 * @return void
                 */
                function get_table_rates() {
                    $this->table_rates = array_filter((array) get_option($this->table_rate_options));
                }

                /**
                 * get_class_priorities function.
                 *
                 * @access public
                 * @return void
                 */
                function get_class_priorities() {
                    $this->class_priorities = array_filter((array) get_option($this->class_priorities_options));
                }

                /**
                 * get_handling_rates function.
                 *
                 * @access public
                 * @return void
                 */
                function get_handling_rates() {
                    $this->handling_rates = array_filter((array) get_option($this->handling_rates_options));
                }

                /**
                 * get_handling_rates function.
                 *
                 * @access public
                 * @return void
                 */
                function get_title_order() {
                    $this->title_order = array_filter((array) get_option($this->title_order_options));
                }

                /**
                 * is_available function.
                 *
                 * @access public
                 * @param mixed $package
                 * @return bool
                 */
                function select_default_rate($chosen_method, $_available_methods) {
//Select available shipping methods
                    foreach ($_available_methods as $key => $value)
                        $shipping_methods[] = $value->method_id;
                    $shipping_methods = array_unique($shipping_methods);

//Select the 'Default' method from WooCommerce settings
                    $default_shipping_method = esc_attr(get_option('woocommerce_default_shipping_method'));

                    if ($default_shipping_method == 'table_rate_shipping' || (!in_array($default_shipping_method, $shipping_methods) )) {
                        foreach ($this->default_rates as $key => $value) {
                            if (array_key_exists($key, $_available_methods))
                                $chosen_method = $key;
                        }
                    }

                    return $chosen_method;
                }

                /**
                 * Calculate package subtotal
                 *
                 * @param array $package The package array/object being shipped
                 * @return float
                 */
                function calculate_subtotal($items) {
                    $subtotal = 0;

                    foreach ($items as $item)
                        if ($item['data']->needs_shipping())
                            if ($item['line_subtotal'] > 0)
                                $subtotal += $item['line_subtotal'];
                            else
                                $subtotal += $item['data']->price;

                    return $subtotal;
                }

                /**
                 * is_available function.
                 *
                 * @access public
                 * @param mixed $package
                 * @return bool
                 */
                function is_available($package) {
                    global $woocommerce;

                    if ($this->enabled == "no")
                        return false;

                    return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', true, $package);
                }

                /**
                 * be_zone_update_notice
                 *
                 * @package		WooCommerce/Classes/Shipping
                 * @access public
                 * @param array $methods
                 * @return array
                 */
                static function be_zone_update_notice() {
                    global $wpdb;

                    $current_zones = get_option('be_woocommerce_shipping_zones');
                    if (!isset($current_zones) || $current_zones == '' || ( is_array($current_zones) && count($current_zones) == 0 )) {
                        echo '<div class="error" style="font-weight:bold;"><p><span style="text-transform:uppercase;">' . __('Attention', 'be-table-ship') . '</span>: ' . __('You have not defined any shipping zones for the WooCommerce Table Rate Shipping plugin. You must setup your zones before creating any rates in the shipping method\'s settings page.', 'be-table-ship') . ' <a href="' . admin_url('admin.php?page=wc-settings&tab=shipping_zones') . '" class="button-primary">Setup Zones</a></p></div>';
                    }
                }

                /**
                 * install function.
                 *
                 * @package		WooCommerce/Classes/Shipping
                 * @access public
                 * @param array $methods
                 * @return array
                 */
                function install_plugin_button() {
                    global $wpdb;

// upgrade original pre-zone versions
                    $old_version = get_option('be_table_rate_version');
                    if ((float) $old_version <= 3.0) {
                        $zones = array();
                        $zone_id = 1;
                        $table_rates = $this->table_rates;
                        if (count($table_rates) > 0) {
                            if (!array_key_exists('zone', $table_rates[0])) {
                                foreach ($table_rates as $key => $value) {
                                    $tmp = array('country' => $value['country'], 'zip' => $value['zip']);
                                    $is_zone = false;
                                    foreach ($zones as $zk => $z) {
                                        if ($z['country'] == $value['country'] && $z['zip'] == $value['zip'])
                                            $is_zone = $zk;
                                    }
                                    if ($is_zone) {
                                        $table_rates[$key]['zone'] = $is_zone;
                                    } else {
                                        $zone_country = ($value['country']) ? array($value['country']) : array();
                                        $zone_type = ($value['country'] == '*') ? 'everywhere' : 'postal';
                                        $zone_postal = ($value['zip'] == '*') ? '' : $value['zip'];
                                        $zones[$zone_id] = array(
                                            'zone_id' => $zone_id,
                                            'zone_enabled' => 'on',
                                            'zone_title' => 'Zone' . $zone_id,
                                            'zone_description' => '',
                                            'zone_type' => $zone_type,
                                            'zone_country' => $zone_country,
                                            'zone_postal' => $zone_postal,
                                            'zone_except' => '',
                                            'zone_order' => $zone_id,
                                        );
                                        $table_rates[$key]['zone'] = $zone_id_new;
                                        $zone_id++;
                                    }
                                    unset($table_rates[$key]['country']);
                                    unset($table_rates[$key]['zip']);
                                }
                                update_option($this->table_rate_options, $table_rates);
                                $this->get_table_rates();
                            }
                        }
                    }

// upgrade users from 3.2.x
                    $findTable = $wpdb->get_results("SHOW TABLES LIKE '" . $wpdb->prefix . "woocommerce_shipping_zones'", ARRAY_A);
                    if (count($findTable)) {
                        $zones = array();
                        $selectCurrentZones = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "woocommerce_shipping_zones", ARRAY_A);
                        if (count($selectCurrentZones)) {
                            foreach ($selectCurrentZones as $key => $zone) {
                                $zones[$zone['zone_id']] = array(
                                    'zone_id' => $zone['zone_id'],
                                    'zone_enabled' => $zone['zone_enabled'],
                                    'zone_title' => $zone['zone_title'],
                                    'zone_description' => $zone['zone_description'],
                                    'zone_type' => $zone['zone_type'],
                                    'zone_country' => $zone['zone_country'],
                                    'zone_postal' => $zone['zone_postal'],
                                    'zone_order' => $zone['zone_order'],
                                );
                            }
                        }
                        update_option('be_woocommerce_shipping_zones', $zones);
                    }

                    if (!get_option('be_table_rate_version'))
                        add_option('be_table_rate_version', BE_Table_Rate_Shipping::$version);
                    else
                        update_option('be_table_rate_version', BE_Table_Rate_Shipping::$version);
                    ?>
                    <div class="updated" style="font-weight:bold;">
                        <p><?php _e('Your zones have been updated. Please test your forms to ensure that everything is in working order.', 'be_table_rate'); ?></p>
                    </div>
                    <p><a href="<?php echo admin_url('admin.php?page=wc-settings&tab=shipping&section=be_table_rate_shipping'); ?>" class="button-primary">Continue to Table Rate Settings</a> <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=shipping_zones'); ?>">View Shipping Zones</a></p>
                    <?php
                }

                /**
                 * Initialize CSS stylesheet for use with plugin
                 */
                public function register_plugin_styles() {
                    wp_register_style('be-table-rate-shipping', plugins_url('assets/plugin.css', __FILE__));
                    wp_enqueue_style('be-table-rate-shipping');
                    wp_enqueue_script('jquery-ui-core');
                }

                /**
                 * Translate shipping titles for WPML
                 *
                 * @param array $rates Array of rates found for the package
                 * @param array $package The package array/object being shipped
                 * @return array of modified rates
                 */
                function wpml_translate_titles($rates, $package) {
                    if (!function_exists('icl_t'))
                        return $rates;

                    foreach ($rates as $key => $rate)
                        $rate->label = icl_t('be-table-ship', 'shipping-title-' . sanitize_title($rate->label), $rate->label);

                    return $rates;
                }

                /**
                 * Hide shipping rates when free shipping is available
                 *
                 * @param array $rates Array of rates found for the package
                 * @param array $package The package array/object being shipped
                 * @return array of modified rates
                 */
                function hide_shipping_when_free_is_available($rates, $package) {
                    global $woocommerce;

                    if ($this->hide_method == 'yes') {

// Only modify rates if free_shipping is present
                        if (isset($rates['free_shipping'])) {
// Remove all rates beginning with this method's prefix
                            foreach ($rates as $key => $value) {
                                if (strpos($key, 'table_rate_shipping') === 0)
                                    unset($rates[$key]);
                            }
                        }
                    }

                    return $rates;
                }

            }

            /**
             * add_cart_rate_method function.
             *
             * @package		WooCommerce/Classes/Shipping
             * @access public
             * @param array $methods
             * @return array
             */
            function add_table_rate_method($methods) {
                $methods[] = 'BE_Table_Rate_Shipping';
                return $methods;
            }

            add_filter('woocommerce_shipping_methods', 'add_table_rate_method');

            /**
             * ensure zone table is created and being used
             */
//register_activation_hook( __FILE__, array( 'BE_Table_Rate_Shipping', 'activate' ) );
//add_action( 'admin_init', array( 'BE_Table_Rate_Shipping', 'update' ) );
            add_action('admin_notices', array('BE_Table_Rate_Shipping', 'be_zone_update_notice'));
        }
    }
}

/**
 * Modify links on plugin listing page (Left, Network Included)
 *
 * @access public
 * @return void
 */
function be_table_shipping_wc_action_links($links) {
    return array_merge(
            array(
        'settings' => '<a href="' . get_admin_url() . 'admin.php?page=wc-settings&tab=shipping&section=BE_Table_Rate_Shipping">' . __('Settings', 'be-table-ship') . '</a>',
        'register' => '<a href="' . get_admin_url() . 'admin.php?page=be-manage-plugins">' . __('Registration', 'be-table-ship') . '</a>',
            ), $links
    );
}

add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'be_table_shipping_wc_action_links');

function be_table_shipping_wc_network_action_links($links) {
    return array_merge(
            array(
        'register' => '<a href="' . get_admin_url() . 'admin.php?page=be-manage-plugins">' . __('Registration', 'be-table-ship') . '</a>',
            ), $links
    );
}

add_filter('network_admin_plugin_action_links_' . plugin_basename(__FILE__), 'be_table_shipping_wc_network_action_links');

/**
 * Modify links on plugin listing page (Right)
 *
 * @access public
 * @return array
 */
function be_table_shipping_wc_plugin_meta($links, $file) {

    if ($file == plugin_basename(__FILE__)) {

// Check if plugin already has a 'View details' link
        $index = 'details';
        foreach ($links as $key => $value)
            if (strstr($value, 'View details'))
                $index = $key;

        $row_meta = array(
            $index => '<a href="' . network_admin_url('plugin-install.php?tab=plugin-information&plugin=woocommerce-table-rate-shipping&TB_iframe=true&width=600&height=550') . '" class="thickbox">' . __('View details', 'be-table-ship') . '</a>',
            'docs' => '<a href="http://bolderelements.net/docs/woocommerce-table-rate-shipping/">' . __('Docs', 'be-table-ship') . '</a>',
            'support' => '<a href="http://bolderelements.net/support/" target="_blank">' . __('Support', 'be-table-ship') . '</a>'
        );
        return ( $links + $row_meta );
    }
    return (array) $links;
}

add_filter('plugin_row_meta', 'be_table_shipping_wc_plugin_meta', 10, 2);


/**
 * Initialise Auto Update Features
 *
 * @access public
 * @return void
 */
add_action('init', 'Updater_WooTableRateShipping');

function Updater_WooTableRateShipping() {
    include_once( 'upgrader/class-be-config.php' );

    if (class_exists('BolderElements_Plugin_Updater'))
        new BolderElements_Plugin_Updater(__FILE__, BE_WooTableShipping_VERSION, '3796656', 'woocommerce-table-rate-shipping', 'WooCommerce Table Rate Shipping');
}
?>