✨专业 WordPress 开发,定制建站,高效上线,合作即享优化服务!🚀

WooCommerce 产品添加30天销量功能

功能逻辑:如果后台启用,并填写销量,前台根据后台填写显示。如果没有填写销量值,则会生成一个随机数(50-100)。可以选择启用或者关闭显示,把√ 去掉 前台就不显示了。

WooCommerce 产品添加30天销量功能 WooCommerce 产品添加30天销量功能

function add_custom_sales_meta_box() {
    add_meta_box(
        'custom_sales_meta_box',
        'Sales Display Settings',
        'render_custom_sales_meta_box',
        'product',
        'side',
        'default'
    );
}
add_action('add_meta_boxes', 'add_custom_sales_meta_box');

// 渲染自定义字段表单
function render_custom_sales_meta_box($post) {
    // 获取当前产品的元数据
    $sales_count = get_post_meta($post->ID, '_custom_sales_count', true);
    $sales_display = get_post_meta($post->ID, '_custom_sales_display', true);

    // 如果未设置显示控制,默认为 "显示" 状态
    if ($sales_display === '') {
        $sales_display = 'yes';
    }

    // 安全校验
    wp_nonce_field('custom_sales_meta_box_nonce', 'custom_sales_meta_box_nonce_field');

    // 显示表单
    echo '<p><label for="custom_sales_display">';
    echo '<input type="checkbox" name="custom_sales_display" id="custom_sales_display" value="yes" ' . checked($sales_display, 'yes', false) . ' />';
    echo ' Display Sales Text</label></p>';

    echo '<p><label for="custom_sales_count">Sales Count:</label><br />';
    echo '<input type="number" name="custom_sales_count" id="custom_sales_count" value="' . esc_attr($sales_count) . '" min="0" style="width: 100%;" /></p>';
}

// 保存自定义字段数据
function save_custom_sales_meta_box_data($post_id) {
    // 验证安全性
    if (!isset($_POST['custom_sales_meta_box_nonce_field']) || 
        !wp_verify_nonce($_POST['custom_sales_meta_box_nonce_field'], 'custom_sales_meta_box_nonce')) {
        return;
    }

    // 检查自动保存
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    // 检查用户权限
    if (isset($_POST['post_type']) && 'product' === $_POST['post_type'] && !current_user_can('edit_post', $post_id)) {
        return;
    }

    // 保存是否显示字段
    $sales_display = isset($_POST['custom_sales_display']) && $_POST['custom_sales_display'] === 'yes' ? 'yes' : '';
    update_post_meta($post_id, '_custom_sales_display', $sales_display);

    // 保存销量字段
    if (isset($_POST['custom_sales_count'])) {
        $sales_count = sanitize_text_field($_POST['custom_sales_count']);
        update_post_meta($post_id, '_custom_sales_count', $sales_count);
    }
}
add_action('save_post', 'save_custom_sales_meta_box_data');

// 在产品页面标题下方显示销售数据文案
function display_custom_sales_text() {
    global $post;

    // 获取元数据
    $sales_display = get_post_meta($post->ID, '_custom_sales_display', true);
    $sales_count = get_post_meta($post->ID, '_custom_sales_count', true);

    // 如果未开启显示功能,直接返回
    if ($sales_display !== 'yes') {
        return;
    }

    // 如果后台没有填写销量,生成随机数
    if (empty($sales_count)) {
        $sales_count = rand(50, 100);
    }

    // 输出 HTML 和内嵌 CSS
    echo '<style>
        .custom-sales-text {
            font-size: 16px;
            color: #333;
            margin-top: 10px;
            display: flex;
            align-items: center;
            font-weight: bold;
        }
        .custom-sales-text .icon {
            font-size: 24px;
            margin-right: 8px;
            animation: flame-breathe 1.5s infinite;
        }
        @keyframes flame-breathe {
            0% {
                transform: scale(1);
                opacity: 1;
            }
            50% {
                transform: scale(1.2);
                opacity: 0.8;
            }
            100% {
                transform: scale(1);
                opacity: 1;
            }
        }
        .custom-sales-text strong {
            font-size: 18px;
            color: #ff5722;
            margin-left: 4px;
        }
        .custom-sales-text span.highlight {
            font-size: 20px;
            font-weight: bold;
            color: #ff4500;
            margin-left: 5px;
        }
    </style>';

    // 输出文案内容
    echo '<p class="custom-sales-text">';
    echo '<span class="icon">🔥</span>';
    echo 'In the <strong>last 30 days</strong>, sold <span class="highlight">' . $sales_count . '</span> pieces!';
    echo '</p>';
}
add_action('woocommerce_single_product_summary', 'display_custom_sales_text', 6);

 

WordPress 技术支持团队
WordPress 技术支持团队

💻 一位真诚且细致入微的年轻开发者🎓,专注于外贸网站定制开发领域。
🌟 专业承接 WordPress 建站设计定制与全方位开发服务。我们的独特优势在于,不仅精通网站开发技术,更将国际化主流美术设计元素🎨巧妙融合,同时制定精准有效的品牌营销策略📈。
💡 服务涵盖:
🔍WordPress 主题开发,打造独一无二的网站视觉风格与用户体验。
💻WordPress 网站定制,根据您的外贸业务需求,量身定制专属网站。
无论是网站架构搭建,还是界面美学设计,亦或是品牌推广策略,我们都能为您提供一站式的优质解决方案,助力您的外贸事业在互联网上脱颖而出,迈向全球市场🚀。

文章: 45

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注

提交您的请求

报价收集表

💥网站建设 | 实力说话,不搞虚的!

宝子们,咱就说,好多甲方都不乐意自己的网站被同行抄作业,这就跟你开个店,肯定不想让别人知道你的进货渠道是一个道理,对不?所以呢,为了给客户满满的安全感,咱网站上就没放任何客户案例作品的链接哈,这可是咱对客户隐私的保护。

📌但要是有真心想做网站的老板,或者宝子你只是想看看我们的案例,那就赶紧加我们网站的联系方式,咱麻溜地给你发几个瞅瞅,主打一个真诚!

要是你还没拿定主意要做啥风格的网站,也简单,你就找几个同行的网站发给我们,照着做就完事儿。咱选仿站可不代表没本事哈,只是咱实在受不了那种瞎扯犊子的无效沟通和繁琐到让人抓狂的流程。有些甲方做个企业展示型网站,非得找几十家供应商来卷,说实话,没真技术的公司才会在那跪舔甲方。咱不一样,咱就想踏踏实实地给客户做出性价比超高、让客户满意得直拍大腿的网站。那些跪舔的活儿,就让那些只会耍嘴皮子、没啥技术的公司去干吧,咱不伺候! #网站建设

#甲方乙方 #拒绝内卷

联系WordPress技术团队

我们期待您的来信。

提交您的请求

报价收集表