• Resolved arconsulting

    (@arconsulting)


    Been using a plugin (not heard back from the author). And they add meta data to the cart in the form of a pick-up time and date. Problem is accessing that information. The product is not a variation, yet html tags show dl with class=””variation”. When I attempt to access the cart data using $woocommerce->cart->get_cart(), the pick-up time and date doesn’t not appear. Does anyone know how to access all the data in the cart incluing meta data? In this particular case I can’t use jQuery.

    If you wish to see how this works, select a location, time and then add to cart.

    Thank you for helping,
    Andrea

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @arconsulting,

    You are accessing it in the wrong way. Meta is saved in another table of woo-commerce. If you wish to access the meta field you show access you should be clear where it is saved. Their is two possibilities first it saves in order meta or in item meta.

    Here is the code to get it from both

    
    //from item meta
    foreach ($order->get_items() as $item_id => $item ) {
    
        echo $item->get_meta('meta_key');
    }
    //from order meta
    get_post_meta($order->ID,'meta_key',false);
    

    I hope this will help you. If stuck somewhere feel free to ask again

    Thank you

    Thread Starter arconsulting

    (@arconsulting)

    Very helpful, but can one access this while items are in the cart or only after the order is complete? I ask because I am trying to ensure the customer adds the item to the cart correctly before proceeding. Was going to make sure the metadata is in the cart.

    Hi @arconsulting,

    You can access the metadata while adding to the cart too, you just need to access it at the right hook.

    You can use this code

    function zwk_get_item_meta( $title, $cart_item ){
        $_product = $cart_item['data'];
        $meta     = $_product->get_meta( 'meta_key', true );
        return $title;
    }
    add_filter( 'woocommerce_cart_item_name', 'zwk_get_item_meta', 10, 2 );
    

    You can also access the metadata by accessing the session of woocommerce of on the cart page hook. Here’s a reference

    foreach( WC()->cart->get_cart() as $cart_item ) {
      var_dump( $cart_item );
      var_dump( WC()->cart->get_item_data( $cart_item ) );
    }

    You can directly use this function too.

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Plugin adds meta data to cart’ is closed to new replies.