1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個贊
由于該方法WC_Order set_order_number()不存在,我們將在下訂單時(在訂單創(chuàng)建時)添加自定義字段(自定義元數(shù)據(jù))。然后我們將在過濾器掛鉤中獲取該訂單自定義元數(shù)據(jù)woocommerce_order_number。
代碼:
add_action( 'woocommerce_checkout_update_order_meta', 'save_the_order_number', 10, 2 );
function save_the_order_number( $order_id, $data ) {
$order = wc_get_order( $order_id ); // The order Object
if ( 'cheque' === $order->get_payment_method() ) {
$prefix = 'CHE';
$suffix = '';
} else {
$prefix = '';
$suffix = '';
}
update_post_meta( $order_id, '_order_number', $prefix . $order_id . $suffix );
}
add_filter( 'woocommerce_order_number', 'set_order_number', 10, 2 );
function set_order_number( $order_id, $order ) {
// Get the order number (custom meta data)
$order_number = $order->get_meta('_order_number');
// If the order number doesn't exist (we keep that for old orders, or manual orders)
if ( empty($order_number) ) {
if ( 'cheque' === $order->get_payment_method() ) {
$prefix = 'CHE';
$suffix = '';
$order_number = $prefix . $order_id . $suffix;
} else {
$order_number = $order_id;
}
}
return $order_number;
}
代碼位于活動子主題(或活動主題)的functions.php 文件中。經(jīng)過測試并工作。
現(xiàn)在,如果您希望能夠在管理訂單頁面上編輯訂單號,請另外使用以下代碼:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_admin_order_order_number_custom_field' );
function display_admin_order_order_number_custom_field( $order ){
echo '<div class="edit_order_number"><p class="form-field _order_number_field" style="width:100%;">
<label for="_order_number">'. __("Order number", "woocommerce").':</label>
<input type="text" id="_order_number" name="_order_number" value="'. $order->get_order_number() .'">
</p></div>';
}
add_action( 'save_post_shop_order', 'save_admin_order_order_number_custom_field' );
function save_admin_order_order_number_custom_field( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
// Make sure that 'shipping_date' is set.
if ( isset( $_POST['_order_number'] ) ) {
// Update custom field value
update_post_meta( $post_id, '_order_number', sanitize_text_field( $_POST['_order_number'] ) );
}
}
代碼位于活動子主題(或活動主題)的functions.php 文件中。經(jīng)過測試并工作。
- 1 回答
- 0 關(guān)注
- 145 瀏覽
添加回答
舉報(bào)