我为我的产品设置了“交货时间”属性。我需要在单个产品页面的cart&checkout表中以及订单电子邮件中显示此内容。
我已经成功地在单个产品页面和购物车/订单表中显示了以下内容:
function wp_woo_cart_attributes( $cart_item, $cart_item_key ) {
$item_data = $cart_item_key['data'];
$attributes = $item_data->get_attributes();
if ( ! $attributes ) {
return $cart_item;
}
$out = $cart_item . '<br />';
foreach ( $attributes as $attribute ) {
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$product_id = $item_data->get_id();
$terms = wp_get_post_terms( $product_id, $name, 'all' );
if ( ! empty( $terms ) ) {
if ( ! is_wp_error( $terms ) ) {
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ( $tax_object->labels->singular_name ) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
$label_prefix = 'Product ';
if ( 0 === strpos( $tax_label, $label_prefix ) ) {
$tax_label = substr( $tax_label, strlen( $label_prefix ) );
}
}
$out .= $tax_label . ': ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
array_push( $tax_terms, $single_term );
}
$out .= implode(', ', $tax_terms). '<br />';
}
}
} else {
// not a taxonomy
$out .= $name . ': ';
$out .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
}
}
echo $out;
}
add_filter( 'woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2 );
//Single Product
function product_attribute_delivery(){
global $product;
$taxonomy = 'pa_delivery';
$value = $product->get_attribute( $taxonomy );
if ( $value ) {
$label = get_taxonomy( $taxonomy )->labels->singular_name;
echo '<p>' . $label . ': ' . $value . '</p>';
}}
add_action( 'woocommerce_single_product_summary', 'product_attribute_dimensions', 25 );
如何将其添加到订单电子邮件中,甚至还有一种解决方案,仅当该商品有库存时才将其显示在单个产品页面上?
答案 0 :(得分:1)
好吧,让我们首先简化上面的代码,并保持其清洁。
如果只希望在“单个产品”页面中仅显示属性,则可以删除此
//for Cart items and mini cart
add_filter('woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2);
function wp_woo_cart_attributes($cart_item, $cart_item_key)
{
$productId = $cart_item_key['product_id'];
$product = wc_get_product($productId);
$taxonomy = 'pa_delivery';
$value = $product->get_attribute($taxonomy);
if ($value) {
$label = get_taxonomy($taxonomy)->labels->singular_name;
$cart_item .= "<br><p> $label : $value</p>";
}
return $cart_item;
}
单个产品
add_action('woocommerce_single_product_summary', 'product_attribute_delivery', 25);
function product_attribute_delivery()
{
global $product;
$taxonomy = 'pa_delivery';
$value = $product->get_attribute($taxonomy);
if ($value) {
$label = get_taxonomy($taxonomy)->labels->singular_name;
echo '<p>' . $label . ': ' . $value . '</p>';
}}
在电子邮件订单项中显示特定的分类法
add_action('woocommerce_order_item_meta_end', 'custom_item_meta', 10, 4);
function custom_item_meta($item_id, $item, $order, $plain_text)
{
$productId = $item->get_product_id();
$product = wc_get_product($productId);
$taxonomy = 'pa_delivery';
$value = $product->get_attribute($taxonomy);
if ($value) {
$label = get_taxonomy($taxonomy)->labels->singular_name;
echo "<br><p> $label : $value</p>";
}
}
上面的代码已经过测试并且可以正常工作