• Home
    • English
    • 中文
  • About Us
  • Services
    • SEO Services
    • Website Design Service
  • Projects
  • Docs
  • Blog
    • Affiliate
    • Ecommerce
    • Frontend
    • linux
      • nginx
    • PHP
      • Magento
      • wordpress
    • Python
    • SEO
    • Web
  • Contact Us

Subscribe to Updates

Get the latest creative news from FooBar about art, design and business.

What's Hot

Design a plugin for wordpress woocommerce to display a tab to show attachment download

2024-04-06

TranslatePress v2.6.9 – WordPress Translation Plugin

2023-12-25

A Linux batch script converting pictures to webp format

2023-07-10
Facebook Twitter Instagram
  • 中文
  • English
Facebook Twitter Instagram Pinterest VKontakte
Weilai Tech Weilai Tech
  • Home
    • English
    • 中文
  • About Us
  • Services
    • SEO Services
    • Website Design Service
  • Projects
  • Docs
  • Blog
    • Affiliate
    • Ecommerce
    • Frontend
    • linux
      • nginx
    • PHP
      • Magento
      • wordpress
    • Python
    • SEO
    • Web
  • Contact Us
Weilai Tech Weilai Tech
Home»WooCommerce»Design a plugin for wordpress woocommerce to display a tab to show attachment download
WooCommerce

Design a plugin for wordpress woocommerce to display a tab to show attachment download

OxfordBy Oxford2024-04-06No Comments2 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email
Share
Facebook Twitter LinkedIn Pinterest Email

Design a plugin for wordpress woocommerce to display a tab to show attachment download

You can add the attachement tab in the product edit page.

Demo Link: https://www.shopym.com/product/amd-fd6350frhkbox-fx-6350-fx-series-6-core-black-edition/

add a custom_field ,  spare it with   link|description and commas,   the default description is the file name.

save below code to a .php file, and upload it to the  /wp-content/plugin

<?php
/*
Plugin Name: Your WooCommerce Product Attachment Plugin
Description: This plugin allows you to upload and display attachments on product pages in WooCommerce.
Version: 1.0
Author: Shopaii
Author URI: https://www.shopaii.com
License: GPL2
*/

// 插件其他功能代码...

// 在产品编辑页面添加自定义字段
add_action('woocommerce_product_options_general_product_data', 'custom_attachment_field');
function custom_attachment_field() {
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_attachment_field',
            'label' => __('Attachment URLs', 'text_domain'),
            'desc_tip' => 'true',
            'description' => __('Enter attachment URLs|Descriptions separated by commas ', 'text_domain')
        )
    );
}

// 保存自定义字段的值
add_action('woocommerce_process_product_meta', 'save_custom_attachment_field');
// 保存自定义字段的值为二维数组
function save_custom_attachment_field($product_id) {
    $custom_attachment = sanitize_text_field($_POST['_custom_attachment_field']);
    $attachment_array = array_map('trim', explode(',', $custom_attachment));
    $attachment_data = array();
    foreach ($attachment_array as $attachment) {
        $attachment_parts = explode('|', $attachment); // 按照 | 分割链接和说明
        $url = trim($attachment_parts[0]); // 链接
        $description = isset($attachment_parts[1]) ? trim($attachment_parts[1]) : basename($url); // 说明,默认为文件名
        $attachment_data[] = array(
            'url' => $url,
            'description' => $description
        );
    }
    update_post_meta($product_id, '_custom_attachment_field', $attachment_data);
}




// JavaScript 上传文件并更新自定义字段
add_action('admin_footer', 'custom_attachment_js_code');
function custom_attachment_js_code() {
?>
<script>
    jQuery(document).ready(function($) {
        $(document).on('change', 'input[type="file"]', function() {
            var attachmentData = new FormData();
            attachmentData.append('file', $(this)[0].files[0]);
            attachmentData.append('action', 'upload_attachment');
            $.ajax({
                type: 'POST',
                url: ajaxurl,
                data: attachmentData,
                contentType: false,
                processData: false,
                success: function(response) {
                    // 更新自定义字段的值
                    $('input#_custom_attachment_field').val(response.url);
                }
            });
        });
    });
</script>
<?php
}

// 在产品详情页添加自定义标签
add_filter('woocommerce_product_tabs', 'add_attachment_tab');
function add_attachment_tab($tabs) {
    global $product;

    // 获取附件信息
    $attachment_urls = get_post_meta($product->get_id(), '_custom_attachment_field', true);

    // 如果自定义附件字段不为空,则添加自定义标签
    if ($attachment_urls !== '') {
        $tabs['attachment_tab'] = array(
            'title' => __('Attachment', 'text_domain'),
            'priority' => 50, 
            'callback' => 'attachment_tab_content'
        );
    }

    return $tabs;
}


// 显示附件信息
function attachment_tab_content() {
    global $product;
    $attachment_data = get_post_meta($product->get_id(), '_custom_attachment_field', true);

    if (!empty($attachment_data)) {
        echo '<ul>';
        foreach ($attachment_data as $attachment) {
            echo '<li><a href="' . esc_url($attachment['url']) . '">' . $attachment['description'] . '</a></li>';
        }
        echo '</ul>';
    }
}



?>
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Avatar photo
Oxford

Related Posts

TranslatePress v2.6.9 – WordPress Translation Plugin

2023-12-25

Add custom field to Woocommerce tab

2022-03-07

Surror Product Tabs for WooCommerce

2022-03-07

make wordpress static with gatsby

2021-09-29
Recent Posts
  • Design a plugin for wordpress woocommerce to display a tab to show attachment download
  • TranslatePress v2.6.9 – WordPress Translation Plugin
  • A Linux batch script converting pictures to webp format
  • Hearing aid listed company official website SEO case
  • how to use docker to run php5.6 plus apache
April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  
« Dec    
Tags
app branding design digital Docly docs etc faq fix github Helpdesk Image issue magento Manual marketing memecached Photography planing seo sequrity tips Travel ui/ux web WordPress 爬虫
Editors Picks
About Us

Guangzhou Weilai Technology is a foreign trade integrated marketing service provider focusing on Google as the drainage center and marketing self-built website as the carrier.

Email Us: [email protected]
Contact: +86 18676917505

Facebook Pinterest YouTube LinkedIn
Recent Posts
  • Design a plugin for wordpress woocommerce to display a tab to show attachment download
  • TranslatePress v2.6.9 – WordPress Translation Plugin
  • A Linux batch script converting pictures to webp format
  • Hearing aid listed company official website SEO case
  • how to use docker to run php5.6 plus apache
From Flickr
Website Design Case
© 2024 Copyright by Guangzhou Weilai Technology Co.,Ltd..
  • Home
  • About Us
  • SEO Services
  • Website Design Service
  • Projects
  • Blog
  • Contact Us

Type above and press Enter to search. Press Esc to cancel.