dongpu4141 2017-07-18 18:55
浏览 94
已采纳

WooCommerce删除我添加到产品简短描述的自定义jQuery代码

I'm developing an eCommerce site with WordPress+WooCommerce.

I added buttons to certain products in the Short Description field in order to quickly select certain number of products to be added in the cart (this is because I have different offers, for example: "Buy 4 for the price of 3").

I have a script like this in the general jquery file:

<script>
    function pack4() { document.getElementById("quantity").value = "4"; }
    function pack8() { document.getElementById("quantity").value = "8"; }
</script>

And this is an example of the code I have in some products Short Description:

<span><strong>4x3 Pack:</strong>TOTAL<strong>$1800</strong></span>
<button class="select" onclick="pack4()">Select</button>

Everything works and whenever you click on every button the order quantity adds the corresponding number.

The thing is: Whenever my client (who usually edits products) click on the Text/Visual flap of Short Description, the onclick="pack4()" code disappears. I guess WordPress/WooCommerce have got some jQuery filter that erases this kind of codes. I need it to stop doing that, because the client regularly edits things (and of course they don't know anything about coding). Otherwise, I have to add the code again every time.

I also suspect WP/WC also filters it without needing to click on this Text/Visual flap (but I might be wrong about that).

Anyone knows what to do?
Thanks!

  • 写回答

1条回答 默认 最新

  • douba4275 2017-07-18 23:04
    关注

    The easiest and accurate way is to create a custom shortcode this way:

    if( !function_exists('display_product_button_pack') ) {
    
        function display_product_button_pack( $atts ) {
    
            // Shortcode Attributes
            $atts = shortcode_atts(
                array(
                    'pack' => '4', // default pack is 4
                ),
                $atts,
                'button_pack'
            );
    
            $output = '<button class="select" onclick="pack'.$atts['pack'].'()">Select</button>';
    
            return $output;
    
        }
    
        add_shortcode( 'button_pack', 'display_product_button_pack' );
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    The code is tested and works.


    USAGE:

    There is an optional pack argument in your shortcode, which default value is 4

    For your Pack 8 for example, you will insert this in the short description product editor:

    [button_pack pack="8"]
    

    This will output this html:

    <button class="select" onclick="pack8()">Select</button>
    

    For the Pack 4, as default argument value is already 4 you just use this:

    [button_pack]
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
  • ¥20 谁刷目标页面的uv记录器上数据,数据只记录跳转的数值
  • ¥30 数据库软件的安装方法
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部