Is it possible to update the total price on the checkout page in woocommerce from a custom field

I would like to know how I go about this. Let’s say I have some items in my checkout page in woocommerce. I want to apply a gift card to the order, I can’t use a gift card plugin because it is implemented already by a third party gift card service. Someone is building the connection for me to hit to connect to the third pary gift card service.

Now what I would like to do is on the checkout page, have a custom field where I can enter a gift card. with a submit button, that through I assume ajax run a php function that submits to the thrid party gift card service, with the amount of the total price and will return to me the amount it can cover.

Basically I want to know what would I hook into in woocomerce after this function is run to update the price in the checkout (cart), and I guess where would I hookinto to also add this custom field (or I guess custom form)

So far I have done this, what I am wondering is how do I update the total price from submitting a function before checking out?

add_action( 'woocommerce_after_checkout_billing_form', 'gift_card_redeem' );

// select
function gift_card_redeem( $checkout ){
 
    // you can also add some custom HTML here
 
    woocommerce_form_field( 'contactmethod', array(
        'type'          => 'text', // text, textarea, select, radio, checkbox, password, about custom validation a little later
        'required'  => false, // actually this parameter just adds "*" to the field
        'class'         => array('gift-card-redeem'), // array only, read more about classes and styling in the previous step
        'label'         => 'gift-card-redeem',
        'label_class'   => 'gift-card-redeem', // sometimes you need to customize labels, both string and arrays are supported
        ), $checkout->get_value( 'contactmethod' ) );
 
    // you can also add some custom HTML here
 
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Well, you can use a WooCommerce hook( woocommerce_before_calculate_totals )
I will give an example so that it may be easier to understand what you believe.
As far as you know , I think you problem will be fixed

    function calculate_embossing_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* Gift wrap price */
        $additionalPrice = 5;
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["embossing_fee"] ) ) {
                // Turn $value['data']->price in to $value['data']->get_price()
                $orgPrice = floatval( $value['data']->get_price() );
                $discPrice = $orgPrice + $additionalPrice;
                $value['data']->set_price($discPrice);
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_embossing_fee', 99 );

I will Suggest to you can’t use this woocommerce form field , because you can’t update price in checkout ,better than for you can add custom field in product page

/* Gift wrap price */ $additionalPrice = custom field value;

I will refer to product Custom field link


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x