lundi 23 août 2021

How do I change a custom woocommerce checkbox label programmatically?

I have this custom checkbox in the checkout page:

add_action('woocommerce_after_checkout_billing_form', 'checkout_shipping_form_packing_addition', 20);
function checkout_shipping_form_packing_addition()
{
    $domain = 'woocommerce';
    echo '<tr class="packing-select";>' . __('', $domain) . '<td>';

    $chosen   = WC()->session->get('chosen_packing');

    // Add a custom checkbox field
    $variable = <<<XYZ
<html>
<body>
 <p style="font-size:15px; margin-top: -38px; margin-left: 5px; height:auto; border:1px; border-style:solid; border-color:#FF0000; padding: 20px;"><b>Consegna rapida?</b></p>
</body>
</html>
XYZ;
    woocommerce_form_field('chosen_packing', array(
        'type'      => 'checkbox',
        'class'     => array('form-row-wide packing'),
        'label'     => __($variable, 'woocommerce'),
        'required'  => false,
    ), $chosen);

    echo '</td></tr>';
}

I want the label to change when it's checked, so I tried adding this

add_action('woocommerce_after_checkout_billing_form', 'my_custom_checkout_field_process2');
 
function my_custom_checkout_field_process2() {
    global $woocommerce;
 
    // Check if set, if its not set add an error.
    if (isset($_POST['chosen_packing'])){
        
        $_POST['chosen_packing']['label'] = 'no';
    
        }
}

But it doesn't work, and I'm not sure how to proceed exactly. I need the label to change whenever the checkbox is checked.




Aucun commentaire:

Enregistrer un commentaire