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>