0

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
 
}

1 Answer 1

2

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

3
  • I think you answered the majority of my question. Now how do I tell wordpress to run this add_action after I have submitted a request to my third party gift card servers. Do I need a seperate form on the page that runs this function after it's request is sent. Commented Oct 21, 2020 at 20:45
  • You Can use Webhooks also make it easier for third-party apps to integrate with WooCommerce.
    – HK89
    Commented Oct 21, 2020 at 21:01
  • I asked some more details in a different question to try and explain where I am at. wordpress.stackexchange.com/questions/376929/… Commented Oct 21, 2020 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.