jfo*_*tch 9 php wordpress woocommerce
我将如何更改此PHP代码以更改根据WordPress的Woocommerce插件中的select元素的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>';
}
}
Run Code Online (Sandbox Code Playgroud)
您可以看到我尝试将show_option_color和show_option_size添加到数组中的位置,然后为它们添加if语句,但它似乎不起作用.我不确定如何引用select元素的id并根据if语句是否是正确的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>
Run Code Online (Sandbox Code Playgroud)
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;?>
Run Code Online (Sandbox Code Playgroud)
Jos*_*Bau 10
这是自定义过滤器的完美用例!我首先要描述的方法并不是最快捷的方法,但对于可能需要阅读代码的其他人来说,这可能是最干净,最容易理解的方法.如果你处于紧张状态,我还会描述一种"更脏"的方式.
快速方式:
找到它的位置的位置在文件中:
/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/variable.php
Run Code Online (Sandbox Code Playgroud)
第27行,根据您的WooCommerce版本,您会看到类似这样的内容:
<option value=""><?php echo __( 'Choose an option', 'woocommerce' ) ?>…</option>
Run Code Online (Sandbox Code Playgroud)
__()函数使用'woocommerce'文本域通过WordPress的翻译系统运行第一个参数.最好保留翻译的可能性,因此我们希望在通过翻译功能发送之前更改此文本.
这行代码发生在输出所有产品变体属性的循环中.这使我们可以通过查看$ name变量轻松查看正在输出的属性.
我们需要创建一个接收$ name变量的函数,并根据它输出一个字符串.它看起来像这样:
function get_text_for_select_based_on_attribute($attribute) {
// Find the name of the attribute for the slug we passed in to the function
$attribute_name = wc_attribute_label($attribute);
// Create a string for our select
$select_text = 'Select a ' . $attribute_name;
// Send the $select_text variable back to our calling function
return $select_text;
}
Run Code Online (Sandbox Code Playgroud)
现在,在变量.php第27行的代码之前,我们可以这样做:
<?php
$select_text = get_text_for_select_based_on_attribute($name);
?>
Run Code Online (Sandbox Code Playgroud)
然后,只需用$ select_text变量替换"选择一个选项":
<option value=""><?php echo __( $select_text, 'woocommerce' ) ?>…</option>
Run Code Online (Sandbox Code Playgroud)
不要忘记在模板覆盖中全部执行此操作,否则您的自定义将在下次更新时丢失!
http://docs.woothemes.com/document/template-structure/
清洁方式:
更好,更可扩展的方法是添加自定义过滤器以通过它.这是一些额外的步骤,但如果您希望根据您的产品逐个覆盖功能,则可以轻松添加更多自定义逻辑.
首先,使用具有语义意义的名称创建自定义过滤器,并将其放在powers.php文件中的某个主题:
add_filter('variable_product_select_text', 'get_text_for_select_based_on_attribute', 10, 1);
Run Code Online (Sandbox Code Playgroud)
然后,在variable.php文件中,而不是直接调用函数,将其传递给新的过滤器:
$select_text = apply_filters('variable_product_select_text', $name);
Run Code Online (Sandbox Code Playgroud)
为这样的事情设置自定义过滤器确实需要更长的时间,但是您可以获得可维护性的优势,因为您可以在不需要进一步修改现有代码的情况下堆叠或关闭功能.
WC 2.4的更新
WooCommerce 2.4版引入了一种获取属性及其相关选择的不同方式.由于他们仍然没有为此提供过滤器,我建议使用上述方法覆盖wc_dropdown_variation_attribute_options函数.因此,将整个函数复制并粘贴到主题的functions.php文件中,从声明开始,如果选择文本不是颜色或大小,则为其添加变量:
//Don't include the if(!function_exists(...) part.
wc_dropdown_variation_attribute_options($args = array()) {
// Uses the same function as above, or optionally a custom filter
$select_text = get_text_for_select_based_on_attribute($args['attribute']);
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' => __( $select_text, 'woocommerce' ),
'show_option_color' => __( 'Choose a color', 'woocommerce' ),
'show_option_size' => __( 'Choose a size', 'woocommerce' )
) );
// Put the rest of the function here
Run Code Online (Sandbox Code Playgroud)
我找到了WC 2.5的方法.将其添加到functions.php:
function my_dropdown_variation_attribute_options_html($html, $args){
$html = str_replace('Choose an option', 'Choose', $html);
return $html;
}
add_filter('woocommerce_dropdown_variation_attribute_options_html', 'my_dropdown_variation_attribute_options_html', 10, 2);
Run Code Online (Sandbox Code Playgroud)
小智 7
我在这里结合了其他答案来完成此操作,对我来说效果很好。
这是使用WooCoommerce 3.3.5完成的,并假定过滤器是最近才添加的。
您可以对该wc_dropdown_variation_attribute_options()功能使用过滤器。
// define the woocommerce_dropdown_variation_attribute_options_args callback
function filter_woocommerce_dropdown_variation_attribute_options_args( $array ) {
// Find the name of the attribute for the slug we passed in to the function
$attribute_name = wc_attribute_label($array['attribute']);
// Create a string for our select
$select_text = 'Select a ' . $attribute_name;
$array['show_option_none'] = __( $select_text, 'woocommerce' );
return $array;
};
// add the filter
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
Run Code Online (Sandbox Code Playgroud)
希望这对某人有帮助。
实际上有一种更简单的方法来定制它.
如果您查看核心WC文件wc-template-functions.php,您可以看到wc_dropdown_variation_attribute_options函数内的数组实际上是具有以下键值对的参数:
'options' => false,
'attribute' => false,
'product' => false,
'selected' => false,
'name' => '',
'id' => '',
'class' => '',
'show_option_none' => __( 'Choose an option', 'woocommerce' )
Run Code Online (Sandbox Code Playgroud)
现在您需要做的就是在variable.php模板文件中的函数中更改相同的键值对.所以我想显示下拉标签而不是选择一个选项文本,所以我改变了fu
wc_dropdown_variation_attribute_options(
array(
'options' => $options,
'attribute' => $attribute_name,
'product' => $product,
'selected' => $selected,
'show_option_none' => wc_attribute_label( $attribute_name )
)
);
Run Code Online (Sandbox Code Playgroud)
其他对也一样.这有帮助.顺便说一句,这只适用于WC 2.4+,所以要小心.
| 归档时间: |
|
| 查看次数: |
22294 次 |
| 最近记录: |