Reputation: 113
I have a 'Custom field' on my product page that I want to add above the title of a product in a Woocommerce cart page.
This is the custom field data:
I got it to work on a single product card with this PHP code
add_action( 'woocommerce_after_shop_loop_item_title', 'product_card_beschrijving', 2);
function product_card_beschrijving(){
global $product;
$beschrijving = get_post_meta( $product->id, 'product_card_beschrijving', true );
if ( ! empty( $beschrijving ) ) {
echo '<span class="beschrijving">' . $beschrijving . '</span>';
}
}
I've tried this same code for the cart page but altered it a little as such but the meta data of the custom field isn't being loaded. I've used $get_item_data
and more but can't get it to work
<p class="cart-subtitel">
<?php $beschrijving = get_post_meta( $product->id, 'product_card_beschrijving', true ); echo '<span class="beschrijving">' . $beschrijving . '</span>'?>
</p>
Upvotes: 2
Views: 11106
Reputation: 26319
This is adapted from my Subtitle plugin/WooCommerce bridge plugin
/**
* Cart product title.
*
* @param string $title - The product title.
* @param array $cart_item - The array of cart item product data.
* @return string
*/
function kia_add_subtitle_to_cart_product( $title, $cart_item ){
$_product = $cart_item['data'];
$meta = $_product->get_meta( 'product_card_beschrijving', true );
if( $meta ) {
$title .= '<span class="meta">' . $meta . '</span>';
}
return $title;
}
add_filter( 'woocommerce_cart_item_name', 'kia_add_subtitle_to_cart_product', 10, 2 );
Upvotes: 6