【发布时间】:2021-11-29 05:10:18
【问题描述】:
我想要完成的是从附加到产品的自定义分类中添加术语元并将其显示在购物车/结帐/订单详细信息/电子邮件通知中。我在这方面不是最优秀的,但我知道的足够了。
我使用的是在“pwb-brand”中添加的“Perfect Brands WooCommerce”插件。我还添加了我自己的术语,以便我们可以根据品牌显示 ETA 的提前期。我想要显示的内容如下:
/**
* 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>";
}
}
我在试图弄清楚这一点时发现了这些 sn-ps,它与我所追求的很接近,但是因为它使用“wp_get_post_terms”来拉动,所以我无法得到任何超出名称或 ID 的信息。有没有办法从“pwb-brand”中提取术语 meta 并在购物车/结帐/订单详细信息/电子邮件通知中为每个购物车项目显示它?
【问题讨论】:
标签: php wordpress woocommerce metadata cart