0
votes

In WooCommerce and I have added a custom field "description" for each product. I was able to find a way to show both, the label name and the value:

add_filter( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
function save_days_field( $cart_item_data, $product_id ) {
    $special_item = get_post_meta( $product_id , 'description',true );

    if(!empty($special_item)) {
        $cart_item_data[ 'description' ] = $special_item;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'description', $special_item );
    }
    return $cart_item_data;
}

// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data','rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
    if( isset( $cart_item['description'] ) ) {
        $cart_item_data[] = array( "name" => __( "Description", "woocommerce" ), "value" => $cart_item['description'] );
    }
    return $cart_item_data;
}

Now I need to display ONLY the value (not the label name "Description") of this custom field in the cart and checkout table. I need to display with <small>, like the attribute I am displaying with this code:

add_filter('woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2);
function wp_woo_cart_attributes($cart_item, $cart_item_key){
    $productId = $cart_item_key['product_id'];
    $product = wc_get_product($productId);
    $taxonomy = 'pa_color';
    $value = $product->get_attribute($taxonomy);

    if ($value) {
        $label = get_taxonomy($taxonomy)->labels->singular_name;
        $cart_item .= "<small>$value</small>";
    }
    return $cart_item;
}

How can I make it for this custom field, displaying the value only?

1
If your asking how to do that within the plugin.it is best you look at the documentation for it. Most plugins create a standard "widget" with minimal/base/standard functionality. If people want to expand or customize the widget, you need to edit the php code of the widget. I wouldnt do this without knowing what you are doing providing some code would helpJujubes

1 Answers

5
votes

You don't need to include a product custom field as custom cart item data, as it's directly accessible from the product object (or the product ID).

Note: On a cart item variable $cart_item, the WC_Product Object is included and available using $cart_item['data'].

Try the following to add a custom field after the item name in cart and checkout pages:

// Display in cart and checkout pages
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
    $product = $cart_item['data']; // Get the WC_Product Object

    if ( $value = $product->get_meta('description') ) {
        $product_name .= '<small>'.$value.'</small>';
    }
    return $product_name;
}

To display it on orders and email notifications use:

// Display in orders and email notifications
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 10, 2 );
function customizing_order_item_name( $product_name, $item ) {
    $product = $item->get_product(); // Get the WC_Product Object

    if ( $value = $product->get_meta('description') ) {
        $product_name .= '<small>'.$value.'</small>';
    }
    return $product_name;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.