【发布时间】:2015-08-23 19:21:41
【问题描述】:
我将如何更改此 PHP 代码以更改基于 WordPress 的 Woocommerce 插件中选择元素的 ID 选择一个选项?我相信我在 wc-template-function.php 中找到了正确的 PHP 文件,但我缺乏 PHP 技能让我望而却步。这是我目前所拥有的:
if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) {
/**
* Output a list of variation attributes for use in the cart forms.
*
* @param array $args
* @since 2.4.0
*/
function wc_dropdown_variation_attribute_options( $args = array() ) {
$args = wp_parse_args( $args, array(
'options' => false,
'attribute' => false,
'product' => false,
'selected' => false,
'name' => '',
'id' => '',
'class' => '',
'show_option_none' => __( 'Choose an option', 'woocommerce' ),
'show_option_color' => __( 'Choose a color', 'woocommerce' ),
'show_option_size' => __( 'Choose a size', 'woocommerce' )
) );
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
echo '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '">';
if ( $args['show_option_none'] ) {
echo '<option value="">' . esc_html( $args['show_option_none'] ) . '</option>';
}
if ( $args['$id_colors'] ) {
echo '<option value="">' . esc_html( $args['show_option_color'] ) . '</option>';
}
if ( $args['$id_sizes'] ) {
echo '<option value="">' . esc_html( $args['show_option_size'] ) . '</option>';
}
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
echo '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}
echo '</select>';
}
}
您可以看到我尝试将 show_option_color 和 show_option_size 添加到数组中的位置,然后为它们添加 if 语句,但它似乎不起作用。我不确定如何引用 select 元素的 id 并根据它是否是正确的 select 元素编写 if 语句。
这是我要定位的 HTML。
<select id="sizes" class="" name="attribute_sizes" data-attribute_name="attribute_sizes">Want this to say Choose a size</select>
<select id="colors" class="" name="attribute_sizes" data-attribute_name="attribute_sizes">Want this to say Choose a color</select>
variable.php 代码行 27 - 38:
<?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( $_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 ? '<a class="reset_variations" href="#">' . __( 'Clear selection', 'woocommerce' ) . '</a>' : '';
?>
</td>
</tr>
<?php endforeach;?>
【问题讨论】:
-
你能澄清这部分吗? '根据产品的 id 选择一个选项' 你的意思是根据产品的 id 吗?
-
当然。每个产品都没有可供选择的选项、选择尺寸选项、选择颜色选项或两者都有。但是,他们没有说选择尺寸或选择颜色,而是说“选择一个选项”。我只想让每个人说选择颜色或根据您实际选择的尺寸选择尺寸。选择颜色按钮的 id 为 #colors,选择尺寸按钮的 id 为 #sizes,但两者的显示文本都显示“选择一个选项。希望这可以澄清。
-
是的,这有帮助。尽快寻找答案!
标签: php wordpress woocommerce