Last active
August 23, 2021 10:52
-
-
Save plugin-republic/579e258c0f608ff5598901983652b190 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
/** | |
* Update cart item notes | |
*/ | |
function prefix_update_cart_notes() { | |
// Do a nonce check | |
if( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'woocommerce-cart' ) ) { | |
wp_send_json( array( 'nonce_fail' => 1 ) ); | |
exit; | |
} | |
// Save the notes to the cart meta | |
$cart = WC()->cart->cart_contents; | |
$cart_id = $_POST['cart_id']; | |
$notes = $_POST['notes']; | |
$cart_item = $cart[$cart_id]; | |
$cart_item['notes'] = $notes; | |
WC()->cart->cart_contents[$cart_id] = $cart_item; | |
WC()->cart->set_session(); | |
wp_send_json( array( 'success' => 1 ) ); | |
exit; | |
} | |
add_action( 'wp_ajax_prefix_update_cart_notes', 'prefix_update_cart_notes' ); | |
add_action( 'wp_ajax_nopriv_prefix_update_cart_notes', 'prefix_update_cart_notes' ); | |
function prefix_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) { | |
foreach( $item as $cart_item_key=>$cart_item ) { | |
if( isset( $cart_item['notes'] ) ) { | |
$item->add_meta_data( 'notes', $cart_item['notes'], true ); | |
} | |
} | |
} | |
add_action( 'woocommerce_checkout_create_order_line_item', 'prefix_checkout_create_order_line_item', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks, very helpful!