Home > Blockchain >  get_post_meta is not displaying on Woocmmerce Cart page
get_post_meta is not displaying on Woocmmerce Cart page

Time:06-07

I am creating another list of products on cart page but I am wondering why this code below is not displaying the custom field on woocommerce cart page. I hope someone can help fix the code below to make it work.

function display_fork_lift_msg_single_product() {

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];         
        
        if(!empty($product) && is_cart()){

            $fork_lift = get_post_meta( $product->ID, '_shipping_notes', true );
                                   
            echo $product->get_image() . '<br>' . $fork_lift;

        }
    }
   
}
add_action('woocommerce_before_cart_table', 'display_fork_lift_msg_single_product', 10);

Thank you very much.

CodePudding user response:

You have 1 mistake here regarding getting the product ID, use get_id() to return the ID.

function display_fork_lift_msg_single_product() {
    // I moved this check here instead making it inside the foreach loop
    if (is_cart()) {
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product = $cart_item['data'];

            if(!empty($product)){
                $fork_lift = get_post_meta($product->get_id(), '_shipping_notes', true);

                echo $product->get_image() . '<br>' . $fork_lift;
            }
        }
    }
}
add_action('woocommerce_before_cart_table', 'display_fork_lift_msg_single_product', 10);

And you should be good now :)

  • Related