dpka7974 2014-02-05 19:16
浏览 93
已采纳

如何根据库存值更改opencart中的“购买”按钮颜色?

What is the best way to change button color in a product based on the stock status?

I have successfully done it with javascript by "reading" the innerHTML and if it is i.e out-of-stock, i make the button background green, else blue...

But if there are 5 statuses and 3 translations this results in a big and "ugly" if clause.

My problem is that the store is multilanguage and i am wondering how could i do it without reading innerHTML with js but only read the status value (from model or controller...)?

The previous "dummy" solution is this:

$(document).ready(function() {
    var stck = $('#stocktxt').html();
    if(stck.indexOf("5-") != -1){
        $("#button-cart").css("background-color","#f59525");
    }
});
  • 写回答

1条回答 默认 最新

  • dongsuishou8039 2014-02-05 19:20
    关注

    How is the site being generated? I'm guessing PHP! The best would be for the server to apply a style to the button dynamically based on the stock status. Applying style client side is inefficient unless the stock is being updated in real time. Obviously the stock data lives in a database somewhere, you should be getting the data from that, not innerHTML.

    <?php
    //get stock value for your items
    $someItemStock = 3;
    
    function stockstyle($stock)
    {
        if($stock > 5)
        {
            echo "highStock"
        }
        else if($stock < 5 && $stock > 2)
        {
            echo "medStock"
        }
        else if($stock < 2 && $stock > 0)
        {
            echo "lowStock"
        }
        else
        {
        echo "noStock"
        }
    }
    

    ?>

    and then in your HTML for the item:

    <button id="whatever this item" class="<?php stockstyle($someItemStock) ?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊