php - 将自定义分类中的术语元添加到购物车项目

标签 php wordpress woocommerce metadata cart

我想要完成的是从附加到产品的自定义分类中添加术语元,并将其显示在购物车/结账/订单详细信息/电子邮件通知上。我在这方面不是最擅长的,但知道的足够了。

我使用的是添加在“pwb-brand”中的“Perfect Brands WooCommerce”插件。我还添加了我自己的条款,以便我们可以根据品牌显示预计到达时间。我想要显示的内容如下:

/**
   *  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;
  }

这是我们在产品页面 ( https://25e62027b7.nxcli.net/shop/dining/extend-a-bench/brooklyn-extend-a-bench/ ) 上显示的内容的精简版本。我正在尝试将添加到 pwb-brand 的可用时间添加到每个产品项目。我现在的functions.php中有以下内容:

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>";
    }
}

我在试图弄清楚这一点时发现了这些片段,它与我所追求的很接近,但是因为它使用“wp_get_post_terms”来拉取,所以我无法获得任何超过名称或ID的内容。有没有办法从“pwb-brand”中提取术语元并将其显示在购物车/结帐/订单详细信息/电子邮件通知中的每个购物车项目中?

最佳答案

尝试一下购物车

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 );

这是电子邮件

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')) . ')';
}
}

关于php - 将自定义分类中的术语元添加到购物车项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69509453/

相关文章:

php - 根据下一个字段中的值选择或不选择字段

wordpress - 如何向 WordPress 主题添加评论

javascript - ExternalInterface addCallback 多次失败

在我的 URL 末尾添加/?name=someonesname 时出现 WordPress 404 错误

php - jQuery 到 PHP : Add a selector class to children elements if more than one

php - MYSQL 中的乘法、除法和加法

php - PostgreSQL 连接问题

php - 使用 Laravel 和希伯来语进行 URL 编码

javascript - 追加到具有相同类的所有 div 上

我的自定义模板的 WordPress URL 问题