我需要减少 WooCommerce 网站上的欺诈行为。大多数时候,有人编写了一个机器人,它一遍又一遍地使用不同的被盗信用卡,并使用该人的账单信息,但将用被盗资金购买的产品运送到其他地方。因此,如果账单信息与送货信息不匹配,我想将所有订单置于保留状态。
这是我迄今为止在functions.php 文件中无法正常工作的函数。谁能指出我哪里出错了?
add_action( 'woocommerce_thankyou', 'woocommerce_billing_shipping_address_match', 10, 1);
function woocommerce_billing_shipping_address_match( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get an instance of the WC_Customer Object from the user ID
$customer = new WC_Customer( $user_id );
// Customer billing information details (from account)
// if this doesn't work, try removing the 2nd equals sign in each line below
$billing_first_name == $customer->get_billing_first_name();
$billing_last_name == $customer->get_billing_last_name();
$billing_company == $customer->get_billing_company();
$billing_address_1 == $customer->get_billing_address_1();
$billing_address_2 == $customer->get_billing_address_2();
$billing_city == $customer->get_billing_city();
$billing_state == $customer->get_billing_state();
$billing_postcode == $customer->get_billing_postcode();
$billing_country == $customer->get_billing_country();
// Customer shipping information details (from account)
$shipping_first_name == $customer->get_shipping_first_name();
$shipping_last_name == $customer->get_shipping_last_name();
$shipping_company == $customer->get_shipping_company();
$shipping_address_1 == $customer->get_shipping_address_1();
$shipping_address_2 == $customer->get_shipping_address_2();
$shipping_city == $customer->get_shipping_city();
$shipping_state == $customer->get_shipping_state();
$shipping_postcode == $customer->get_shipping_postcode();
$shipping_country == $customer->get_shipping_country();
$order = wc_get_order( $order_id );
// The text for the note
$note_1 = __("This was a test order");
// Add the note
$order->add_order_note( $note_1 );
// check if all the billing info matches all of the shipping info and place on hold if not
if( $billing_first_name !== $shipping_first_name ||
$billing_last_name !== $shipping_last_name ||
$billing_address_1 !== $shipping_address_1 ||
$billing_address_2 !== $shipping_address_2 ||
$billing_city !== $shipping_city ||
$billing_state !== $shipping_state ||
$billing_postcode !== $shipping_postcode ||
$billing_country !== $shipping_country) {
// The text for the note
$note_2 = __("The billing and shipping info did not match");
// Add the note
$order->add_order_note( $note_2 );
// Place the order on hold
$order->update_status( 'on-hold' );
}
}
最佳答案
你在这里做错了两件事。 #1,$order 变量应该位于获取账单和运输信息的变量上方。 #2,您正在从 $customer 中获取存储地址数据的变量,但这是不必要的。你也可以从 $order 那里获取。不需要 $customer 变量。总的来说,您的functions.php代码应如下所示:
/* PLACE ORDERS ON HOLD WHEN BILLING & SHIPPING INFO DO NOT MATCH */
add_action( 'woocommerce_thankyou', 'woocommerce_billing_shipping_address_match', 10, 1);
function woocommerce_billing_shipping_address_match( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get the order id
$order = wc_get_order( $order_id );
// Customer billing information details
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$billing_company = $order->get_billing_company();
$billing_address_1 = $order->get_billing_address_1();
$billing_address_2 = $order->get_billing_address_2();
$billing_city = $order->get_billing_city();
$billing_state = $order->get_billing_state();
$billing_postcode = $order->get_billing_postcode();
$billing_country = $order->get_billing_country();
// Customer shipping information details (from account)
$shipping_first_name = $order->get_shipping_first_name();
$shipping_last_name = $order->get_shipping_last_name();
$shipping_company = $order->get_shipping_company();
$shipping_address_1 = $order->get_shipping_address_1();
$shipping_address_2 = $order->get_shipping_address_2();
$shipping_city = $order->get_shipping_city();
$shipping_state = $order->get_shipping_state();
$shipping_postcode = $order->get_shipping_postcode();
$shipping_country = $order->get_shipping_country();
// check if any the billing info does not match the shipping info and place on hold if not
if($billing_first_name !== $shipping_first_name || $billing_last_name !== $shipping_last_name || $billing_address_1 !== $shipping_address_1 || $billing_address_2 !== $shipping_address_2 || $billing_city !== $shipping_city || $billing_state !== $shipping_state || $billing_postcode !== $shipping_postcode || $billing_country !== $shipping_country) {
// Place the order on hold
$order->update_status( 'on-hold' );
}
}
关于php - 如果帐单地址和送货地址不匹配,则下 WooCommerce 订单 'On Hold',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68687587/