按类型和升序费用排序WooCommerce运输选项

huangapple go评论122阅读模式
英文:

Sort WooCommerce shipping options by type and by ascending cost

问题

根据 https://stackoverflow.com/questions/62570577/sort-woocommerce-shipping-options-by-ascending-cost-with-free-shipping-at-the-en/62579857#62579857 答案线程,我尝试按类型和升序成本对所有运输选项进行排序:

  • 首先是免费运输选项,
  • 然后是固定费率选项,
  • 然后是升序成本的付费运输选项,
  • 最后是本地自取选项,因为很少有客户使用它。

我无法使其工作。

任何帮助都将不胜感激。

英文:

Following https://stackoverflow.com/questions/62570577/sort-woocommerce-shipping-options-by-ascending-cost-with-free-shipping-at-the-en/62579857#62579857 answer thread, I have tried to sort all shipping options by type and by ascending cost:

  • Free shipping options first,
  • followed by Flat rate options,
  • then Paid shipping options by ascending cost
  • and last Local pickup option(s) as very few customers use it.

I haven't been able to make it work.

Any help is appreciated.

答案1

得分: 1

以下是根据此线程编写的代码,将按类型和升序成本排序的运输费率:

add_filter( 'woocommerce_package_rates', 'sort_shipping_method_by_type_and_by_asc_cost', 10, 2 );
function sort_shipping_method_by_type_and_by_asc_cost( $rates, $package ) {
    if ( empty( $rates ) || ! is_array( $rates ) ) return;

    // 根据成本对运输方式进行排序
    uasort( $rates, function ( $a, $b ) {
        if ( $a == $b ) return 0;
        return ( $a->cost < $b->cost ) ? -1 : 1;
    } );

    $free = $flat = $local = $other = []; // 初始化

    // 循环遍历运输费率
    foreach ( $rates as $rate_key => $rate ) {
        // 对于“免费运输”方法
        if ( 'free_shipping' === $rate->method_id ) {
            $free[$rate_key] = $rate;
            unset($rates[$rate_key]);
        } 
        // 对于“固定费率”
        elseif ( 'flat_rate' === $rate->method_id ) {
            $flat[$rate_key] = $rate;
            unset($rates[$rate_key]);
        }
        // 对于“本地自取”
        elseif ( 'local_pickup' === $rate->method_id ) {
            $local[$rate_key] = $rate;
            unset($rates[$rate_key]);
        } 
        // 其他运输方式
        else {
            $other[$rate_key] = $rate;
            unset($rates[$rate_key]);
        }
    }
    // 合并分开的费率
    return array_merge($free, $flat, $other, $local);
}

此代码应放在您的子主题的functions.php文件中(或者放在插件中)。已测试并可用。

重要提示: 您需要通过清空购物车来刷新运输方式缓存。

英文:

The following, based on this thread, will sort shipping rates by type and by ascending cost:

add_filter( &#39;woocommerce_package_rates&#39; , &#39;sort_shipping_method_by_type_and_by_asc_cost&#39;, 10, 2 );
function sort_shipping_method_by_type_and_by_asc_cost( $rates, $package ) {
    if ( empty( $rates ) || ! is_array( $rates ) ) return;

    // Sort shipping methods based on cost
    uasort( $rates, function ( $a, $b ) {
        if ( $a == $b ) return 0;
        return ( $a-&gt;cost &lt; $b-&gt;cost ) ? -1 : 1;
    } );

    $free = $flat = $local = $other = []; // Initializing

    // Loop through shipping rates
    foreach ( $rates as $rate_key =&gt; $rate ) {
        // For &quot;Free shipping&quot; methods
        if ( &#39;free_shipping&#39; === $rate-&gt;method_id ) {
            $free[$rate_key] = $rate;
            unset($rates[$rate_key]);
        } 
        // For &quot;Flat rate&quot;
        elseif ( &#39;flat_rate&#39; === $rate-&gt;method_id ) {
            $flat[$rate_key] = $rate;
            unset($rates[$rate_key]);
        }
        // For &quot;Local pickup&quot;
        elseif ( &#39;local_pickup&#39; === $rate-&gt;method_id ) {
            $local[$rate_key] = $rate;
            unset($rates[$rate_key]);
        } 
        // Other shipping methods
        else {
            $other[$rate_key] = $rate;
            unset($rates[$rate_key]);
        }
    }
    // Merge separated rates
    return array_merge($free, $flat, $other, $local);
}

Code goes in functions.php file of your child theme (or in a plugin). Tested and work.

> Inmportant: You need to refresh shipping methods cache, by emptying your cart.

huangapple
  • 本文由 发表于 2023年8月10日 23:00:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定