Snippets Collections
// Add a checkbox to the terms and conditions section
add_action('woocommerce_checkout_terms_and_conditions', 'add_custom_checkout_checkbox', 20);
function add_custom_checkout_checkbox() {
    woocommerce_form_field('factor_need', array(
        'type'  => 'checkbox',
        'class' => array('woocommerce-form__input woocommerce-form__input-checkbox'),
        'label' => ('آیا مایل به دریافت فاکتور هستید؟', 'woocommerce'),
    ));
}

// Save the checkbox value in the order meta
add_action('woocommerce_checkout_update_order_meta', 'save_custom_checkout_checkbox');
function save_custom_checkout_checkbox($order_id) {
    if (isset($_POST['factor_need'])) {
        update_post_meta($order_id, 'factor_need', 'yes');
    } else {
        update_post_meta($order_id, 'factor_need', 'no');
    }
}

// Display the checkbox value in the admin order details
add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_checkout_checkbox_in_admin', 10, 1);
function display_custom_checkout_checkbox_in_admin($order) {
    $factor_need = get_post_meta($order->get_id(), 'factor_need', true);
    if ($factor_need === 'yes') {
        echo '<p><strong>' . ('نیاز به فاکتور:', 'woocommerce') . '</strong> ' . __('هست', 'woocommerce') . '</p>';
    } 
}
star

Mon Aug 05 2024 15:07:42 GMT+0000 (Coordinated Universal Time)

#php #wordpress #checkout #factor #checkbox

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension