[英]WooCommerce delete custom jQuery codes I add to Product Short Description
I'm developing an eCommerce site with WordPress+WooCommerce. 我正在使用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"). 我在“简短描述”字段中为某些产品添加了按钮,以便快速选择要在购物车中添加的特定数量的产品(这是因为我有不同的优惠,例如:“以3的价格购买4”)。
I have a script like this in the general jquery file: 我在一般的jquery文件中有这样的脚本:
<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. 问题是:每当我的客户(通常编辑产品)点击短描述的文本/视觉翻盖时, onclick="pack4()"
代码就会消失。 I guess WordPress/WooCommerce have got some jQuery filter that erases this kind of codes. 我猜WordPress / WooCommerce有一些jQuery过滤器可以删除这种代码。 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). 我还怀疑WP / WC也可以过滤它而不需要点击这个文本/视觉翻盖(但我可能错了)。
Anyone knows what to do? 谁知道该怎么办?
Thanks! 谢谢!
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. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
The code is tested and works. 代码经过测试和运行。
USAGE: 用法:
There is an optional pack
argument in your shortcode, which default value is 4
… 您的短代码中有一个可选的pack
参数,默认值为4
...
For your Pack 8 for example, you will insert this in the short description product editor: 例如,对于Pack 8 ,您将在简短描述产品编辑器中插入:
[button_pack pack="8"]
This will output this html: 这将输出此html:
<button class="select" onclick="pack8()">Select</button>
For the Pack 4, as default argument value is already 4 you just use this: 对于Pack 4,由于默认参数值已经是4,您只需使用:
[button_pack]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.