将自定义分类中的术语元添加到购物车项目
Add Term Meta from Custom Taxonomy to Cart Item
我想要完成的是从附加到产品的自定义分类中添加术语元并将其显示在 cart/checkout/订单 details/email 通知上。我在这方面不是最擅长的,但我知道的足够多了。
我使用的是 'Perfect Brands WooCommerce' 添加了“pwb-brand”的插件。我还添加了我自己的术语,这样我们就可以根据品牌显示预计到达时间。我想要显示的内容如下所示:
/**
* 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 品牌的可用时间添加到每个产品项目。目前我的 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' 中提取元数据,并在 cart/checkout/order details/email 通知中为每个购物车项目显示它?
试试这个购物车
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')) . ')';
}
}
我想要完成的是从附加到产品的自定义分类中添加术语元并将其显示在 cart/checkout/订单 details/email 通知上。我在这方面不是最擅长的,但我知道的足够多了。
我使用的是 'Perfect Brands WooCommerce' 添加了“pwb-brand”的插件。我还添加了我自己的术语,这样我们就可以根据品牌显示预计到达时间。我想要显示的内容如下所示:
/**
* 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 品牌的可用时间添加到每个产品项目。目前我的 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' 中提取元数据,并在 cart/checkout/order details/email 通知中为每个购物车项目显示它?
试试这个购物车
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')) . ')';
}
}