WooCommerce 外鏈產品按鈕新標籤頁開啟的方法 External product add to cart

正在檢視 3 個帖子:1-3 (共 3 個帖子)
  • 作者
    帖子
  • 詩語
    管理員
    • 文章數量: 5,933
    @feibisi
    樓主

    先說下為啥要弄這麼複雜,因為模板商店需要上架一批模板內容,就拿 divi 和 avada 來說,如果是程式本身內建的,那麼我們需要提供這些演示模板的下載地址。

    也是上圖中的 【去商城購買】 按鈕,還是由於想要借鑑過去的經驗,所以這幾天都有種處處碰壁的感覺。目的是想著如何才能讓使用者訂閱了我們的模板商店後直接下載到全部的產品不用再去一個一個的購買了。

    嘗試了幾種方式都不太合適,還是想著換其他的外掛功能來吧。結果是找了一大圈都沒個合適的。

    睡了一覺想了下,如果此路不通,那麼幹脆換條新路線,直接將 WooCommerce 這些免費提供的模板給打包提供下載算了,然後想辦法給使用者下載許可權加個驗證。

    然後把全站的資源再新增到一個新的品類裡,當使用者訂閱了套餐之後,就可以直接去下載相關的產品。


    對於產品外鏈的話,WooCommerce 外鏈產品按鈕預設是在當前頁面開啟,不是很適合中國人的習慣,所以我們調整成了新標籤頁開啟,如下,是直接修改的模板,加了個 target="_blank" 標籤。

    <?php
    /**
     * External product add to cart
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/external.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you
     * (the theme developer) will need to copy the new files to your theme to
     * maintain compatibility. We try to do this as little as possible, but it does
     * happen. When this occurs the version of the template file will be bumped and
     * the readme will list any important changes.
     *
     * @see https://docs.woocommerce.com/document/template-structure/
     * @package WooCommerce\Templates
     * @version 3.4.0
     */
    
    defined( 'ABSPATH' ) || exit;
    
    do_action( 'woocommerce_before_add_to_cart_form' ); ?>
    
    <form class="cart" action="<?php echo esc_url( $product_url ); ?>" target="_blank" method="get">
    	<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
    
    	<button type="submit" target="_blank"class="single_add_to_cart_button button alt"><?php echo esc_html( $button_text ); ?></button>
    
    	<?php wc_query_string_form_fields( $product_url ); ?>
    
    	<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
    </form>
    
    <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>

    本身是有外掛可以用的,主要是找了幾個外掛和其他的程式碼段用了之後會覆蓋掉原有的設計樣式,這樣就有些討厭了,那麼反正都是自己做的主題產品,就乾脆寫到模板檔案裡吧。

    其他外掛下樓列出。

    詩語
    管理員
    • 文章數量: 5,933
    @feibisi
    樓主

    需要外掛實現 WooCommerce 外鏈產品按鈕新標籤頁開啟的可以搜尋

    WooCommerce External Product New Tab

    或者這裡下載

    WooCommerce External Product New Tab WooCommerce 外部產品新標籤

    詩語
    管理員
    • 文章數量: 5,933
    @feibisi
    樓主

    除了修改模板和外掛外,再貼一段程式碼

    remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open');
    add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 15);
    add_action('woocommerce_before_shop_loop_item', 'woocommerce_add_aff_link_open', 10);
    add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_add_aff_link_close', 10);
    
    function woocommerce_add_aff_link_open(){
    $product = wc_get_product(get_the_ID());
    
    if( $product->is_type( 'external' ) ) {
    echo '<a class="" href="' . $product->get_product_url() . '" target="_blank" rel="noopener">';
    }
    }</a>
    
    function woocommerce_add_aff_link_close(){
    $product = wc_get_product(get_the_ID());
    
    if( $product->is_type( 'external' ) ) {
    echo '';
    }
    }

    實現效果相同,就是這個程式碼段會過濾掉按鈕樣式。

正在檢視 3 個帖子:1-3 (共 3 個帖子)
  • 哎呀,回覆話題必需登入。

話題資訊

  • 當前位於:WooCommerce
  • 2 條回覆
  • 1 個參與人
  • 最後回覆:<a href="https://bbs.weixiaoduo.com/users/feibisi/" title=" 檢視詩語的個人資料" class="bbp-author-link"><span class="bbp-author-name"> 詩語</span></a>
  • 上次活動:<a href="https://bbs.weixiaoduo.com/topic/38643/#post-38645" title=" 回覆至:WooCommerce 外鏈產品按鈕新標籤頁開啟的方法 External product add to cart">4 年、 1 月前</a>