3

I have a store where each product has different delivery date. I have this date stored in a custom field called shpng using ACF. What I'm trying to achieve is to display appropriate field next to each product in cart.

What I tried so far is that I tested approximately 10 solutions listed on SOF (example) and on the other sites, but none of them seems to work.

I know how to display this data, I just don't know how to display it for each product below product title.

I'm looking for someone to point me in the right direction.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Wed
  • 323
  • 1
  • 9
  • 20
  • How did you create the custom field; using some plugin or custom code? Can you share what solutions you have tried that didn't work for you? Also how do you display this data? Sharing that code will make it easier to help you. – Kashif Rafique Feb 13 '19 at 17:33
  • @Kashif Rafique, I used ACF plugin to generate fileds. I display that data (and other ACF data) on single product pages in two ways (first to display them as text on page and second one to generate svg graphic that represents energy consumption certificate according to EU laws). – Wed Feb 13 '19 at 17:52
  • Tried this and similar variations of this code: https://stackoverflow.com/q/39400033/9339501 – Wed Feb 13 '19 at 17:53
  • This link is exactly what I need. https://stackoverflow.com/questions/39400033/adding-a-notice-on-cart-and-checkout-page-based-on-item-custom-field-highest-val – Wed Feb 13 '19 at 18:31
  • What you described in your question is different than the question on the above link. See my answer for *displaying the custom field below the product title*. – Kashif Rafique Feb 13 '19 at 19:09
  • 1
    @KashifRafique thats exactly what I needed. Ill dig my way through from this point – Wed Feb 14 '19 at 07:30
  • Glad to know it works for you. – Kashif Rafique Feb 14 '19 at 12:20
  • @KashifRafique would the same logic for SKU be $cartsku = get_field ( ‘sku’, $cart_item[‘product_id’] ? – Wed Feb 14 '19 at 12:51
  • Are you saving SKU as ACF field or would you like to show WooCommerce SKU field value? – Kashif Rafique Feb 18 '19 at 19:28

1 Answers1

4

You can use woocommerce_after_cart_item_name hook for displaying the value of ACF custom field for each product below the product title.

//To display the ACF custom field 'shpng' below the product title on cart page
function lh_cart_item_sub_title( $cart_item ) {
    $shpng = get_field( 'shpng', $cart_item['product_id'] );
    echo "<div class='small custom-cart-shipping-date'>My Custom Shipping Date: $shpng.</div>";
}
add_action( 'woocommerce_after_cart_item_name', 'lh_cart_item_sub_title', 10, 1 );
Kashif Rafique
  • 1,273
  • 1
  • 12
  • 25