ajax - WC()->cart->get_cart(); giving blank array in functions.php -
so have added custom price functionality based on checkbox on product single page. user has been asked add gift tag , input text message saving using item meta. here's code :
function save_gift_wrap_fee( $cart_item_data, $product_id ) { if( isset( $_post['option_gift_wrap'] ) && $_post['option_gift_wrap'] === 'yes' ) { $cart_item_data[ "gift_wrap_fee" ] = "yes"; $cart_item_data[ "gift_wrap_text" ] = $_post['option_gift_wrap_text']; } return $cart_item_data; } add_filter( 'woocommerce_add_cart_item_data', 'save_gift_wrap_fee', 99, 2 ); function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) { $meta_items = array(); /* woo 2.4.2 updates */ if( !empty( $cart_data ) ) { $meta_items = $cart_data; } if( isset( $cart_item["gift_wrap_fee"] ) ) { $meta_items[] = array( "name" => "gift tag message", "value" => $cart_item['gift_wrap_text'] ); } return $meta_items; } add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 99, 2 ); function gift_wrap_order_meta_handler( $item_id, $values, $cart_item_key ) { if( isset( $values["gift_wrap_fee"] ) ) { //wc_add_order_item_meta( $item_id, "gift wrap", 'yes' ); wc_add_order_item_meta( $item_id, "gift tag message", $values["gift_wrap_text"] ); } } add_action( 'woocommerce_add_order_item_meta', 'gift_wrap_order_meta_handler', 99, 3 );
it's fine till here. now, want give user facility let them change gift message on cart page. doing using custom ajax function. don't know, doing wrong. made ajax call tried cart in functions.php using $items = wc()->cart->get_cart();
it's giving me blank array. tried $items = wc()->cart->get_cart_from_session();
no luck.
ajax : simple dd.variation
click function , here's functions.php code
add_action( 'wp_ajax_change_msg_text', 'change_msg_text' ); add_action( 'wp_ajax_nopriv_change_msg_text', 'change_msg_text' ); function change_msg_text(){ global $woocommerce; $itm = wc()->cart->get_cart(); print_r($itm); die(); }
in short: want take edited gift message user on cart page , want update text using wc_update_order_item_meta();
via ajax call. may wrong function names want change & update cart meta data on cart page.
any suggestion, please?
thanks..
Comments
Post a Comment