admin管理员组文章数量:1194922
Our store sells software and we're adding a software voucher code to each purchased. Once the purchase is completed (via the woocommerce_payment_complete
hook) we generate the voucher code and add it to each item purchased via wc_add_order_item_meta method.
Summarized code:
add_filter('woocommerce_payment_complete', 'add_voucher_code');
function add_voucher_code( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item) {
for ($i = 1; $i <= $item['qty']; $i++) {
$voucher_code = 'API request based on order information to get voucher code';
wc_add_order_item_meta($item->get_id(), 'Voucher Code', $voucher_code);
}
}
}
For some reason or another the item custom meta shows up on the order confirmation page, but doesn't in the confirmation email. (problem 1 slaps forehead) So we're utilizing the woocommerce_order_item_meta_end
hook to add it to the confirmation email. (wc_get_order_item_meta)
Summarized code:
add_action('woocommerce_order_item_meta_end', 'email_confirmation_display_order_items', 10, 4);
function email_confirmation_display_order_items($item_id, $item, $order, $plain_text) {
echo '<div>Voucher Code: '. wc_get_order_item_meta( $item_id, 'Voucher Code') .'</div>';
}
Problem 2 is that added snippet of code shows on the both the order confirmation page (so now it shows twice) and in the order confirmation email. (slaps forehead again)
Current Problem 2 Solution
Right now we solved it by adding an if statement which is suggested here. Like so:
// Only on emails notifications
if( ! (is_admin() || is_wc_endpoint_url() )) {
echo '<div>Voucher Code: '. wc_get_order_item_meta( $item_id, 'Voucher Code') .'</div>';
}
This feels like a band-aid fix and any insight/suggestions would be much appreciated. Thanks!
Our store sells software and we're adding a software voucher code to each purchased. Once the purchase is completed (via the woocommerce_payment_complete
hook) we generate the voucher code and add it to each item purchased via wc_add_order_item_meta method.
Summarized code:
add_filter('woocommerce_payment_complete', 'add_voucher_code');
function add_voucher_code( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item) {
for ($i = 1; $i <= $item['qty']; $i++) {
$voucher_code = 'API request based on order information to get voucher code';
wc_add_order_item_meta($item->get_id(), 'Voucher Code', $voucher_code);
}
}
}
For some reason or another the item custom meta shows up on the order confirmation page, but doesn't in the confirmation email. (problem 1 slaps forehead) So we're utilizing the woocommerce_order_item_meta_end
hook to add it to the confirmation email. (wc_get_order_item_meta)
Summarized code:
add_action('woocommerce_order_item_meta_end', 'email_confirmation_display_order_items', 10, 4);
function email_confirmation_display_order_items($item_id, $item, $order, $plain_text) {
echo '<div>Voucher Code: '. wc_get_order_item_meta( $item_id, 'Voucher Code') .'</div>';
}
Problem 2 is that added snippet of code shows on the both the order confirmation page (so now it shows twice) and in the order confirmation email. (slaps forehead again)
Current Problem 2 Solution
Right now we solved it by adding an if statement which is suggested here. Like so:
// Only on emails notifications
if( ! (is_admin() || is_wc_endpoint_url() )) {
echo '<div>Voucher Code: '. wc_get_order_item_meta( $item_id, 'Voucher Code') .'</div>';
}
This feels like a band-aid fix and any insight/suggestions would be much appreciated. Thanks!
Share Improve this question edited Nov 4, 2019 at 17:57 Ryan asked Oct 30, 2019 at 19:18 RyanRyan 2212 silver badges7 bronze badges2 Answers
Reset to default 2Since this isn't getting much action I'll put our band-aid fix as the current solution.
Problem 1 Solution
The added item meta data shows on the order confirmation page and does not show on the confirmation email. We solved this by utilizing the woocommerce_order_item_meta_end
hook to add the extra item meta.
Problem 2 Solution
Adding the item meta data via woocommerce_order_item_meta_end
to the confirmation
email also adds it to the confirmation page (visibly duplicating it). We solved this by adding an if statement suggested by @LoicTheAztec here.
add_action('woocommerce_order_item_meta_end', 'email_confirmation_display_order_items', 10, 4);
function email_confirmation_display_order_items($item_id, $item, $order, $plain_text) {
// Only on emails notifications
if( ! (is_admin() || is_wc_endpoint_url() )) {
echo '<div>Voucher Code: '. wc_get_order_item_meta( $item_id, 'Voucher Code') .'</div>';
}
}
Resources
email confirmation hook visual guide
wc_get_order_item_meta docs
Filter out unwanted order item meta data from Woocommerce email notifications
You could also just only execute the add_action whenever you're in a different action. You could hook into add_action( 'woocommerce_email_header' );
first and then hook into woocommerce_order_item_meta_end
. This way we're only doing this whenever the woocommerce_email_header
action is executed.
add_action( 'woocommerce_email_header', function() {
add_action( 'woocommerce_order_item_meta_end', [ $this, 'functionThatDoesSomething' ], 10, 2 );
} );
本文标签: phpWooCommerce How to display item meta data in email confirmation using woocommerceorderitemmetaend
版权声明:本文标题:php - WooCommerce: How to display item meta data in email confirmation using woocommerce_order_item_meta_end 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738494798a2089918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论