我想为Woocommerce插件做一个自定义变量产品选择器,我将解释一下情况:
我已做过微小更改in this php code,如果有库存可用性,可以使用变体名称进行显示。工作得很好!
我的最终目标是将下拉选择器更改为更酷的东西,像某些插件那样的标签选择器,但我想要库存可用性和我尝试的所有插件从变体名称中删除了我的调整。
我将向您展示所涉及的代码,这是告诉我每个变体中是否存在或者没有库存的部分:
档案:mytheme/customfunctions.php
// VARIATION STOCK
add_filter( 'woocommerce_variation_option_name', 'customizing_variations_terms_name', 10, 1 );
function customizing_variations_terms_name( $term_name ){
global $stock_status, $stock_qty;
if(is_admin())
return $term_name;
global $product;
$second_loop_stoped = false;
// Get available product variations
$product_variations = $product->get_available_variations();
// Iterating through each available product variation
foreach($product_variations as $variation){
$variation_id = $variation['variation_id'];
$variation_obj = new WC_Product_Variation( $variation_id );
## WOOCOMMERCE RETRO COMPATIBILITY ##
if ( version_compare( WC_VERSION, '3.0', '<' ) ) # BEFORE Version 3 (older)
{
$stock_status = $variation_obj->stock_status;
$stock_qty = intval($variation_obj->stock);
// The attributes WC slug key and slug value for this variation
$attributes_arr = $variation_obj->get_variation_attributes();
}
else # For newest verions: 3.0+ (and Up)
{
$stock_status = $variation_obj->get_stock_status();
$stock_qty = $variation_obj->get_stock_quantity();
// The attributes taxonomy key and slug value for this variation
$attributes_arr = $variation_obj->get_attributes();
}
if(count($attributes_arr) != 1) // Works only for 1 attribute set in the product
return $term_name;
// Get the terms for this attribute
foreach( $attributes_arr as $attr_key => $term_slug){
// Get the attribute taxonomy
$term_key = str_replace('attribute_', '', $attr_key );
// get the corresponding term object
$term_obj = get_term_by( 'slug', $term_slug, $term_key );
if( $term_obj->name == $term_name ){ // If the term name matches we stop the loops
$second_loop_stoped = true;
break;
}
}
if($second_loop_stoped)
break;
}
if( $stock_qty>0 )
return $term_name .= ' - Disponible' ;
else
return $term_name .= ' - Consultar' ;
}
还有一部分我认为我需要调整以实现我的目标,但我有点迷失:
档案:woocommerce/single-product/add-to-cart/variable.php
<?php do_action('woocommerce_before_add_to_cart_form'); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $available_variations ) ) ?>">
<?php do_action( 'woocommerce_before_variations_form' ); ?>
<?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
<p class="stock out-of-stock"><?php esc_html_e( 'This product is currently out of stock and unavailable.', 'virtue' ); ?></p>
<?php else : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<tr>
<td class="product_label label"><label for="<?php echo sanitize_title($attribute_name); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="product_value 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( 'select' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected, 'class'=>'kad-select') );
echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear selection', 'virtue' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
我的目标是将默认选择下拉列表更改为带有标签的更酷选择器而不会丢失库存可用性,我只需要从html选择下拉列表切换到标签时需要一些指导。
非常感谢任何见解。
代码来源:Show stock status next to each attribute value in WooCommerce variable products
更新:我设法将选择下拉列表更改为输入表格无线电选择器。 我现在的目标是获取正确的值,因为当我点击所需的值时,数据未正确发送。
当我点击该选项时页面重新加载,因为我在woc_dropdown_variation_attribute_options函数的输入标记中放置了一个带有javascript的表单提交,但是看起来所选的选项处理得不好。
我所做的是用输入形式半径选择器替换选择下拉列表(我会在以后功能良好的情况下更改样式)
以下是代码:
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->get_id(), $attribute,
array( 'fields' => 'all' ) );
/*DIV MODIFICACION*/
echo '<div class="radio-group">';
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
/*OPCIONES VARIABLES*/
/*$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . '</option>';*/
/*Modificacion*/
echo '<input onclick="this.form.submit()"; type="radio" value="' . esc_attr(
$term->slug ) . '" id="' . esc_attr( $id ) . '" name="' . esc_attr( $name )
. ' ><label for="option-one" > ' . esc_html( apply_filters(
'woocommerce_variation_option_name', $term->name ) ) . ' </label>';
}
}
/*Cierre DIV modificacion*/
echo '</div>';
上述文件的第284至287行。