WooCommerce Products Add 30-Day Sales Feature

Functional logic: if the background is enabled and fill in the sales value, the frontend will display according to the background fill in. If the sales value is not filled in, a random number (50-100) will be generated. You can choose to enable or disable the display, remove the √ the foreground will not display.

WooCommerce Products Add 30-Day Sales Feature WooCommerce Products Add 30-Day Sales Feature

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 Support Team
WordPress Support Team

💻 A sincere and meticulous young developer 🎓 specializing in the field of custom foreign trade website development.
🌟 specializes in WordPress website design customization and full-service development. Our unique advantage is not only proficient in website development technology, but also the international mainstream art design elements 🎨 skillful integration, while developing accurate and effective brand marketing strategy 📈.
💡 Services covered:
🔍 WordPress Theme Development for a unique website visual style and user experience.
💻 WordPress website customization, tailor-made exclusive website according to your foreign trade business needs.
Whether it's website architecture construction, interface aesthetic design, or brand promotion strategy, we can provide you with one-stop quality solutions to help your foreign trade business stand out on the Internet and step into the global market 🚀.

Articles: 43

Leave a Reply

Your email address will not be published. Required fields are marked *

Submit your request

Quote Collection Form

💥Website Builder | Strengths speak for themselves, not false!

Treasure, we say, a lot of parties are not happy with their own sites are peers copying homework, which is like you open a store, certainly do not want others to know your sources of supply is a reason, right? So, in order to give customers a full sense of security, we did not put any customer case work on the site of the link ha, this is our protection of customer privacy.

📌 But if there is a sincere desire to do the site of the boss, or treasure you just want to look at our case, then hurry up to add the contact information of our website, we snappy send you a few look at, the main one sincere!

If you have not decided to do what style of website, but also simple, you find a few peers of the site sent to us, according to do on the end of the matter. We choose to imitate the site does not mean that no skill, we just can not stand that kind of invalid communication and cumbersome to make people crazy process. Some parties to do a corporate display site, have to find dozens of suppliers to roll, to be honest, no real technology companies will be there to lick the party. We are not the same, we want to be down-to-earth to the customer to make cost-effective, so that customers are satisfied with the site straight pat. Those kneeling work, let those who only play lip service, no technology companies to do it, we do not serve! # Website Development

# Party A Party B # Rejected Inside Coil

Contact the WordPress Technical Team

We look forward to hearing from you.

Submit your request

Quote Collection Form