我将Woocommerce与WCMP(WC Markeplace)一起使用,但不允许供应商更新订单状态(即:从“处理”到“完成”)。
他们的后端没有选项,所以我尝试添加一个自定义按钮来处理它!
任何帮助都将是惊人的!这是我的代码(我在附近的某个地方找到的):
<form method="post">
<input type="hidden" name="mark_as_received" value="true">
<input type="hidden" name="order_id" value="<?php echo esc_attr($order_id);?>">
<?php wp_nonce_field( \'so_38792085_nonce_action\', \'_so_38792085_nonce_field\' ); ?>
<input type="submit" value="I Got It!">
</form>
并处理表格:
<?php
add_action( \'wp_loaded\', \'custom_button_to_update\' );
function custom_button_to_update(){
// not a "mark as received" form submission
if ( ! isset( $_POST[\'mark_as_received\'] ) ){
return;
}
// basic security check
if ( ! isset( $_POST[\'_so_38792085_nonce_field\'] ) || ! wp_verify_nonce( $_POST[\'_so_38792085_nonce_field\'], \'so_38792085_nonce_action\' ) ) {
return;
}
// make sure order id is submitted
if ( ! isset( $_POST[\'order_id\'] ) ){
$order_id = intval( $_POST[\'order_id\'] );
$order = wc_get_order( $order_id );
$order->update_status( "completed" );
return;
}
else {
echo "failed";
}
}
提前感谢您的帮助!
最合适的回答,由SO网友:João Teixeira 整理而成
谢谢大家,我找到答案了HERE.
我根据自己的需要对代码进行了一些修改,并添加了一个按钮。
html:
<div>
<form method="post" name="update_status">
<button type="submit" name="marked_as_completed">Mark as Completed</button>
</form>
</div>
php:
<?php
if (isset($_POST["marked_as_completed"]))
{
$completed_status = $order->get_id();
$order = new WC_Order($completed_status);
$order->update_status(\'completed\', \'order_note\');
echo \'<h2 style="color:green;">Completato ✓</h2>\';
}
?>