Created
July 7, 2022 16:01
-
-
Save MrJoshFisher/bd09e3831a55f42c59517e9d423cede5 to your computer and use it in GitHub Desktop.
[Add Cart / Checkout Field Woocommerce]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form class=" woocommerce-cart-form"> | |
... | |
<input type="hidden" id="cart_extra_option" name="cart_extra_option" value="value" > | |
<input type="hidden" id="cart_total_price" name="cart_total_price" value="value"> | |
... | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MAKE FIELDS | |
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field'); | |
function wps_add_select_checkout_field($checkout) | |
{ | |
woocommerce_form_field('cart_extra_option', array( | |
'type' => 'text', | |
'class' => array( 'wps-drop' ), | |
'label' => __('cart_extra_option'), | |
'options' => $options, | |
), $checkout->get_value('cart_extra_option')); | |
woocommerce_form_field('cart_total_price', array( | |
'type' => 'text', | |
'class' => array( 'wps-drop' ), | |
'label' => __('cart_total_price'), | |
'options' => $options, | |
), $checkout->get_value('cart_total_price')); | |
} | |
//SAVE FIELDS | |
add_action('woocommerce_checkout_update_order_meta', 'bbloomer_save_new_checkout_field'); | |
function bbloomer_save_new_checkout_field($order_id) | |
{ | |
if ($_POST['cart_extra_option']) { | |
update_post_meta($order_id, '_cart_extra_option', esc_attr($_POST['cart_extra_option'])); | |
}# | |
if ($_POST['cart_total_price']) { | |
update_post_meta($order_id, '_cart_total_price', esc_attr($_POST['cart_total_price'])); | |
} | |
} | |
//SHOW FIELDS ON ADMIN | |
add_action('woocommerce_admin_order_data_after_billing_address', 'bbloomer_show_new_checkout_field_order', 10, 1); | |
function bbloomer_show_new_checkout_field_order($order) | |
{ | |
$order_id = $order->get_id(); | |
if (get_post_meta($order_id, '_cart_extra_option', true)) { | |
echo '<p><strong>cart_extra_option:</strong> ' . get_post_meta($order_id, '_cart_extra_option', true) . '</p>'; | |
} | |
if (get_post_meta($order_id, '_cart_total_price', true)) { | |
echo '<p><strong>cart_total_price:</strong> ' . get_post_meta($order_id, '_cart_total_price', true) . '</p>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment