最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览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
 
}

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
 
}
Share Improve this question edited Oct 21, 2020 at 19:15 Anders Kitson asked Oct 21, 2020 at 17:23 Anders KitsonAnders Kitson 1571 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论