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

2022-01-13 00:00:00 attributes jquery php wordpress woocommerce

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

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

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

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

推荐答案

使用以下内容将向每个特定操作 html 标记添加属性 target_blank 值在新窗口中打开链接:

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
}

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

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

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

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

<小时>

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


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

相关文章