• Resolved katdidit

    (@katdidit)


    Short story is I have been trying to wrap my head around it, and honestly its been a bit much for my tiny little brain. I thank Claudio Sanches for clarifying some of the things that haven’t sunk in about both the new methods and the code I am trying to update – – the original function used to add the custom fields as meta to the order item with wc_add_order_item_meta and was hooked on woocommerce_add_order_item_meta (deprecated in 3.0). Now the code is hooked via the woocommerce_checkout_create_order_line_item and using “add_meta”

    ecard_order_meta_handler( $item_id, $values, $cart_item_key ) {
        if( isset( $values['ecard_greeting'] ) ) {
            add_meta( $item_id, 'ecard_greeting', $values['ecard_greeting'] );
        }  

    and that seems to work (or at least its not throwing any errors) however it is not saving the data to the order using the same save function as before which is using $cart_item_data with the hook woocommerce_add_cart_item_data:

    save_card_order_info( $cart_item_data, $product_id ) {
    			
    	$extraFields = array( 'ecard_greeting', 'etcetera');
    	 foreach($extraFields as $field) {		
    			if( isset( $_REQUEST[$field] ) ) {
    			$cart_item_data[ $field ] = $_REQUEST[$field];
    					$cart_item_data['unique_key'] = md5( microtime().rand() );
    				}
    				
    			}
    			
    return $cart_item_data;

    So what am I missing?

    • This topic was modified 7 years, 7 months ago by katdidit.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    https://github.com/woocommerce/woocommerce/blob/03004bdd494a6f4d0805b081c02b81e69e12f3ef/includes/class-wc-checkout.php#L371 gives you an $Item object which you can add meta data to using the crud.

    e.g. basic example:

    
    add_action( 'woocommerce_checkout_create_order_line_item', 'add_my_meta_to_line_item', 10, 4 );
    
    function add_my_meta_to_line_item( $item, $cart_item_key, $values, $order) {
      $item->add_meta_data( 'meta key', 'meta value', true );
    }
    
    Thread Starter katdidit

    (@katdidit)

    Same thing. Its showing in the checkout, and in the cart, but its not saving the values to the order itself?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    ^ that saves it to the DB so it would be shown in backend.

    Thread Starter katdidit

    (@katdidit)

    It does not seem to be reading the values of the cart key – If I leave off the isset and $values it returns a string. If I leave the isset and leave off the values it returns nothing. – Leaving me to believe I am not saving the data correctly to the cart in the first place for the new method to pick up on it even though it shows up on the cart and in the checkout.

    Thread Starter katdidit

    (@katdidit)

    @mikejolley and @claudiosanches Thank you guys so much for your help. I am happy to report that my problem has been solved by using add_meta_data and adding a foreach item to the statement

    foreach ( $item as $cart_item_key => $values ) {
        if( isset( $values['metakey'] ) ) {
            $item->add_meta_data('metakey', $values['metakey'], true );
        } 
    • This reply was modified 7 years, 7 months ago by katdidit.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Oh CRUD! Custom Meta to order from cart’ is closed to new replies.