为了账号安全,请及时绑定邮箱和手机立即绑定

增加 WooCommerce 购物车中每个类别计数的运输成本

增加 WooCommerce 购物车中每个类别计数的运输成本

PHP
潇潇雨雨 2023-09-22 17:33:50
我正在尝试根据购物车中的类别数量(计数)来增加运费。到目前为止我有这个:add_filter( 'woocommerce_package_rates', 'vh_wc_shipping_costs', 20 );function vh_wc_shipping_costs( $rates ) {    $increase_cost = 50;    $cat_ids       = array();    $count         = 0;    foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {        $cat_ids = array_merge(            $cat_ids,            (array) $cart_item['data']->get_category_ids()        );        $count   = count( $cat_ids );    }    $i=0;    if ( $count >= 1 ) {        foreach ( $rates as $rate_key => $rate ) {            $i++; $i <= $count;            // Excluding free shipping methods            if ( 'free_shipping' !== $rate->method_id ) {                // Get old cost                $old_cost = $rates[ $rate_key ]->cost;                // Set rate cost                $rates[ $rate_key ]->cost = $old_cost + $increase_cost;            }        }    }    return $rates;}但由于某种原因,它不会增加每个额外类别的运输成本。另外,我想补充一下额外费用。任何建议表示赞赏。
查看完整描述

1 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞


以下代码将为购物车中找到的每个附加产品类别添加额外的运费(因此不适用于第一个):


add_filter( 'woocommerce_package_rates', 'filter_shipping_rates_costs', 10, 2 );

function filter_shipping_rates_costs( $rates, $package ) {

    $step_cost = 50;

    $term_ids  = array();


    // Loop through cart items for the current shipping package

    foreach( $package['contents'] as $cart_item ){

        $term_ids = array_merge(

            $term_ids,

            (array) $cart_item['data']->get_category_ids()

        );

    }


    $terms_count = count( $term_ids );


    // Loop through shipping rates

    foreach ( $rates as $rate_key => $rate ) {

        // Excluding free shipping methods

        if ( 'free_shipping' !== $rate->method_id && $terms_count > 1 ) {

            // Set rate cost

            $rates[$rate_key]->cost = $rate->cost + ($step_cost * ($terms_count - 1));

        }

    }


    return $rates;

}

现在,如果您想为购物车中找到的每个类别添加额外的运费,请使用以下命令:


add_filter( 'woocommerce_package_rates', 'filter_shipping_rates_costs', 10, 2 );

function filter_shipping_rates_costs( $rates, $package ) {

    $step_cost = 50;

    $term_ids  = array();


    // Loop through cart items for the current shipping package

    foreach( $package['contents'] as $cart_item ){

        $term_ids = array_merge(

            $term_ids,

            (array) $cart_item['data']->get_category_ids()

        );

    }


    $terms_count = count( $term_ids );


    // Loop through shipping rates

    foreach ( $rates as $rate_key => $rate ) {

        // Excluding free shipping methods

        if ( 'free_shipping' !== $rate->method_id && $terms_count > 0 ) {

            // Set rate cost

            $rates[$rate_key]->cost = $rate->cost + ($step_cost * $terms_count);

        }

    }


    return $rates;

}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

刷新运输缓存:

  1. 此代码已保存在您的functions.php 文件中。

  2. 在运输区域设置中,禁用/保存任何运输方式,然后启用返回/保存。

    你已经完成了,你可以测试它。


查看完整回答
反对 回复 2023-09-22
  • 1 回答
  • 0 关注
  • 51 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信