Woocommerce:在用户点击结帐页面下订单或点击订单支付页面上的订单支付后,我如何运行一个函数?



我在woocommerce中创建了一个函数,该函数根据所选择的支付方式在订单元数据中创建一个新的自定义字段,并在订单表中显示该字段(如果存在)。该函数在进行前端检出时按预期工作。但是,如果我手动创建一个订单,客户点击"为订单付款",它就不起作用了。在order_pay页面。

只在结帐后运行的旧代码如下所示:

// Add custom order meta data on checkout
add_action( 'woocommerce_checkout_create_order', 'add_custom_order_meta', 10, 2 );
function add_custom_order_meta( $order, $data ) {
// Get the payment method for the order
$payment_method = $order->get_payment_method();
// Only add custom meta data for certain payment methods
if ( in_array( $payment_method, array( 'bacs', 'cheque' ) ) ) {
// Get the order total
$order_total = $order->get_total();

// Set the currency to convert to based on payment method
$currency_to = ( $payment_method === 'bacs' ) ? 'hkd' : 'thb';

// Convert the order total to the selected currency
$converted_total_gptlove = get_conversion( 'number=' . $order_total . '&from=usd&to=' . $currency_to . '&dp=2' );

// Set the currency symbol and code based on payment method
$currency_symbol = ( $payment_method === 'bacs' ) ? '$' : '฿';
$currency_code = ( $payment_method === 'bacs' ) ? 'HKD' : 'THB';

// Update the order meta data with the converted total and currency info
$order->update_meta_data( 'converted_total_gptlove', $currency_symbol . $converted_total_gptlove . ' ' . $currency_code );
}
}
// Add custom meta field to order details on Thank You page and order emails
add_filter( 'woocommerce_get_order_item_totals', 'add_custom_order_item_totals', 10, 3 );
function add_custom_order_item_totals( $total_rows, $order, $tax_display ) {

// Get the value of the custom field "converted_total_gptlove" for the current order
$converted_total_gptlove = get_post_meta( $order->get_id(), 'converted_total_gptlove', true );

// If the custom field is not empty, add it as a new row to the order details table
if ( ! empty( $converted_total_gptlove ) ) {
$total_rows['converted_total_gptlove'] = array(
'label' => __( 'Converted Total:', 'woocommerce' ), // Set the label for the new row
'value' => $converted_total_gptlove, // Set the value for the new row
);
}

// Return the modified order details table with the new row added
return $total_rows;
}

修改后的代码仍然在结帐期间运行,但在用户单击"为订单付款"后不会触发。我哪里做错了?

add_action( 'woocommerce_checkout_create_order', 'add_custom_order_meta' );
add_action( 'woocommerce_receipt_order_pay', 'add_custom_order_meta' );
function add_custom_order_meta( $order ) {
// Get the payment method for the order
$payment_method = $order->get_payment_method();
// Only add custom meta data for certain payment methods
if ( in_array( $payment_method, array( 'bacs', 'cheque' ) ) ) {
// Get the order total
$order_total = $order->get_total();

// Set the currency to convert to based on payment method
$currency_to = ( $payment_method === 'bacs' ) ? 'hkd' : 'thb';

// Convert the order total to the selected currency
$converted_total_gptlove = get_conversion( 'number=' . $order_total . '&from=usd&to=' . $currency_to . '&dp=2' );

// Set the currency symbol and code based on payment method
$currency_symbol = ( $payment_method === 'bacs' ) ? '$' : '฿';
$currency_code = ( $payment_method === 'bacs' ) ? 'HKD' : 'THB';

// Update the order meta data with the converted total and currency info
$order->update_meta_data( 'converted_total_gptlove', $currency_symbol . $converted_total_gptlove . ' ' . $currency_code );
}
}
// Add custom meta field to order details on Thank You page and order emails
add_filter( 'woocommerce_get_order_item_totals', 'add_custom_order_item_totals', 10, 3 );
function add_custom_order_item_totals( $total_rows, $order, $tax_display ) {

// Get the value of the custom field "converted_total_gptlove" for the current order
$converted_total_gptlove = get_post_meta( $order->get_id(), 'converted_total_gptlove', true );

// If the custom field is not empty, add it as a new row to the order details table
if ( ! empty( $converted_total_gptlove ) ) {
$total_rows['converted_total_gptlove'] = array(
'label' => __( 'Converted Total:', 'woocommerce' ), // Set the label for the new row
'value' => $converted_total_gptlove, // Set the value for the new row
);
}

// Return the modified order details table with the new row added
return $total_rows;
}

你可以使用下面的代码

add_action( 'woocommerce_checkout_create_order', 'add_custom_order_meta' );
add_action( 'woocommerce_receipt_order_pay', 'add_custom_order_meta' );
function add_custom_order_meta( $order, $data = null ) {
// Use the appropriate parameter based on the hook that triggered the function
$payment_method = ($data !== null) ? $data['payment_method'] : $order->get_payment_method();
// rest of the function code remains the same...
}

最新更新