基于产品类别的运输方式的额外费用

ADv

我正在尝试根据产品类别向用户选择的运输方式添加额外费用(使用 $multiplier)。

所以,我需要检查购物车中是否有任何产品在 $product_category_id 上。如果是这样,那么我需要将选定的运输成本乘以 $multiplier 值。

此 $multiplier 应应用于按运输方式计算的总运输成本(而不是购物车中的每件商品)。

add_filter( 'woocommerce_package_rates','overwrite_shipping_cost', 100, 2 );
function overwrite_shipping_cost($rates, $package ) {
    global $woocommerce;

        if ( get_field( 'multiplier', 'option' ) ) :
            $multiplier = get_field( 'multiplier', 'option' );
        else :
            $multiplier = 1;
        endif;

        $product_category_id    = array( 86, 21, 47 ); 
        $product_cats_ids       = wc_get_product_term_ids( $product_id, 'product_cat' ); 
        $product_category_id_check = false;

        foreach( $products as $cart_item ){
            $product_id = $cart_item['product_id'];
        }

        foreach ( $product_category_id as $key => $value ) {
            if ( in_array( $value, $product_cats_ids ) ) { 
                $product_category_id_check = true;
            }
        }

        if ( ! is_admin() && $product_category_id_check ) {
                    // If Shiptor 1
                    if ( isset( $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal'] )) {
                        $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost = $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost * $multiplier;
                    } 

                    // If Shiptor 2
                    if ( isset( $rates['shiptor-shiptor:14:to-door_shiptor_courier'] )) {
                        $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost = $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost * $multiplier;
                    }

                    //If anothee shipping method
                    if ( isset( $rates['rpaefw_post_calc:16'] )) {
                        $rates['rpaefw_post_calc:16']->cost = $rates['rpaefw_post_calc:16']->cost * $multiplier;
                    }
        }


    return $rates;
}

执行代码后没有结果。我在代码中提供的运输方式没有应用 $multiplier。

茨梅霍坎

在活动主题的functions.php中添加以下代码片段 -

function add_extra_woocommerce_shipping_rate_cost( $cost, $method ) {
    /* let's assume its applicable for flat rate and local pickup
     * And instance id for flat rate = 12
     * And instance id for local pickup = 14 
     * 
     */
    if ( in_array( $method->get_instance_id(), array( 12, 14) ) && WC()->cart ) {
        if ( get_field( 'multiplier', 'option' ) ) :
            $multiplier = get_field( 'multiplier', 'option' );
        else :
            $multiplier = 1;
        endif;

        $product_category_id = array( 86, 21, 47 ); // applicable categories
        $product_category_id_check = false;
        $items = WC()->cart->get_cart();
        foreach( $items as $item => $values ) { 
            $product_cats_ids = wp_get_post_terms($values['data']->get_id(),'product_cat',array('fields'=>'ids'));
            foreach ( $product_cats_ids as $id ) {
                if( in_array( $id, $product_category_id ) ) 
                    $product_category_id_check = true;
            }
        }
        if( $product_category_id_check ){
            $cost = $cost * $multiplier;
        }
    }
    return $cost;
}
add_filter( 'woocommerce_shipping_rate_cost', 'add_extra_woocommerce_shipping_rate_cost', 99, 2 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

根据产品类别和运输方式添加费用WooCommerce

来自分类Dev

Woocommerce中基于状态和产品类别的累进购物车项目费用

来自分类Dev

Woocommerce-基于产品类别的电子邮件文本

来自分类Dev

基于特定产品类别的WooCommerce结帐消息

来自分类Dev

基于产品类别的WooCommerce特定优惠券折扣

来自分类Dev

基于WooCommerce中产品类别的自定义“缺货”文本

来自分类Dev

如何以编程方式根据产品所属的产品类别的图像设置产品的特色图像

来自分类Dev

通过子句获取WooCommerce产品类别的ID

来自分类Dev

根据产品类别自动设置WooCommerce产品运输类别

来自分类Dev

WooCommerce客户完成的订单电子邮件通知中基于产品类别的条件文本

来自分类Dev

库存产品类别

来自分类Dev

更改特定产品类别的WooCommerce产品数量设置

来自分类Dev

(1.9)获取所有产品类别的ID(在产品页面上)

来自分类Dev

Woocommerce:整体产品类别的最低数量,不可变化

来自分类Dev

如何使用WooCommerce更改每行产品类别的数量

来自分类Dev

隐藏购物车中产品类别的优惠券字段

来自分类Dev

为Woocommerce中已定义产品类别的特定项目计数设置零税种

来自分类Dev

WooCommerce中特定产品类别的最小购物车数量

来自分类Dev

删除产品类别的已定义组的WooCommerce付款网关

来自分类Dev

WooCommerce特定产品类别的渐进式数量折扣

来自分类Dev

WooCommerce禁止特定产品类别的ClearPay付款

来自分类Dev

Woocommerce:整体产品类别的最低数量,不可变化

来自分类Dev

在 WooCommerce 中输出特定产品类别的自定义短代码

来自分类Dev

获取 WooCommerce 中顶级产品类别的自定义数组

来自分类Dev

更改 Woocommerce 中特定产品类别的购物车项目价格

来自分类Dev

隐藏特定 WooCommerce 产品类别的属性下拉列表

来自分类Dev

以默认语言获取产品类别的术语数(使用 WPML)

来自分类Dev

比较不同产品类别的标准偏差

来自分类Dev

更改多个特定 woocommerce 产品类别的默认排序顺序

Related 相关文章

  1. 1

    根据产品类别和运输方式添加费用WooCommerce

  2. 2

    Woocommerce中基于状态和产品类别的累进购物车项目费用

  3. 3

    Woocommerce-基于产品类别的电子邮件文本

  4. 4

    基于特定产品类别的WooCommerce结帐消息

  5. 5

    基于产品类别的WooCommerce特定优惠券折扣

  6. 6

    基于WooCommerce中产品类别的自定义“缺货”文本

  7. 7

    如何以编程方式根据产品所属的产品类别的图像设置产品的特色图像

  8. 8

    通过子句获取WooCommerce产品类别的ID

  9. 9

    根据产品类别自动设置WooCommerce产品运输类别

  10. 10

    WooCommerce客户完成的订单电子邮件通知中基于产品类别的条件文本

  11. 11

    库存产品类别

  12. 12

    更改特定产品类别的WooCommerce产品数量设置

  13. 13

    (1.9)获取所有产品类别的ID(在产品页面上)

  14. 14

    Woocommerce:整体产品类别的最低数量,不可变化

  15. 15

    如何使用WooCommerce更改每行产品类别的数量

  16. 16

    隐藏购物车中产品类别的优惠券字段

  17. 17

    为Woocommerce中已定义产品类别的特定项目计数设置零税种

  18. 18

    WooCommerce中特定产品类别的最小购物车数量

  19. 19

    删除产品类别的已定义组的WooCommerce付款网关

  20. 20

    WooCommerce特定产品类别的渐进式数量折扣

  21. 21

    WooCommerce禁止特定产品类别的ClearPay付款

  22. 22

    Woocommerce:整体产品类别的最低数量,不可变化

  23. 23

    在 WooCommerce 中输出特定产品类别的自定义短代码

  24. 24

    获取 WooCommerce 中顶级产品类别的自定义数组

  25. 25

    更改 Woocommerce 中特定产品类别的购物车项目价格

  26. 26

    隐藏特定 WooCommerce 产品类别的属性下拉列表

  27. 27

    以默认语言获取产品类别的术语数(使用 WPML)

  28. 28

    比较不同产品类别的标准偏差

  29. 29

    更改多个特定 woocommerce 产品类别的默认排序顺序

热门标签

归档