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

Jei*_*eil 4 php wordpress jquery attributes woocommerce

我将从我的帐户订单中添加一个按钮,使其转到特定的网址。我制作了一个按钮,但我想用新窗口打开它而不是转到页面。我应该怎么办?下面是我添加的代码,我想用笼子打开这里的网址:

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 );
Run Code Online (Sandbox Code Playgroud)

如何添加target="_blank"到每个附加的自定义按钮?

Loi*_*tec 6

使用以下命令将属性添加到每个特定操作 html 标记中target,并_blank在新窗口中打开链接:

// 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
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

您必须$action_slug在两个函数中设置相同的唯一且显式的变量。


要仅完成目标订单,请if ( $order->has_status('completed') ) {仅在第一个函数中添加,例如:

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;
}
Run Code Online (Sandbox Code Playgroud)