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 对象可用时(在客户结帐之前)。您将像使用它一样使用它(例如使用“服装”类别):
$cat_count = cat_sum( 'clothing' );
echo '<p>'.$cat_count.'</p>';
2) 当 WC_Cart 对象不可用时(客户结帐后)。您需要在收到的订单页面(谢谢)中设置订单 ID 或 WC_Order 对象,如本例所示:
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 的第三个参数。该函数将返回一个包含类别计数和相应消息(通知)的数组。
此示例适用于电子邮件通知:
// 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 文件或任何插件文件中。
一切都经过测试并且有效。