• Resolved 1timothy412

    (@1timothy412)


    I know only a little bit of PHP and am stuck on this. I am customizing a WP site running Woocommerce. I have added some snippets that I found on the internet to add a check box to the checkout page. I need it to charge a fee when the check-box is marked. So basically, customers can add a card to their order for 0.75. They check the box if they want the card, and the fee needs to then be added to the cart. I have the boxes showing up, but can’t get the fee to add to the cart. Can someone help me with this?

    This is working to add the extra fields to checkout:

    add_action('woocommerce_after_order_notes', 'gift_wrapping_field');
    
    function gift_wrapping_field( $checkout )
    {
    
    woocommerce_form_field( 'gift_wrapping', array(
        'type'          => 'checkbox',
        'class'         => array('input-checkbox'),
        'label'         => __(' Include a card for the recipient of my gift purchase (additional $0.75) '),
        'required'        => false,
        ), $checkout->get_value( 'gift_wrapping' ));
    
    woocommerce_form_field( 'gift_card_name', array(
        'type'          => 'text',
        'class'         => array('gift-card-text'),
        'label'         => __(' Name of recipient: '),
        'required'        => false,
        ), $checkout->get_value( 'gift_card_name' ));
    }

    This is the code I am using to try to have the fee in the cart when the check box is selected, but its not working:

    function woo_add_cart_fee() {
    
      global $woocommerce;
    
      if( get_option('gift_wrapping') == 'value' ) { $woocommerce->cart->add_fee( __('Gift Card', 'woocommerce'), 0.75 );
    
    }
    add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

    Could someone kindly explain to me how to get this working properly? Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘PHP conditional field in Woocommerce checkout’ is closed to new replies.