Home > OS >  Add Term Meta from Custom Taxonomy to Cart Item (Woocommerce)
Add Term Meta from Custom Taxonomy to Cart Item (Woocommerce)

Time:10-12

I feel like this is a long shot but I'm at my wit's end. What I'm trying to accomplish is adding term meta from a custom taxonomy that is attached to a product and display it on the cart/checkout/ order details/email notification. I'm not the greatest at this but know enough to get by. What I'm using is the 'Perfect Brands Woocommerce' plugin that adds in the "pwb-brand". I've also added in my own terms so we can show ETA leads times based on the brand. What I'm trying to have show up is like the following:

/**
   *  Add ETA for builder for
   *  Better user experience
   */

  public function product_eta($eta) {

  global $product;
  $brands = wp_get_object_terms($product->get_id(), 'pwb-brand');

  ob_start();
?>

    <?php 
      if ( is_product() ) {
      foreach ($brands as $brand) : 
    ?>

      <?php
        $brand_eta_txt  = get_option('wc_pwb_admin_tab_brand_single_product_eta_txt');
        $eta_start      = get_term_meta($brand->term_id, 'pwb_brand_eta_start', true);
        $eta_end        = get_term_meta($brand->term_id, 'pwb_brand_eta_end', true);
        $eta_txt        = get_term_meta($brand->term_id, 'pwb_brand_eta_txt', true);
      ?>

      <div id="pwb-eta-content">
        <?php 
            echo '<span class="avail"><strong>Availability: </strong> Estimated '.$eta_start.'-'.$eta_end.' weeks (' . date('m/d', strtotime(' '.$eta_start.' weeks')). '-' . date('m/d', strtotime(' '.$eta_end.' weeks')) . ')');
        ?>
      </div>

    <?php 
      endforeach; 
      }
    ?>

<?php
    echo ob_get_clean();
    return $eta;
  }

This is a stripped down version of what we display on our product pages (https://25e62027b7.nxcli.net/shop/dining/extend-a-bench/brooklyn-extend-a-bench/). I'm trying to add the availability time that I added to the pwb-brand to each product item. I have the following in our functions.php at the moment:

add_filter( 'woocommerce_cart_item_name', function( $link_text, $cart_item, $cart_item_key ) {

  $_product = $cart_item['data'];

  $brands = implode(', ', wp_get_post_terms( $_product->get_id(), 'pwb-brand', ['fields' => 'names'] ) );
  $link_text = '<div>' . __( 'Brand', 'perfect-woocommerce-brands' ) . ': ' . $brands . '</div>';

  $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );

  if ( ! $product_permalink ) {
    $link_text .= $_product->get_name();
  } else {
    $link_text .= sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() );
  }

  return $link_text;

}, 10, 3 );
// Display order items product brands (Orders on front end and emails)
add_action( 'woocommerce_order_item_meta_end', 'display_custom_data_in_emails', 10, 4 );
function display_custom_data_in_emails( $item_id, $item, $order, $bool ) {
    // Get the product brands for this item
    $terms = wp_get_post_terms( $item->get_product_id(), 'pwb-brand', array( 'fields' => 'names' ) );

    // Output a coma separated string of product brand names
    echo "<br><small>" . implode(', ', $terms) . "</small>";
}

// Display order items product brands in admin order edit pages
add_action( 'woocommerce_after_order_itemmeta', 'custom_admin_order_itemmeta', 15, 3 );
function custom_admin_order_itemmeta( $item_id, $item, $product ){
    //if( ! is_admin() ) return; // only backend

    // Target order "line items" only to avoid errors
    if( $item->is_type( 'line_item' ) ){
        // Get the product brands for this item
        $terms = wp_get_post_terms( $item->get_product_id(), 'pwb-brand', array( 'fields' => 'names' ) );

        // Output a coma separated string of product brands names
        echo "<br><small>" . implode(', ', $terms) . "</small>";
    }
}

I found these snippets while trying to figure this out and it's close to what I'm after however since it uses the "wp_get_post_terms" to pull I can not get anything past the name or id. Is there a way to pull the term meta from the 'pwb-brand' and have it displayed for each cart item in the cart/checkout/order details/email notification?

CodePudding user response:

Try this for cart

add_filter( 'woocommerce_cart_item_name', function( $link_text, $cart_item, $cart_item_key ) {

$brands = wp_get_object_terms($cart_item['product_id'], 'pwb-brand');

$term_id = $terms[0]->term_id;

$eta_start      = get_term_meta($term_id, 'pwb_brand_eta_start', true);
$eta_end        = get_term_meta($term_id, 'pwb_brand_eta_end', true);
$eta_txt        = get_term_meta($term_id, 'pwb_brand_eta_txt', true);

  if ( $eta_start != "" ){
        $link_text .= ' <br><span class="avail"><strong>Availability: </strong> Estimated '.$eta_start.'-'.$eta_end.' weeks (' . date('m/d', strtotime(' '.$eta_start.' weeks')). '-' . date('m/d', strtotime(' '.$eta_end.' weeks')) . ')';
}

  return $link_text;

}, 10, 3 );

And this for email

add_action( 'woocommerce_order_item_meta_end', 'display_custom_data_in_emails', 10, 4 );
function display_custom_data_in_emails( $item_id, $item, $order, $bool ) {
    // Get the product brands for this item
    $terms = wp_get_post_terms( $item->get_product_id(), 'pwb-brand', array( 'fields' => 'names' ) );
    
$brands = wp_get_object_terms($cart_item['product_id'], 'pwb-brand');

$term_id = $terms[0]->term_id;

$eta_start      = get_term_meta($term_id, 'pwb_brand_eta_start', true);
$eta_end        = get_term_meta($term_id, 'pwb_brand_eta_end', true);
$eta_txt        = get_term_meta($term_id, 'pwb_brand_eta_txt', true);
if ( $eta_start != "" ){
 
echo ' <br><span class="avail"><strong>Availability: </strong> Estimated '.$eta_start.'-'.$eta_end.' weeks (' . date('m/d', strtotime(' '.$eta_start.' weeks')). '-' . date('m/d', strtotime(' '.$eta_end.' weeks')) . ')';
}
}
  • Related