【问题标题】:Open External WooCommerce Products in New Tabs - Adding Attribute target="_blank"在新选项卡中打开外部 WooCommerce 产品 - 添加属性 target="_blank"
【发布时间】:2016-10-14 14:01:15
【问题描述】:

我已设法将属性 target="_blank" 添加到我的外部产品页面,但我似乎无法更改父(分组产品)页面上的链接。

我可以通过修改 external.php 并将标签添加到实际链接本身来做到这一点。

<p class="cart">
<?php sdnetwork(); sdcondition(); parent_permalink_button(); ?><a href="<?php echo esc_url( $product_url ); ?>" target="_blank" rel="nofollow" class="single_add_to_cart_button button alt"><img src="/wp-content/themes/wootique-child/images/icons/new_tab_icon.gif" alt="Opens in New Tab"> <?php echo esc_html( $button_text ); ?></a>
</p>

如何更改分组产品父页面上的链接添加此属性,我首先想到的是修改grouped.php但链接生成方式不同。

<?php woocommerce_template_loop_add_to_cart(); ?>

我怎样才能将我的标签添加到上面生成的链接中?我曾考虑过使用钩子,但我需要一些帮助。

编辑:

只是想知道我是否可以像这样使用 jQuery .....

jQuery(document).ready(function($) {
  $(".button.product_type_external").each(function() {
    $(this).find("a").attr("target", "_blank");
  });
});

问题是大多数链接在页面加载时都被隐藏了,我担心这可能会占用大量资源,还是会这样吗?对 jQuery 很陌生。

http://mobilereactor.co.uk/shop/mobile-phones/sony-xperia-z5-compact-coral-deals/

编辑由于 cal_b 解决了:

add_filter( 'woocommerce_loop_add_to_cart_link', 'add_target_blank', 10, 2 );

function add_target_blank( $link, $product ){
    global $post;
    $product = get_product( $post->ID );
        if( $product->is_type( 'external' ) ){
            // I simply added target="_blank" in the line below
            $link = sprintf( '<a rel="nofollow" href="%s" target="_blank" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
                    esc_url( $product->add_to_cart_url() ),
                    esc_attr( isset( $quantity ) ? $quantity : 1 ),
                    esc_attr( $product->id ),
                    esc_attr( $product->get_sku() ),
                    esc_attr( isset( $class ) ? $class : 'button' ),
                    esc_html( $product->add_to_cart_text() )
                );
            return $link;
        } else {
            // I simply remove target="_blank" in the line below
            $link = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
                    esc_url( $product->add_to_cart_url() ),
                    esc_attr( isset( $quantity ) ? $quantity : 1 ),
                    esc_attr( $product->id ),
                    esc_attr( $product->get_sku() ),
                    esc_attr( isset( $class ) ? $class : 'button' ),
                    esc_html( $product->add_to_cart_text() )
                );
            return $link;
        }
}

【问题讨论】:

  • 不要使用 jQuery。有一个模板可以让您覆盖链接,就像您的 external.php 一样。您已经彻底修改了您链接到的模板,因此您需要为我们提供更多工作。我们在您上面的链接中查看的是哪个模板?
  • 在上面的链接中,它是 grouped.php 的修改版本
  • 我认为链接是由&lt;?php woocommerce_template_loop_add_to_cart(); ?&gt;在grouped.php中生成的,虽然不知道如何修改它?

标签: php jquery wordpress woocommerce hook


【解决方案1】:

这是将target="_blank" 添加到 add_to_cart 链接以在新标签页中打开它们的更简洁的方法:

function ns_open_in_new_tab($args, $product) 
{
    if( $product->is_type('external') ) {
        // Inject target="_blank" into the attributes array
        $args['attributes']['target'] = '_blank';
    }    

    return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );

ns_ 部分替换为您自己的命名空间缩写。

【讨论】:

    【解决方案2】:

    如果你跟踪代码,那个函数会做一些事情,然后加载模板loop/add-to-cart.php

    如果你打开loop/add-to-cart.php,你会发现代码应该是这样的:

    echo apply_filters( 'woocommerce_loop_add_to_cart_link',
        sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            esc_attr( $product->id ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $class ) ? $class : 'button' ),
            esc_html( $product->add_to_cart_text() )
        ),
    $product );
    

    要修改链接,请使用过滤器 (woocommerce_loop_add_to_cart_link)。

    注意:切勿直接在插件文件夹中修改 WooCommerce 模板。使用他们出色的Template Structure 将模板复制到您的主题并在那里进行修改,否则您的更改将在您下次更新 WooCommerce 时丢失。

    最后一点:跟踪代码很容易,并且在使用好的 IDE 时会很有趣。我是 PHPStorm 的忠实粉丝 (https://www.jetbrains.com/phpstorm/)。

    编辑

    为外部产品添加它,然后你会采取不同的方法。

    您将利用过滤器,正如您在 cmets 中提到的那样,在您的 functions.php 中编写一个函数,执行如下操作:

    add_filter('woocommerce_loop_add_to_cart_link', 'my_external_product_links', 10, 2);
    
    function my_external_product_links( $link, $product ) {
        // Set up the $target variable to contain the correct text depending on the product
        $target = ( 'external' == $product->product_type  ) ? 'target="_blank"' : '';
        // Use the code from the core function here, but with our modification to include target
        echo sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s" %s>%s</a>',
                esc_url( $product->add_to_cart_url() ),
                esc_attr( isset( $quantity ) ? $quantity : 1 ),
                esc_attr( $product->id ),
                esc_attr( $product->get_sku() ),
                esc_attr( isset( $class ) ? $class : 'button' ),
                esc_html( $product->add_to_cart_text() ),
                $target
            );
    }
    

    在上面的代码中,密切关注sprintf语句中的新%s

    // Watch for this --->--->--->--->--->--->--->--->--->--->--->--->--->--->--->--->--->--->-->--->--->-vv
    '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s" %s>
    

    【讨论】:

    • 这非常有帮助,谢谢,如果我在functions.php 中创建一个仅echo target="_blank" 的函数,对于外部产品,这是否是正确的做法?因为我认为这也管理单个产品??
    • 我认为使用您的信息 function target_blank() { global $post; $product = get_product( $post-&gt;ID ); if( $product-&gt;is_type( 'external' ) ){ echo 'target="_blank"'; } } 会更简单,然后只需将 target_blank(); 弹出到 add-to-cart.php 模板中 - 它会起作用吗??
    • 刚刚试了没用,谢谢你的帮助,非常感谢
    • 所以添加过滤器应该是add_filter('add_to_cart', 'my_external_product_links');这部分我不太明白,_blank是如何添加的?
    • 我尝试将 _blank 添加到 add-to-cart.php 中,但这会导致所有 woocom 按钮都在新标签页中打开,猜测它是高速路
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多