所以我在这里遇到了一个问题。我不知道这个问题的主题是否是这里的解决办法,但让我解释一下我想要实现的目标。
我创建了这个函数,它将购物车中某一类别中的所有产品的总价汇总成一个小计。基于该小计,用户将得到一条消息,说明用户在欧元中得到的金额:
## 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;
}用户现在知道他们得到了多少。但只在他们的车里。我想能从任何地方的电子邮件,发票或其他任何地方拨付这笔款项.我在这个问题上迷路了。有人有解决办法吗?
亲切的问候
发布于 2017-11-04 00:42:47
WC()->cart对象将无法处理订单和电子邮件通知,…因此,一旦客户结帐,您就不能使用它。
此外,对于if ( ! is_page( 'winkelmand' ) ) return;,此代码仅在页'winkelmand'…上工作。为了秩序。
下面,您将发现您的重新排列的函数包含3个参数:
$cat_id (类别ID)$arg (可选:订单ID或WC_Order对象)$is_email (可选:(布尔值)仅在电子邮件通知中设置为“true”)这个函数有两个循环:
$arg未定义…将工作的cart items循环$arg (由Order Id或Order对象),则将使用order is循环。下面是您的函数的代码:
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对象,如本例中的顺序接收页面(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的第三个参数。该函数将返回一个数组,其中包含类别计数和相应的消息(通知)。
此示例适用于电子邮件通知:
// 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文件中,或者在任何插件文件中。
一切都是经过测试和运作的。
https://stackoverflow.com/questions/47098490
复制相似问题