gpt4 book ai didi

php - 如何在 Woocommerce 中的属性上方的一行中添加自定义文本?

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:37 29 4
gpt4 key购买 nike

我使用此函数在属性标签旁边添加文本,但我需要在属性上方或下方添加文本,例如 pa_colour 属性上方(或下方)的“请选择所需颜色”之类的内容。

add_filter( 'woocommerce_attribute_label', 'custom_attribute_label', 10, 3 );
function custom_attribute_label( $label, $name, $product ) {
$taxonomy = 'pa_'.$name;

if( $taxonomy == 'pa_size' )
$label .= '<div class="custom-label">' . __('MY TEXT', 'woocommerce') . '</div>';

return $label;
}

问题是,我不确定该怎么做。如果您能指导我,我将不胜感激。

我想看到的:

我现在看到的:

最佳答案

没有什么钩子(Hook)可以轻易得到你想要的。因为 html 看起来像这样:

<table class="variations" cellspacing="0">
<tbody>
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="value">
<?php
$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( stripslashes( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) ) : $product->get_variation_default_attribute( $attribute_name );
wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>

您正在为 wc_attribute_label 使用过滤器在 <td> 中调用标签。你想要实现的需要在它自己的tr td至少。

我可以建议您继续使用现有的,然后通过使用 css 获得您想要实现的目标。或 jQuery .

最好复制 woocommerce/templates/single-product/add-to-cart/variable.phpyourtheme/woocommerce/single-product/add-to-cart/variable.php并对该文件进行编辑。

关于php - 如何在 Woocommerce 中的属性上方的一行中添加自定义文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646760/

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