将操作按钮添加到Woocommerce我的在新窗口中打开的帐户订单 [英] Add an action button to Woocommerce my account orders that open in a new window

查看:52
本文介绍了将操作按钮添加到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 );

如何向每个其他自定义按钮添加 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 变量.


要仅定位目标订单,只需在第一个函数中添加 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;
}

这篇关于将操作按钮添加到Woocommerce我的在新窗口中打开的帐户订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆