将额外的隐藏信息附加到 Woocommerce 订单
Attaching additional hidden information to Woocommerce order
所以我在 Woocommerce 中遇到了一个问题。
我不知道这个问题的主题是不是这里的解决方案,但让我解释一下我想要实现的目标。
我创建了这个函数,它将某个类别中购物车中所有产品的总价格汇总为一个小计。根据该小计,用户会收到一条消息,说明用户获得的欧元金额:
## Category based sum ##
function cat_cart_sum($cat_id) {
if ( ! is_page( 'winkelmand' ) ) {
return;
}
$cat_count = 0;
// Iterating through each cart item
foreach(WC()->cart->get_cart() as $cart_item)
if( has_term( $cat_id, 'product_cat', $cart_item['product_id'])) {
$regularprice = $cart_item['data']->get_price();
$price = $regularprice * $cart_item['quantity'];
$cat_count += $price;
}
if ($cat_count >= 40 && $cat_count <= 100) {
wc_add_notice('U krijgt bij het afhalen €10 gratis vuurwerk!', 'notice');
} elseif ($cat_count > 100 && $cat_count <= 200) {
wc_add_notice('U krijgt bij het afhalen €25 gratis vuurwerk!', 'notice');
} elseif ($cat_count > 200 && $cat_count <= 300) {
wc_add_notice('U krijgt bij het afhalen €50 gratis vuurwerk!', 'notice');
} elseif ($cat_count > 300 && $cat_count <= 400) {
wc_add_notice('U krijgt bij het afhalen €75 gratis vuurwerk!', 'notice');
} elseif ($cat_count >= 400) {
wc_add_notice('U krijgt bij het afhalen €100 gratis vuurwerk!', 'notice');
}
return $cat_count;
}
用户现在知道他们得到了多少。但仅限于他们的购物车。我希望能够从任何地方通过电子邮件、发票或其他方式调用此金额。
我迷失在这个问题中。有人有解决办法吗?
亲切的问候
WC()->cart
对象不适用于订单和电子邮件通知……所以一旦客户结账,您就不能使用它。
此外 if ( ! is_page( 'winkelmand' ) ) return;
此代码仅适用于页面 'winkelmand'
… 对于订单。
您将在下面找到具有 3 个参数的重新排列的函数:
$cat_id
(类别 ID)
$arg
(可选:订单 ID 或 WC_Order 对象)
$is_email
(可选:(布尔值)仅在电子邮件通知中设置为 'true')
这个函数有 2 个循环:
- 如果
$arg
未定义,购物车项目循环将起作用…
- 如果定义了
$arg
(通过订单 ID 或订单对象),订单项目循环将改为工作。
这是您的函数代码:
function cat_sum( $cat_id, $arg = null, $is_email = false ) {
// Will work everywhere except on page 'winkelmand'
if ( is_page( 'winkelmand' ) ) return;
$total_count = 0;
$type = gettype($arg);
// 1. WC_Cart
if( $type == 'NULL' && gettype(WC()->cart) == 'object' && ! WC()->cart->is_empty() ){
// Iterating through each cart item
foreach(WC()->cart->get_cart() as $cart_item)
if( has_term( $cat_id, 'product_cat', $cart_item['product_id'] ) )
$total_count += $cart_item['data']->get_price() * $cart_item['quantity'];
$is_order = false; // Not an order
}
// Order ID is set in $arg
elseif( $type == 'integer' || $type == 'string' ){
// get an instance of the WC_Order object
$order = wc_get_order($arg);
$is_order = true; // An order
}
// WC_Order Object is set in $arg
elseif( $type == 'object' ){
$order = $arg;
$is_order = true; // An order
}
else return;
// 2. WC_Order
if( $is_order )
foreach($order->get_items() as $item )
if( has_term( $cat_id, 'product_cat', $item->get_product_id() ) )
$total_count += $item->get_product()->get_price() * $item->get_quantity();
// Display notice
if ( $total_count >= 40 && $total_count <= 100 )
$message = __( 'U krijgt bij het afhalen €10 gratis vuurwerk!' );
elseif ( $total_count > 100 && $total_count <= 200 )
$message = __( 'U krijgt bij het afhalen €25 gratis vuurwerk!' );
elseif ( $total_count > 200 && $total_count <= 300 )
$message = __( 'U krijgt bij het afhalen €50 gratis vuurwerk!' );
elseif ( $total_count > 300 && $total_count <= 400 )
$message = __( 'U krijgt bij het afhalen €75 gratis vuurwerk!' );
elseif ( $total_count >= 400 )
$message = __( 'U krijgt bij het afhalen €100 gratis vuurwerk!' );
else return;
if( ! $is_email ){
wc_add_notice( $message, 'notice' );
return $total_count;
} else {
return array(
'total' => $total_count,
'notice' => $message
);
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
用法:
1) 当 WC_Cart 对象可用时(在客户结帐之前)。您将按原样使用它 (例如使用 'clothing' 类别):
$cat_count = cat_sum( 'clothing' );
echo '<p>'.$cat_count.'</p>';
2) 当 WC_Cart 对象不可用时(客户结账后)。您将需要设置订单 ID 或 WC_Order 对象,如本示例中的订单接收页面 (Thankyou):
add_action( 'woocommerce_thankyou', 'cat_count_in_thankyou', 10, 1 );
function cat_count_in_thankyou( $order_id ) {
echo '<p>'.cat_sum( 'clothing', $order_id ).'</p>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
3) 在电子邮件通知中 (WC_Cart 对象不可用并且不显示通知) 并且您将需要设置订单 ID 或 WC_Order 对象。对于这种情况,我们使用设置为 true
的第三个参数。该函数将 return 一个包含类别计数和相应消息(通知)的数组。
此示例适用于电子邮件通知:
// Display the total Cat in email notifications
add_action( 'woocommerce_email_after_order_table', 'add_cat_sum_to_emails', 9, 4 );
function add_cat_sum_to_emails( $order, $sent_to_admin, $plain_text, $email ){
$cat_sum = cat_sum( 'clothing', $order, true );
echo '<p>Total cat: '.$cat_sum['total'].'</p>';
}
// Display the notice in email notifications
add_action( 'woocommerce_email_order_details', 'add_custom_notice_to_emails', 4, 4 );
function add_custom_notice_to_emails( $order, $sent_to_admin, $plain_text, $email ){
$cat_sum = cat_sum( 'clothing', $order, true );
echo '<p style="border:solid 1px #333; padding:12px;"> '.$cat_sum['notice'].'</p>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
一切都经过测试并且可以正常工作。
所以我在 Woocommerce 中遇到了一个问题。 我不知道这个问题的主题是不是这里的解决方案,但让我解释一下我想要实现的目标。
我创建了这个函数,它将某个类别中购物车中所有产品的总价格汇总为一个小计。根据该小计,用户会收到一条消息,说明用户获得的欧元金额:
## Category based sum ##
function cat_cart_sum($cat_id) {
if ( ! is_page( 'winkelmand' ) ) {
return;
}
$cat_count = 0;
// Iterating through each cart item
foreach(WC()->cart->get_cart() as $cart_item)
if( has_term( $cat_id, 'product_cat', $cart_item['product_id'])) {
$regularprice = $cart_item['data']->get_price();
$price = $regularprice * $cart_item['quantity'];
$cat_count += $price;
}
if ($cat_count >= 40 && $cat_count <= 100) {
wc_add_notice('U krijgt bij het afhalen €10 gratis vuurwerk!', 'notice');
} elseif ($cat_count > 100 && $cat_count <= 200) {
wc_add_notice('U krijgt bij het afhalen €25 gratis vuurwerk!', 'notice');
} elseif ($cat_count > 200 && $cat_count <= 300) {
wc_add_notice('U krijgt bij het afhalen €50 gratis vuurwerk!', 'notice');
} elseif ($cat_count > 300 && $cat_count <= 400) {
wc_add_notice('U krijgt bij het afhalen €75 gratis vuurwerk!', 'notice');
} elseif ($cat_count >= 400) {
wc_add_notice('U krijgt bij het afhalen €100 gratis vuurwerk!', 'notice');
}
return $cat_count;
}
用户现在知道他们得到了多少。但仅限于他们的购物车。我希望能够从任何地方通过电子邮件、发票或其他方式调用此金额。 我迷失在这个问题中。有人有解决办法吗?
亲切的问候
WC()->cart
对象不适用于订单和电子邮件通知……所以一旦客户结账,您就不能使用它。
此外 if ( ! is_page( 'winkelmand' ) ) return;
此代码仅适用于页面 'winkelmand'
… 对于订单。
您将在下面找到具有 3 个参数的重新排列的函数:
$cat_id
(类别 ID)$arg
(可选:订单 ID 或 WC_Order 对象)$is_email
(可选:(布尔值)仅在电子邮件通知中设置为 'true')
这个函数有 2 个循环:
- 如果
$arg
未定义,购物车项目循环将起作用… - 如果定义了
$arg
(通过订单 ID 或订单对象),订单项目循环将改为工作。
这是您的函数代码:
function cat_sum( $cat_id, $arg = null, $is_email = false ) {
// Will work everywhere except on page 'winkelmand'
if ( is_page( 'winkelmand' ) ) return;
$total_count = 0;
$type = gettype($arg);
// 1. WC_Cart
if( $type == 'NULL' && gettype(WC()->cart) == 'object' && ! WC()->cart->is_empty() ){
// Iterating through each cart item
foreach(WC()->cart->get_cart() as $cart_item)
if( has_term( $cat_id, 'product_cat', $cart_item['product_id'] ) )
$total_count += $cart_item['data']->get_price() * $cart_item['quantity'];
$is_order = false; // Not an order
}
// Order ID is set in $arg
elseif( $type == 'integer' || $type == 'string' ){
// get an instance of the WC_Order object
$order = wc_get_order($arg);
$is_order = true; // An order
}
// WC_Order Object is set in $arg
elseif( $type == 'object' ){
$order = $arg;
$is_order = true; // An order
}
else return;
// 2. WC_Order
if( $is_order )
foreach($order->get_items() as $item )
if( has_term( $cat_id, 'product_cat', $item->get_product_id() ) )
$total_count += $item->get_product()->get_price() * $item->get_quantity();
// Display notice
if ( $total_count >= 40 && $total_count <= 100 )
$message = __( 'U krijgt bij het afhalen €10 gratis vuurwerk!' );
elseif ( $total_count > 100 && $total_count <= 200 )
$message = __( 'U krijgt bij het afhalen €25 gratis vuurwerk!' );
elseif ( $total_count > 200 && $total_count <= 300 )
$message = __( 'U krijgt bij het afhalen €50 gratis vuurwerk!' );
elseif ( $total_count > 300 && $total_count <= 400 )
$message = __( 'U krijgt bij het afhalen €75 gratis vuurwerk!' );
elseif ( $total_count >= 400 )
$message = __( 'U krijgt bij het afhalen €100 gratis vuurwerk!' );
else return;
if( ! $is_email ){
wc_add_notice( $message, 'notice' );
return $total_count;
} else {
return array(
'total' => $total_count,
'notice' => $message
);
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
用法:
1) 当 WC_Cart 对象可用时(在客户结帐之前)。您将按原样使用它 (例如使用 'clothing' 类别):
$cat_count = cat_sum( 'clothing' );
echo '<p>'.$cat_count.'</p>';
2) 当 WC_Cart 对象不可用时(客户结账后)。您将需要设置订单 ID 或 WC_Order 对象,如本示例中的订单接收页面 (Thankyou):
add_action( 'woocommerce_thankyou', 'cat_count_in_thankyou', 10, 1 );
function cat_count_in_thankyou( $order_id ) {
echo '<p>'.cat_sum( 'clothing', $order_id ).'</p>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
3) 在电子邮件通知中 (WC_Cart 对象不可用并且不显示通知) 并且您将需要设置订单 ID 或 WC_Order 对象。对于这种情况,我们使用设置为 true
的第三个参数。该函数将 return 一个包含类别计数和相应消息(通知)的数组。
此示例适用于电子邮件通知:
// Display the total Cat in email notifications
add_action( 'woocommerce_email_after_order_table', 'add_cat_sum_to_emails', 9, 4 );
function add_cat_sum_to_emails( $order, $sent_to_admin, $plain_text, $email ){
$cat_sum = cat_sum( 'clothing', $order, true );
echo '<p>Total cat: '.$cat_sum['total'].'</p>';
}
// Display the notice in email notifications
add_action( 'woocommerce_email_order_details', 'add_custom_notice_to_emails', 4, 4 );
function add_custom_notice_to_emails( $order, $sent_to_admin, $plain_text, $email ){
$cat_sum = cat_sum( 'clothing', $order, true );
echo '<p style="border:solid 1px #333; padding:12px;"> '.$cat_sum['notice'].'</p>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
一切都经过测试并且可以正常工作。