gpt4 book ai didi

javascript - 在 Woocommerce 中使用 jQuery 产品变体属性呈现

转载 作者:行者123 更新时间:2023-11-30 14:34:47 25 4
gpt4 key购买 nike

我正在构建一个 woocommerce 网上商店,我需要在单个产品页面中显示每个产品的属性。我卡在了变量产品上。

虽然我能够在控制台上获取当前的变体属性,但我找不到在页面上打印这些属性值的方法。

我在 woocommerce 的 short-description.php 文件中使用了这个脚本。

 <script>
jQuery(document).ready(function( $ ){
$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
$('#variation_descr').html(variation.variation_description);
console.log (variation.attributes);

} );
});
</script>

有什么帮助吗?

最佳答案

使用 jQuery each() 尝试以下操作类似于 PHP foreach 循环的函数……我已将代码嵌入到一个 Hook 函数中,这将使显示无需更改模板(仅用于测试):

add_action( 'woocommerce_before_single_variation', 'custom_before_single_variation' );
function custom_before_single_variation() {
?>
<div id="variation_descr"></div>
<script>
jQuery(function($){
$(".single_variation_wrap" ).on( "show_variation", function ( event, data ) {
$('#variation_descr').html(data.variation_description);

$.each(variation.attributes, function(attribute,value){
$('#variation_descr').append('<p><strong>'+attribute+'</strong>: '+value+'</p>');
console.log("Attribute: "+attribute+" | Value: "+value);
});
console.log("Variation Id: "+data.variation_id);
});
});
</script>
<?php
}

代码进入事件子主题(或事件主题)的 function.php 文件。经过测试并有效。

关于javascript - 在 Woocommerce 中使用 jQuery 产品变体属性呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50589880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com