jeudi 18 février 2021

Check if a submitted checkbox was checked or not in WooCommerce

In WooCommerce, I am trying to check if submitted checkbox is checked or not in the code below:

add_filter('kco_wc_api_request_args', 'krokedil_add_required_checkbox');

function krokedil_add_required_checkbox( $create ) {
    $create['options']['additional_checkbox']['text'] = 'Prenumerera på nyhetsbrevet';
    $create['options']['additional_checkbox']['checked'] = true;
    $create['options']['additional_checkbox']['required'] = false;
    return $create;
}

add_action( 'kco_wc_confirm_klarna_order', 'my_add_kco_order_data', 10, 2 );
function my_add_kco_order_data($order_id, $klarna_order ) {
    if( $klarna_order['additional_checkbox']['checked'] ) {
        add_post_meta( $order_id, 'mailchimp_woocommerce_is_subscribed', 1 );
    } else {
        add_post_meta( $order_id, 'mailchimp_woocommerce_is_subscribed', 0 );
    }   
}

But I can't get it to work. Feels like I've tried all combinations.

Any suggestions?




Aucun commentaire:

Enregistrer un commentaire