单击上的woocommerce订单列表操作按钮

时间:2017-11-14 12:09:19

标签: wordpress button woocommerce hook action

我正在尝试在woocommerce订单列表中添加一个自定义按钮,该按钮将向客户发送一封电子邮件(该按钮仅显示在状态为'processing'且自定义字段'deltime'不为空的订单旁边)。所以我得到了显示的按钮,但每次我在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

1 个答案:

答案 0 :(得分:0)

现在定义它的方式,按钮定义和操作属于同一个功能,因此当创建按钮时,也会执行其操作。 您必须将按钮定义和操作拆分为两个不同的功能:

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 );
      }
    }
  }
}