WooCommerce 使產品屬性可連結,給產品屬性新增上可點選的自定義連結。

  • 該話題包含 0 個回覆,1 人參與,最後由詩語 更新於 7 年前
正在檢視 1 個帖子:1-1 (共 1 個帖子)
  • 作者
    帖子
  • 詩語
    管理員
    • 文章數量: 5,930
    @feibisi
    樓主

    預設情況下,WooCommerce 將從屬性值中剝離所有 HTML,這意味著您不能使用 HTML 錨來使產品屬性可連結。儘管使用一些自定義程式碼,我們仍然可以做到。

    幸運的是,WordPress 現在可以幫助我們在 WordPress 4.4 中引入術語元組。

    
    /**
     * Register term fields
     */
    add_action( 'init', 'register_attributes_url_meta' );
    function register_attributes_url_meta() {
            $attributes = wc_get_attribute_taxonomies();
    
            foreach ( $attributes as $tax ) {
                $name = wc_attribute_taxonomy_name( $tax->attribute_name );
    
                add_action( $name . '_add_form_fields', 'add_attribute_url_meta_field' );
                add_action( $name . '_edit_form_fields', 'edit_attribute_url_meta_field', 10 );
                add_action( 'edit_' . $name, 'save_attribute_url' );
                add_action( 'create_' . $name, 'save_attribute_url' );
            }
    }
    
    /**
     * Add term fields form
     */
    function add_attribute_url_meta_field() {
    
        wp_nonce_field( basename( __FILE__ ), 'attrbute_url_meta_nonce' );
        ?>
    
        <div class="form-field">
            <label for="attribute_url"><?php _e( 'URL', 'domain' ); ?></label>
            <input type="url" name="attribute_url" id="attribute_url" value="" />
        </div>
        <?php
    }
    /**
     * Edit term fields form
     */
    function edit_attribute_url_meta_field( $term ) {
        $url = get_term_meta( $term->term_id, 'attribute_url', true );
        wp_nonce_field( basename( __FILE__ ), 'attrbute_url_meta_nonce' );
        ?>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="attribute_url"><?php _e( 'URL', 'domain' ); ?></label></th>
            <td>
                <input type="url" name="attribute_url" id="attribute_url" value="<?php echo esc_url( $url ); ?>" />
            </td>
        </tr>
        <?php
    }
    /**
     * Save term fields
     */
    function save_attribute_url( $term_id ) {
        if ( ! isset( $_POST['attribute_url'] ) || ! wp_verify_nonce( $_POST['attrbute_url_meta_nonce'], basename( __FILE__ ) ) ) {
            return;
        }
        $old_url = get_term_meta( $term_id, 'attribute_url', true );
        $new_url = esc_url( $_POST['attribute_url'] );
        if ( ! empty( $old_url ) && $new_url === '' ) {
            delete_term_meta( $term_id, 'attribute_url' );
        } else if ( $old_url !== $new_url ) {
            update_term_meta( $term_id, 'attribute_url', $new_url, $old_url );
        }
    }
    /**
     * Show term URL
     */
    add_filter( 'woocommerce_attribute', 'make_product_atts_linkable', 10, 3 );
    function make_product_atts_linkable( $text, $attribute, $values ) {
        $new_values = array();
        foreach ( $values as $value ) {
            if ( $attribute['is_taxonomy'] ) {
                $term = get_term_by( 'name', $value, $attribute['name'] );
                $url = get_term_meta( $term->term_id, 'attribute_url', true );
                if ( ! empty( $url ) ) {
                    $val = '<a href="' . esc_url( $url ) . '" title="' . esc_attr( $value ) . '">' . $value . '</a>';
                    array_push( $new_values, $val );
                } else {
                    array_push( $new_values, $value );
                }
            } else {
                $matched = preg_match_all( "/\[([^\]]+)\]\(([^)]+)\)/", $value, $matches );
                if ( $matched && count( $matches ) == 3 ) {
                    $val = '<a href="' . esc_url( $matches[2][0] ) . '" title="' . esc_attr( $matches[1][0] ) . '">' . sanitize_text_field( $matches[1][0] ) . '</a>';
                    array_push( $new_values, $val );
                } else {
                    array_push( $new_values, $value );
                }
            }
        }
        $text = implode( ', ', $new_values );
        return $text;
    }

    將網址新增到您的產品屬性
    在 WooCommerce 中,我們有兩種型別的產品屬性:

    全球產品屬性
    本地產品屬性
    有什麼不同?

    全球產品屬性是從產品> 屬性建立的,它們是分類法,如產品類別或帖子標籤。任何產品都可以使用在此定義的屬性,這就是為什麼它們是全球性的。

    相反,本地產品屬性是在產品中定義的,而在建立/編輯它的屬性標籤中定義。它們僅在已定義的產品上可用,並且不能被其他產品使用。

    由於這種差異,我們需要使用兩種不同的方式將 URL 新增到產品屬性字詞中。

    在 wp-content / themes / your-child-theme-name /中開啟你的 functions.php 檔案,並在檔案末尾新增下面的程式碼:

    這段程式碼做了什麼?

    從第 1 行到第 67 行,程式碼在表單中新增一個名為 URL 的欄位,用於建立和編輯全域性屬性。功能是註冊該欄位,將其新增到新增新術語表單中,將其新增到編輯術語表單中並儲存該欄位。

    從第 69 行到檔案末尾,它會調整單個產品頁面中附加資訊選項卡中的值,以處理產品屬性項的 URL 。這個函式也處理本地屬性的 URL 。

    如何使用產品屬性 URL
    對於全域性屬性,請轉至產品> 屬性並新增一個新屬性,或編輯現有屬性。它會出現在右邊的表格中。點選齒輪圖示新增產品屬性條款,你會看到這樣的形式:

    如何使用產品屬性 URL
    對於全域性屬性,請轉至產品> 屬性並新增一個新屬性,或編輯現有屬性。它會出現在右邊的表格中。點選齒輪圖示新增產品屬性條款,你會看到這樣的形式:

    正如您所看到的,表單新增新設計器 (設計器是我用於此示例的屬性的名稱) 表單的底部有一個新的欄位,名為 URL 。在那裡寫下你想要的產品屬性條款連結到的 URL 。

    編輯產品屬性字詞時可以使用相同的欄位:

    對於本地屬性,我們沒有這樣的介面,所以我們必須找到一種解決方法來將 URL 新增到產品屬性條款中。

    你知道我喜歡 Markdown 嗎?這就是我們要使用的本地屬性 Markdown!

    從產品> 所有產品編輯產品,然後開啟 「 屬性」 選項卡。從這裡新增一個新的自定義產品屬性。然後給它一個名字並使用 Markdown 新增一些值來建立連結:

    [Text shown](https://myurl.com)

    這是使用的格式。在此螢幕截圖中顯示了一個示例:

    這裡的第一個屬性 Logo 是本地屬性,第二個屬性 Designer 是全域性屬性。

    現在點選儲存屬性併發布您的產品。這是它將如何使用主題店面在網站上顯示:

    感謝 James 在這篇文章中成為我的榜樣。我不能用我的名字,因為稱我為設計師是一種恥辱。

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

話題資訊

  • 當前位於:WooCommerce
  • 0 條回覆
  • 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/25609/" title="WooCommerce 使產品屬性可連結,給產品屬性新增上可點選的自定義連結。">7 年前</a>