2017-11-14 7 views
-1

私は顧客に電子メールを送信するカスタム取引ボタンをオーダーリストに追加しようとしています(ステータスは「処理中」の注文とカスタムフィールドの注文の横にのみ表示されます) deltime 'は空ではない)。だから私はボタンを表示しているが、電子メールは私がwoocommerce/ordersにいるとき、またはボタンがクリックされたときにだけ送信する代わりにリフレッシュ中に送信される。私はアクションフックについて間違ったことを理解していたに違いない...どんな考え?woocommerceオーダーリストの操作ボタンをクリックすると

ここのfunctions.phpで使用されるコードされています。ボタンがあるので、とき

// Add your custom order status action button (for orders with "processing" status) 
add_filter('woocommerce_admin_order_actions', 'add_custom_email_button', 100, 2); 
function add_custom_email_button($actions, $order) { 
// Display the button for all orders that have a 'processing' status 



    // Get Order ID (compatibility all WC versions) 
    $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 

    global $wpdb; 
    $deltime = $wpdb->get_var("SELECT meta_value FROM wp_postmeta WHERE post_id = $order_id AND meta_key = 'is_gift' "); 

    if ($order->has_status(array('processing')) && (!empty($deltime))) { 

    $mailer = WC()->mailer(); 
$mails = $mailer->get_emails(); 
if (! empty($mails)) { 
foreach ($mails as $mail) { 
    if ($mail->id == 'customer_processing_order') { 
     $mail->trigger($order_id); 
    } 
} 
} 



    // Set the action button 
    $actions['partial'] = array(
     'url'  => wp_nonce_url(admin_url('admin-ajax.php?action=add_custom_email_button&order_id=' . $order_id)), 
     'name'  => __('Αποστολή Email', 'woocommerce'), 
     'action' => "view partial", // keep "view" class for a clean button CSS 
    ); 
} 
return $actions; 
} 

// Set Here the WooCommerce icon for your action button 
add_action('admin_head', 'add_custom_email_button_css'); 
function add_custom_email_button_css() { 
echo '<style>.view.partial::after { font-family: woocommerce; content: "\1F4E7" !important; }</style>'; 
} 

button image

答えて

0

あなたはそれが、今、ボタンの定義とアクションを定義した方法は、同じ機能であります作成されると、そのアクションも実行されます。 ボタン定義とアクションを2つの異なる機能で分割する必要があります。

add_filter('woocommerce_admin_order_actions', 'add_custom_email_button', 100, 2); 
function add_custom_email_button($actions, $order) { 
    if ($order->status == "processing") { 
    $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 

    global $wpdb; 
    $deltime = $wpdb->get_var("SELECT meta_value FROM wp_postmeta WHERE post_id = $order_id AND meta_key = 'is_gift' "); 

    if (!empty($deltime)) { 
     // Set the action button 
     $actions['partial'] = array(
     'url'  => wp_nonce_url(admin_url('admin-ajax.php?action=click_custom_email_button&order_id=' . $order_id)), 
     'name'  => __('Αποστολή Email', 'woocommerce'), 
     'action' => "view partial", // keep "view" class for a clean button CSS 
    ); 
    } 
    } 
    return $actions; 
} 

add_action('wp_ajax_click_custom_email_button', 'click_custom_email_button'); 
function click_custom_email_button($order_id) { 
    $mailer = WC()->mailer(); 
    $mails = $mailer->get_emails(); 
    if (! empty($mails)) { 
    foreach ($mails as $mail) { 
     if ($mail->id == 'customer_processing_order') { 
     $mail->trigger($order_id); 
     } 
    } 
    } 
} 
+0

ありがとうございます。残念ながら、それは動作しませんでした。私が使用した 'admin_head'の代わりに、 'wp_ajax_click_custom_email_button'というadd_actionタグがあるため、上記のコードではボタンが表示されません。 –

+0

私は最初の 'admin_head'に戻しましたが、注文リストにアクセスすると自動的に実行されます。ボタン画像:(https://i.stack.imgur.com/Ite5V.png) –

+0

あなたはどのバージョンのWordPressとWooCommerceを使用していますか?これがどのように動作するのかの基礎は2段階のシステムです。 1つの関数は、ボタンを表示し、ボタンがクリックされたときに実行されるアクションを定義するために使用され、2つ目の関数は実際にアクションを実行します。また、最初のものはフィルタフックで、2番目のものはアクションフックです。 'admin_head'というフィルタフックはありません。特別なアクションボタンをオーダーリストに追加するための正しいフィルターは 'woocommerce_admin_order_actions'です。あなたはAJAXアクションを定義したので、あなたのために作成されたアクションは 'wp_ajax_ ' –

関連する問題