仅当单击“ option1”费用时,如何在结帐评论中显示“自定义字段”

时间:2019-07-17 07:13:54

标签: php wordpress

我有一个用于woocommerce结帐的自定义费用的自定义代码。它的包装额外的保险。但是,即使我单击“无保险”单选按钮,该“保险”字段也始终在“结帐评论”中可见-默认情况下选中了“无保险”。我将如何进行这项工作,以便在选中“无保险”单选按钮时,在“结帐评论”上没有保险文字,只有当我单击“我想要保险”时,保险文字及其价格才会显示? >

no_option定义没有保险 option_1定义了此保险和费用

// Part 1 
// Display Radio Buttons
// Uses woocommerce_form_field()

add_action( 'woocommerce_after_order_notes','bbloomer_checkout_radio_choice' );

function bbloomer_checkout_radio_choice() {

   $chosen = WC()->session->get('radio_chosen');
   $chosen = empty( $chosen ) ? WC()->checkout->get_value('radio_choice') : $chosen;
   $chosen = empty( $chosen ) ? 'no_option' : $chosen;

   $args = array(
   'type' => 'radio',
   'class' => array( 'form-row-wide' ),
   'options' => array(
      'no_option' => 'Brez zavarovanja paketa',
      'option_1' => 'Zavaruj svoj paket za samo 0,99€ (<a href="test"_blank">VEČ</a>)',
   ),
   'default' => $chosen
  );

   echo '<div id="checkout-radio">';
   echo '<img src="https://www.kupko.si/wp- 
   content/uploads/2019/07/shield.png" alt="Zavarovanje"> <h3>Zavaruj svoj paket</h3>';
   woocommerce_form_field( 'radio_choice', $args, $chosen );
   echo '</div>';

}

// Part 2 
// Add Fee and Calculate Total
// Based on session's "radio_chosen"

#2 Calculate New Total

add_action( 'woocommerce_cart_calculate_fees','bbloomer_checkout_radio_choice_fee', 20, 1 
);

function bbloomer_checkout_radio_choice_fee( $cart ) {

  if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

  $radio = WC()->session->get( 'radio_chosen' );

  if ( "option_1" == $radio ) {
   $fee = 0.99;
  } 

  $cart->add_fee( __('Zavarovanje paketa', 'woocommerce'), $fee );

}

0 个答案:

没有答案