-2

I found the following code to put the custom field in the linked products woocommerce function. https://stackoverflow.com/a/45620800/9681621 Can someone please provide me the code to show the selected products in the cart page?

Anuj Gupta
  • 65
  • 1
  • 8
Aung Khant
  • 29
  • 6
  • SO is for questions, and people aren't really willing just to do your job for you, especially since it seems you haven't done anything yourself. – James Z Feb 01 '19 at 11:40

1 Answers1

0

you should look some woocommerce documentation. https://docs.woocommerce.com/documentation/plugins/woocommerce/woocommerce-codex/theming/

you can follow these steps.

the woocommerce>templates>cart>cart.php is the cart page. you will find something like this.

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], 
$cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', 
$cart_item['product_id'], $cart_item, $cart_item_key );

loop display the products you added to the cart.Variable $product_id of the loop have the id of each product you added the the cart. now you can insert your custom field code here

echo get_post_meta( $product_id, 'my-field-slug', true );

it will look something like this.

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], 
$cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', 
$cart_item['product_id'], $cart_item, $cart_item_key );

// Display Custom Field Value
// my-field-slug should be change according to your slug
echo get_post_meta( $product_id, 'my-field-slug', true );
}
Anuj Gupta
  • 65
  • 1
  • 8