WooCommerce Orders metabox:在自定义提交动作上运行php代码

问题描述:

在Woocommerce中,我可以在自定义元框中的订单编辑页面上添加自定义提交按钮。WooCommerce Orders metabox:在自定义提交动作上运行php代码

这里是我的代码(在function.php WordPress的主题添加)

add_action('add_meta_boxes', 'MY_order_meta_boxes'); 
function MY_order_meta_boxes() { 

    add_meta_box(
     'woocommerce-order-verifyemail', 
     __('Trusted List'), 
     'order_meta_box_content', 
     'shop_order', 
     'side', 
     'default' 
    ); 
} 

function order_meta_box_content($order_id) { 
    global $woocommerce, $table_prefix, $wpdb; 
    $order = new WC_Order($order_id); 
    $customeremail = $order->get_billing_email(); 
    ?> 

     <form method="post" action="CURRENT_FILE_URL"> 
      <input type="submit" name="submit" value="submit"/> 
     </form> 

    <?php 
    if(isset($submit)) {$order->add_order_note(sprintf("test2"));} 
    ?> 

    <?php 
    return $order_id; 
} 

但我不知道为什么,代码按钮被点击时不运行(提交)。

如何在自定义元框中单击提交按钮时运行一些自定义代码?

要为你期望你需要一些更多的东西,使这项工作。我也删除了不必要的代码和一些错误。另外<imput>"submit" ID太泛化,可能会产生意想不到的错误。

你可以做任何动作(或保存)在save_post行动钩勾住了自定义函数:

// Add a custom metabox 
add_action('add_meta_boxes', 'trusted_list_order_meta_boxes'); 
function trusted_list_order_meta_boxes() { 

    add_meta_box(
     'woocommerce-order-verifyemail', 
     __('Trusted List'), 
     'trusted_list_order_meta_box_content', 
     'shop_order', 
     'side', 
     'default' 
    ); 
} 

// Custom metabox content 
function trusted_list_order_meta_box_content($post){ 
    $customeremail = get_post_meta($post->ID, '_billing_email', true); 
    $button_text = __('Add Note action', 'woocommerce'); 

    echo '<form method="post" action="CURRENT_FILE_URL"> 
     <input type="submit" name="submit_trusted_list" value="' . $button_text . '"/> 
     <input type="hidden" name="trusted_list_nonce" value="' . wp_create_nonce() . '"> 
    </form>'; 
} 

// Saving or doing an action when submitting 
add_action('save_post', 'trusted_list_save_meta_box_data'); 
function trusted_list_save_meta_box_data($post_id){ 

    // Only for shop order 
    if ('shop_order' != $_POST[ 'post_type' ]) 
     return $post_id; 

    // Check if our nonce is set (and our cutom field) 
    if (! isset($_POST[ 'trusted_list_nonce' ]) && isset($_POST['submit_trusted_list'])) 
     return $post_id; 

    $nonce = $_POST[ 'trusted_list_nonce' ]; 

    // Verify that the nonce is valid. 
    if (! wp_verify_nonce($nonce)) 
     return $post_id; 

    // Checking that is not an autosave 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 

    // Check the user’s permissions (for 'shop_manager' and 'administrator' user roles) 
    if (! current_user_can('edit_shop_order', $post_id) && ! current_user_can('edit_shop_orders', $post_id)) 
     return $post_id; 

    // Action to make or (saving data) 
    if(isset($_POST['submit_trusted_list'])) { 
     $order = wc_get_order($post_id); 
     // $customeremail = $order->get_billing_email(); 
     $order->add_order_note(sprintf("test2")); 
    } 
} 

代码放在您的活动子主题的function.php文件(或主题)还是在任何插件文件中。

代码在Woocommerce 3+上测试并正常工作。您将获得:

enter image description here

在您的自定义Metabox 内容功能您将无法使用$_POST提交的数据得到的任何数据 ...因此,例如$_POST['submit']始终为空

+0

@LoicTheAztech,谢谢你,工作很棒。 – Mostafa

+0

@LoicTheAztech你可以做出那个按钮javascript? – Mostafa

+0

@Mostafa你是什么意思:“做了那个按钮javascript? 。如果你的意思是“阿贾克斯”,那就更加复杂了,我没有时间去解决这个问题。 – LoicTheAztec

...我认为你应该使用$ _ POST [“提交”]不是$提交

+0

你说得对。我更改为$ _POST ['submit']但没有运气:( – Mostafa