【问题标题】:Hide a custom action button on click displaying a text in WooCommerce admin orders list在 WooCommerce 管理员订单列表中单击显示文本时隐藏自定义操作按钮
【发布时间】:2020-08-21 19:05:46
【问题描述】:

我正在创建 woocommerce 插件以通过 WhatsApp 发送订单详细信息。这是我的插件代码

add_filter( 'manage_edit-shop_order_columns', 'dvs_whatsapp_msg_list_column' );
function dvs_whatsapp_msg_list_column( $columns ) {
    $columns['dvs_show_whatsapp'] = 'WhatsApp';
    return $columns;
}
 
add_action( 'manage_shop_order_posts_custom_column', 'dvs_whatsapp_msg_list_column_content' );
function dvs_whatsapp_msg_list_column_content( $column ) {
    global $post;
    if ( 'dvs_show_whatsapp' === $column ) {
        $order = wc_get_order( $post->ID );
        $firstname = $order->get_billing_first_name();
        $lastname = $order->get_billing_last_name();
        $phone = $order->get_billing_phone();
        $ordernum = $order->get_order_number();
        $total = $order->get_total();
        $payment = $order->get_payment_method_title();
        $country = $order->get_billing_country();
        $calling_code = WC()->countries->get_country_calling_code($country);
        $whatsappnum = $calling_code.$phone;

        $msg = 'Hello ' .$firstname. ' ' .$lastname. ', your order #' .$ordernum. ' has been received. The order amount is ' .$total. '. Your payment method is ' .$payment.  '. Please contact us if you have any question regarding your order. Thank you.';
        
echo '<a href="https://wa.me/' .$whatsappnum. '?text=' .urlencode($msg).'" target="blank" class="dvs-whatsapp-btn">Send WhatsApp</a>';
    }
}

这是输出

我希望当商店经理或管理员点击发送 Whatsapp 链接时,它将隐藏链接并显示已发送的消息,以便商店经理或管理员可以知道此消息的详细信息已发送。

请帮忙。

【问题讨论】:

  • 我建议在点击链接时通过 AJAX 更新订单元数据。在订单元中存储类似于whatsapp_link_sent: 1 的内容。然后将列中的输出基于该值。
  • @Terminator-Barbapapa 你的意思是有效的先生,它应该像 update_post_meta( $order->id, '_dvs_whatsapp_order_link', '1' );然后在函数中我可以使用条件但是如何在点击链接时更新_post_meta?
  • 您可以使用 jQuery 检测链接何时被单击并触发 AJAX 调用,该调用将更新订单元数据并通过回调函数将您的链接更改为“链接已发送”。查看using AJAX in plugins 上的 WordPress 文档。

标签: php wordpress woocommerce backend orders


【解决方案1】:

Javascript 不是实现这一点的方法。单击外部链接后,您将使用以下内容隐藏链接并显示“已发送消息”:

add_filter( 'manage_edit-shop_order_columns', 'dvs_whatsapp_msg_list_column' );
function dvs_whatsapp_msg_list_column( $columns ) {
    $columns['whatsapp'] = __('WhatsApp', 'woocommerce');
    return $columns;
}

add_action( 'manage_shop_order_posts_custom_column', 'dvs_whatsapp_msg_list_column_content' );
function dvs_whatsapp_msg_list_column_content( $column ) {
    if ( 'whatsapp' === $column ) {
        global $the_order;

        if( ! $the_order->get_meta('_wapp_sent') ) {
            echo '<a href="?post_type=shop_order&send=dvs_whatsapp&order_id=' . $the_order->get_id() .' target="blank" class="dvs-whatsapp button">' . __("Send WhatsApp") . '</a>';
        }
        else {
            echo __("Message sent", "woocommerce");
        }
    }
}

add_action( 'admin_init', 'dvs_redirect_whatsapp_send' );
function dvs_redirect_whatsapp_send() {
    global $pagenow;

    # Check current admin page.
    if ( $pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'shop_order'
    && isset($_GET['send']) && $_GET['send'] == 'dvs_whatsapp' && isset($_GET['order_id']) && $_GET['order_id'] > 0 ) {
        $order = wc_get_order( $_GET['order_id'] );

        $msg = sprintf( __("Hello %s %s, your order #%s has been received. The order amount is %s. Your payment method is %s. %s", "woocommerce"),
            $order->get_billing_first_name(),
            $order->get_billing_last_name(),
            $order->get_order_number(),
            $order->get_total(),
            $order->get_payment_method_title(),
            __("Please contact us if you have any question regarding your order. Thank you.", "woocommerce")
        );

        $whatsapp_num = WC()->countries->get_country_calling_code( $order->get_billing_country() ) . $order->get_billing_phone();

        update_post_meta( $_GET['order_id'], '_wapp_sent', 'true' ); // Mark order as WhatsApp message sent

        wp_redirect( 'https://wa.me/' . $whatsappnum . '?text=' . urlencode($msg) ); // Redirect to WhatsApp sending service
        exit;
    }
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

【讨论】:

  • 谢谢,代码很完美。但最后一件事我们可以在新标签中打开whatsapp链接吗?比如 targer="blank"
  • 谢谢,我已经添加了。先生,您的技能很棒 if( !$the_order->get_meta('_wapp_sent') ) { echo ''。 __("发送 WhatsApp") 。 '';
【解决方案2】:

我相信这应该可以解决问题:

要插入页面的 jQuery

jQuery('.dvs-whatsapp-btn').click(function(){
    jQuery('<span class="link-clicked">Link clicked!</span>').insertAfter('.dvs-whatsapp-btn');
    jQuery('.dvs-whatsapp-btn').hide();
});

【讨论】:

  • 不,先生,它显示这个:i.imgur.com/3MNdPxm.png 并且当我们刷新页面时它再次显示发送whatsapp链接。
  • 如果您希望页面始终显示链接在被点击后被点击(并且不会在刷新时更改),那么您看到的内容与我发送的内容完全不同……您将需要添加大量自定义 PHP、一个新的数据库列和一个处理来自页面的输入的函数。因此,在这种情况下,您根本不会查看 jQuery,而是查看 something like this(自定义 WordPress 管理页面输入处理程序)。不幸的是,这超出了 stackexchange 的范围。
猜你喜欢
  • 2018-01-12
  • 2018-07-02
  • 2021-09-01
  • 2021-04-15
  • 2018-04-27
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多