doutou7961 2019-04-18 12:30
浏览 69
已采纳

自定义结帐字段组中的附加单选按钮,用于更新Woocommerce中的费用

This question is related "Update fee dynamically based on radio buttons in Woocommerce checkout" answer, which works nicely.

Actually there is 2 radio buttons in the field group and I would like to add an additional one (so 3 radio buttons at all).

My question is I would like to add an additional radio button, but I am just not seeing/understanding where to add an additional option.

I have added an extra field in the 'options' array: ...........................................................................

'options' => array(
            'bag' => __('In a bag '.wc_price(3.00), $domain),
            'box' => __('In a gift box '.wc_price(9.00), $domain),
            'speedboat' => __('In a speedboat '.wc_price(20.00), $domain),

...........................................................................

However I am confused by this section:

...........................................................................

$packing_fee = WC()->session->get( 'chosen_packing' ); // Dynamic packing fee
    $fee = $packing_fee == 'box' ? 9.00 : 3.00;
    $cart->add_fee( __( 'Packaging fee', 'woocommerce' ), $fee );

........................................................................

I tried adding:-

$packing_fee = WC()->session->get( 'chosen_packing' ); // Dynamic packing fee
    $fee = $packing_fee == 'box' ? 9.00 : 3.00 :;
    $cart->add_fee( __( 'Packaging fee', 'woocommerce' ), $fee );

but I doubt I am doing it right.

Can somebody please guide me on how to do this please?

  • 写回答

1条回答 默认 最新

  • douyu5775 2019-04-18 13:19
    关注

    For your 3 radio button (3 options in the array) like:

    'options' => array(
        'bag' => __('In a bag '.wc_price(3.00), $domain),
        'box' => __('In a gift box '.wc_price(9.00), $domain),
        'speedboat' => __('In a speedboat '.wc_price(20.00), $domain),
    ),
    

    You will have to replace this original code:

    $packing_fee = WC()->session->get( 'chosen_packing' ); // Dynamic packing fee
    $fee = $packing_fee == 'box' ? 9.00 : 3.00;
    $cart->add_fee( __( 'Packaging fee', 'woocommerce' ), $fee );
    

    with the following (for a third option):

    $packing_fee = WC()->session->get( 'chosen_packing' ); // Dynamic packing fee
    
    if( $packing_fee === 'box' )
       $fee = 9.00; 
    else if( $packing_fee === 'speedboat' ) 
       $fee = 20.00;
    else
       $fee = 3.00;
    
    $cart->add_fee( __( 'Packaging fee', 'woocommerce' ), $fee );
    

    or this similar in a more compact way:

    $packing_fee = WC()->session->get( 'chosen_packing' ); // Dynamic packing fee
    $fee = $packing_fee === 'bag' ? 3.00 : ( $packing_fee === 'box' ? 9.00 : 20.00 );
    $cart->add_fee( __( 'Packaging fee', 'woocommerce' ), $fee );
    

    Both should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料