duancai9010 2019-05-15 07:10
浏览 163
已采纳

在Woocommerce中添加一个操作按钮,我的帐户订单在新窗口中打开

I'm going to add a button from my account order to make it go to a specific url. I made a button, but I want to open it with a new window instead of going to the page. What should I do? Down below is the code I added, and I'd like to open url here with a cage:

function sv_add_my_account_order_actions( $actions, $order ) {

    $actions['name'] = array(
        'url'  => 'the_action_url',
        'name' => 'The Button Text',
    );
    return $actions;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'sv_add_my_account_order_actions', 10, 2 );

how to add target="_blank" to each additional custom button?

  • 写回答

2条回答 默认 最新

  • dpbe81245 2019-05-15 08:15
    关注

    Use the following that will add to each specific action html tag the attribute target with a _blank value opening the link in a new window:

    // Your additional action button
    add_filter( 'woocommerce_my_account_my_orders_actions', 'add_my_account_my_orders_custom_action', 10, 2 );
    function add_my_account_my_orders_custom_action( $actions, $order ) {
        $action_slug = 'specific_name';
    
        $actions[$action_slug] = array(
            'url'  => home_url('/the_action_url/'),
            'name' => 'The Button Text',
        );
        return $actions;
    }
    
    // Jquery script
    add_action( 'woocommerce_after_account_orders', 'action_after_account_orders_js');
    function action_after_account_orders_js() {
        $action_slug = 'specific_name';
        ?>
        <script>
        jQuery(function($){
            $('a.<?php echo $action_slug; ?>').each( function(){
                $(this).attr('target','_blank');
            })
        });
        </script>
        <?php
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    You will have to set the same unique and explicit $action_slug variable in both functions.


    To target orders complete only, add if ( $order->has_status('completed') ) { in the first function only like:

    add_filter( 'woocommerce_my_account_my_orders_actions', 'add_my_account_my_orders_custom_action', 10, 2 );
    function add_my_account_my_orders_custom_action( $actions, $order ) {
        if ( $order->has_status( 'completed' ) ) {
            $action_slug = 'specific_name';
    
            $actions[$action_slug] = array(
                'url'  => home_url('/the_action_url/'),
            'name' => 'The Button Text',
            );
        }
        return $actions;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿