在电子邮件模板中获取产品 ID:WooCommerce
Get the Product ID in email template: WooCommerce
我试图在发送给管理员的 woocommerce 邮件模板中添加一列,提及所购买产品的 ID。 (管理员/订单)
几次尝试失败后,我把它留给你,希望能找到更快的解决方案!提前谢谢你!
foreach ( $order->get_items() as $item_id => $item ) :
// $_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$_product = $item->get_product();
if ( version_compare( WC_VERSION, '3.1', '>=' ) ) {
$item_meta = new WC_Order_Item_Product( $item, $_product );
}else{
$item_meta = new WC_Order_Item_Meta( $item, $_product );
}
if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td style="text-align:center; width:100px; vertical-align:middle; border: 1px solid #eee; border-left:0; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style=""><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="30px" style="height:30px; width:auto; vertical-align:middle; margin-right: 10px;" /></div>', $item ); ?></td>
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Product name.
echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );
// UGS / SKU ID
if ( is_object( $_product ) && $_product->get_id()!="" ) {
echo ' (#' . $_product->get_id() . ')';
}
// SKU
if ( is_object( $_product ) && $_product->get_sku()!="" ) {
echo ' (#' . $_product->get_sku() . ')';
}
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
wc_display_item_meta(
$item,
array(
'label_before' => '<strong class="wc-item-meta-label" style="float: ' . esc_attr( $text_align ) . '; margin-' . esc_attr( $margin_side ) . ': .25em; clear: both">',
)
);
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
为我尝试这个工作
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
echo $product_id; // Here Your Product!
}
我试图在发送给管理员的 woocommerce 邮件模板中添加一列,提及所购买产品的 ID。 (管理员/订单) 几次尝试失败后,我把它留给你,希望能找到更快的解决方案!提前谢谢你!
foreach ( $order->get_items() as $item_id => $item ) :
// $_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$_product = $item->get_product();
if ( version_compare( WC_VERSION, '3.1', '>=' ) ) {
$item_meta = new WC_Order_Item_Product( $item, $_product );
}else{
$item_meta = new WC_Order_Item_Meta( $item, $_product );
}
if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td style="text-align:center; width:100px; vertical-align:middle; border: 1px solid #eee; border-left:0; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style=""><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="30px" style="height:30px; width:auto; vertical-align:middle; margin-right: 10px;" /></div>', $item ); ?></td>
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Product name.
echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );
// UGS / SKU ID
if ( is_object( $_product ) && $_product->get_id()!="" ) {
echo ' (#' . $_product->get_id() . ')';
}
// SKU
if ( is_object( $_product ) && $_product->get_sku()!="" ) {
echo ' (#' . $_product->get_sku() . ')';
}
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
wc_display_item_meta(
$item,
array(
'label_before' => '<strong class="wc-item-meta-label" style="float: ' . esc_attr( $text_align ) . '; margin-' . esc_attr( $margin_side ) . ': .25em; clear: both">',
)
);
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
为我尝试这个工作
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
echo $product_id; // Here Your Product!
}