PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 8, 2022

[FIXED] Add additional product information on Woocommerce checkout page

 January 08, 2022     checkout, php, product, woocommerce, wordpress     No comments   

Issue

In WooCommerce, I have a custom product field "Time" for which I want to output the field's value on checkout, specifically in the product details, under product name, to have it like this: Event time: (value from wcv_custom_product_field)

I've tried placing :

add_filter( 'woocommerce_get_item_data', 'wc_checkout_producttime', 10, 2 );

function wc_checkout_producttime( $other_data, $cart_item )
{
    $_product = $cart_item['data'];

    $other_data[] = array( 'name' =>  'wcv_custom_product_field', 'value' => $_product->get_wcv_custom_product_field() );
return $other_data;
}

But I'm getting ablank page on the checkout.

What I am doing wrong and how can I solve this problem?

Thanks.


Solution

Here is a custom function hooked in woocommerce_get_item_data filter hook that will display your product custom field in cart and checkout items:

add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );
function display_custom_product_field_data( $cart_data, $cart_item ) {

    // Define HERE your product custom field meta key  <==   <==   <==   <==   <==
    $meta_key = 'wcv_custom_product_field';

    $product_id = $cart_item['product_id'];

    $meta_value = get_post_meta( $product_id, $meta_key, true );

    if( !empty( $cart_data ) )
        $custom_items = $cart_data;

    if( !empty($meta_value) ) {
        $custom_items[] = array(
            'key'       => __('Event time', 'woocommerce'),
            'value'     => $meta_value,
            'display'   => $meta_value,
        );
    }
    return $custom_items;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.



Answered By - LoicTheAztec
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing