2016-09-17 39 views
3

我有一個woocommerce網站。首先,我想在管理產品頁面上添加一個自定義字段,以設置exernal url,我將在我的Archives類別產品頁面上使用該字段。將自定義字段外部鏈接添加到檔案類別頁

另外我希望理想的情況是在我的管理產品頁面設置元框中有這個自定義字段。但是我已經更改了所有存檔頁面上的鏈接。

現在我有這個代碼是沒有做什麼,我需要:

remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10); 
add_action('woocommerce_before_shop_loop_item', 'mycode_woocommerce_template_loop_product_link_open', 20); 
function mycode_woocommerce_template_loop_product_link_open() { 

    $url = 'https://www.some_domain.com/'; 

    echo '<a href="' . $url . '">'; 

} 

我該怎麼做才能讓它只類檔案網頁上運作?

感謝

回答

4

第一步 - 在管理產品頁面自定義字段的創建設置metabox:

enter image description here

// Inserting a Custom Admin Field 
add_action('woocommerce_product_options_general_product_data', 'add_custom_text_field_create'); 
function add_custom_text_field_create() { 
    echo '<div class="options_group">'; 

    woocommerce_wp_text_input(array(
     'type'    => 'text', 
     'id'    => 'extern_link', 
     'label'    => __('External Link', 'woocommerce'), 
     'placeholder'  => '', 
     'description'  => __('Insert url', 'woocommerce'), 
    )); 

    echo '</div>'; 
} 

// Saving the field value when submitted 
add_action('woocommerce_process_product_meta', 'add_custom_field_text_save'); 
function add_custom_field_text_save($post_id){ 
$wc_field = $_POST['extern_link']; 
if(!empty($wc_field)) 
    update_post_meta($post_id, 'extern_link', esc_attr($wc_field)); 
} 

第2步 - 通過自定義的meta值更換鏈接僅在產品類別檔案頁面中。

remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open'); 
add_action('woocommerce_before_shop_loop_item', 'custom_wc_template_loop_product_link_open', 10); 
function custom_wc_template_loop_product_link_open() { 
    // For product category archives pages only. 
    if (is_product_category()) { 
     // You get here your custom link 
     $link = get_post_meta(get_the_ID(), 'extern_link', true); 
     echo '<a href="' . $link . '" class="woocommerce-LoopProduct-link">'; 
    //For the other woocommerce archives pages 
    } else { 
     echo '<a href="' . get_the_permalink() . '" class="woocommerce-LoopProduct-link">'; 
    } 
} 

的代碼放在你的活躍兒童主題(或主題)的function.php文件或也以任何插件文件。

此代碼經過測試和工程

相關問題