WooCommerce 挂钩 woocommerce_cancelled_order
WooCommerce Hook woocommerce_cancelled_order
美好的一天,
第一次使用Whosebug,很高兴认识大家。
任何人,我正在为 WooCommerce 编写一个插件,当订单被取消时我会自动退款。当我在没有钩子的情况下在单独的文件中手动执行代码时,我的代码工作正常,但是,使用我的钩子它似乎无法执行。我正在执行以下操作:
add_action('woocommerce_cancelled_order','change_status_to_refund', 10, 1);
function change_status_to_refund( $order_id ) {
wp_redirect( home_url() );
$order = new WC_Order( $order_id );
if( 'refunded' == $order->get_status() ) {
return false;
}
if(!($order->is_paid())) {
return false;
}
$noRefundLimit = 24 * 60; //in minutes until booking
$customer_orders = get_posts( array(
'numberposts' => 1,
'post_parent' => $order_id,
'post_type' => 'wc_booking', // WC orders post type
'post_status' => 'paid, complete' // Only paid, completed bookings
) );
$bookingId = current($customer_orders)->ID;
$bookingStart = current(get_post_meta($bookingId, "_booking_start"));
$time = (new DateTime($bookingStart, new
DateTimeZone("America/Los_Angeles")))->getTimestamp();
$nowTime = (new DateTime())->getTimestamp();
$difference = round(($time - $nowTime)/60);//in minutes
if($difference >= $noRefundLimit) {
$refundPercentage = 1; //how much will we give back? fraction of 1.
// Get Items
$order_items = $order->get_items();
// Refund Amount
$refund_amount = 0;
// Prepare line items which we are refunding
$line_items = array();
if ( $order_items ) {
foreach( $order_items as $item_id => $item ) {
$refund_amount += $item->get_total();
}
}
$refund_amount = ($refund_amount * $refundPercentage);
$refund_reason = "Order Cancelled";
$refund = wc_create_refund( array(
'amount' => $refund_amount,
'reason' => $refund_reason,
'order_id' => $order_id,
'line_items' => $line_items,
'refund_payment' => true
));
var_dump($refund);
$order->update_status('wc-refunded', 'Order Cancelled And Completely
Refunded');
$order->save();
}
我添加了第一条重定向行,其唯一目的是测试它是否重定向,但事实并非如此!知道为什么这个钩子没有触发吗?
感谢您的评论。貌似woocommerce_order_status_cancelled这个钩子不错,但是参数是$order_id。经过大量调试后,我得到了以下结果:
add_action( 'woocommerce_order_status_cancelled', 'change_status_to_refund',
21, 1 );
function change_status_to_refund( $order_id ) {
$order = new WC_Order( $order_id );
$noRefundLimit = 24 * 60; //in minutes until booking
global $wpdb;
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts
WHERE $wpdb->posts.post_parent = $order_id
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$bookingId = current($pageposts)->ID;
$bookingStart = current(get_post_meta($bookingId, "_booking_start"));
$time = (new DateTime($bookingStart, new
DateTimeZone("America/Los_Angeles")))->getTimestamp();
$nowTime = (new DateTime())->getTimestamp();
$difference = round(($time - $nowTime)/60);//in minutes
if($difference >= $noRefundLimit) {
$refundPercentage = 1; //how much will we give back? fraction of 1.
// Get Items
$order_items = $order->get_items();
// Refund Amount
$refund_amount = 0;
// Prepare line items which we are refunding
$line_items = array();
if ( $order_items ) {
foreach( $order_items as $item_id => $item ) {
$refund_amount += $item->get_total();
}
}
$refund_amount = ($refund_amount * $refundPercentage);
$refund_reason = "Order Cancelled";
$refund = wc_create_refund( array(
'amount' => $refund_amount,
'reason' => $refund_reason,
'order_id' => $order_id,
'line_items' => $line_items,
'refund_payment' => true
));
$order->update_status('wc-refunded', 'Order Cancelled And Completely
Refunded');
}
}
我遇到了一些问题。 $order->is_paid() returns false 即使订单已支付(我认为这是因为状态从已支付变为 cancelled/refunded)所以我的代码甚至没有得到远的。然后 get_posts 没有按预期工作。我被迫使用 $wpdb。之后我的代码工作了。
采取适当的措施woocommerce_order_status_cancelled
。
示例如下
add_action( 'woocommerce_order_status_cancelled', 'change_status_to_refund', 10, 1 );
public function change_status_to_refund( $order_id ){
//Do Something here
}
美好的一天,
第一次使用Whosebug,很高兴认识大家。
任何人,我正在为 WooCommerce 编写一个插件,当订单被取消时我会自动退款。当我在没有钩子的情况下在单独的文件中手动执行代码时,我的代码工作正常,但是,使用我的钩子它似乎无法执行。我正在执行以下操作:
add_action('woocommerce_cancelled_order','change_status_to_refund', 10, 1);
function change_status_to_refund( $order_id ) {
wp_redirect( home_url() );
$order = new WC_Order( $order_id );
if( 'refunded' == $order->get_status() ) {
return false;
}
if(!($order->is_paid())) {
return false;
}
$noRefundLimit = 24 * 60; //in minutes until booking
$customer_orders = get_posts( array(
'numberposts' => 1,
'post_parent' => $order_id,
'post_type' => 'wc_booking', // WC orders post type
'post_status' => 'paid, complete' // Only paid, completed bookings
) );
$bookingId = current($customer_orders)->ID;
$bookingStart = current(get_post_meta($bookingId, "_booking_start"));
$time = (new DateTime($bookingStart, new
DateTimeZone("America/Los_Angeles")))->getTimestamp();
$nowTime = (new DateTime())->getTimestamp();
$difference = round(($time - $nowTime)/60);//in minutes
if($difference >= $noRefundLimit) {
$refundPercentage = 1; //how much will we give back? fraction of 1.
// Get Items
$order_items = $order->get_items();
// Refund Amount
$refund_amount = 0;
// Prepare line items which we are refunding
$line_items = array();
if ( $order_items ) {
foreach( $order_items as $item_id => $item ) {
$refund_amount += $item->get_total();
}
}
$refund_amount = ($refund_amount * $refundPercentage);
$refund_reason = "Order Cancelled";
$refund = wc_create_refund( array(
'amount' => $refund_amount,
'reason' => $refund_reason,
'order_id' => $order_id,
'line_items' => $line_items,
'refund_payment' => true
));
var_dump($refund);
$order->update_status('wc-refunded', 'Order Cancelled And Completely
Refunded');
$order->save();
}
我添加了第一条重定向行,其唯一目的是测试它是否重定向,但事实并非如此!知道为什么这个钩子没有触发吗?
感谢您的评论。貌似woocommerce_order_status_cancelled这个钩子不错,但是参数是$order_id。经过大量调试后,我得到了以下结果:
add_action( 'woocommerce_order_status_cancelled', 'change_status_to_refund',
21, 1 );
function change_status_to_refund( $order_id ) {
$order = new WC_Order( $order_id );
$noRefundLimit = 24 * 60; //in minutes until booking
global $wpdb;
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts
WHERE $wpdb->posts.post_parent = $order_id
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$bookingId = current($pageposts)->ID;
$bookingStart = current(get_post_meta($bookingId, "_booking_start"));
$time = (new DateTime($bookingStart, new
DateTimeZone("America/Los_Angeles")))->getTimestamp();
$nowTime = (new DateTime())->getTimestamp();
$difference = round(($time - $nowTime)/60);//in minutes
if($difference >= $noRefundLimit) {
$refundPercentage = 1; //how much will we give back? fraction of 1.
// Get Items
$order_items = $order->get_items();
// Refund Amount
$refund_amount = 0;
// Prepare line items which we are refunding
$line_items = array();
if ( $order_items ) {
foreach( $order_items as $item_id => $item ) {
$refund_amount += $item->get_total();
}
}
$refund_amount = ($refund_amount * $refundPercentage);
$refund_reason = "Order Cancelled";
$refund = wc_create_refund( array(
'amount' => $refund_amount,
'reason' => $refund_reason,
'order_id' => $order_id,
'line_items' => $line_items,
'refund_payment' => true
));
$order->update_status('wc-refunded', 'Order Cancelled And Completely
Refunded');
}
}
我遇到了一些问题。 $order->is_paid() returns false 即使订单已支付(我认为这是因为状态从已支付变为 cancelled/refunded)所以我的代码甚至没有得到远的。然后 get_posts 没有按预期工作。我被迫使用 $wpdb。之后我的代码工作了。
采取适当的措施woocommerce_order_status_cancelled
。
示例如下
add_action( 'woocommerce_order_status_cancelled', 'change_status_to_refund', 10, 1 );
public function change_status_to_refund( $order_id ){
//Do Something here
}