英文:
Preselect first in stock product variation woocommerce
问题
我想在没有设置默认值的情况下预先选择第一个有库存的产品变体。
目前我正在使用这段代码https://stackoverflow.com/questions/70935529/automatically-pre-select-first-in-stock-value-in-woocommerce-variable-product-dr,它几乎可以工作,但如果变体已重新排列,它会出错,因为索引不再与变体匹配。
英文:
I’d like to preselect the first product variation that is in stock if no default is set.
Currently I’m using this code https://stackoverflow.com/questions/70935529/automatically-pre-select-first-in-stock-value-in-woocommerce-variable-product-dr and it almost works but if the variations have been rearranged it breaks as the indexs no longer match up with the variations
答案1
得分: 1
I have translated the code you provided:
成功修改了我之前使用的上述代码以找到解决方案
function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {
// 检查下拉列表中可用选项的计数
if ( count( $args['options'] ) > 0 ) {
// 初始化
$options = array();
// 获取WC_Product_Variable对象
$product = $args['product'];
$has_default = count($product->get_default_attributes());
if(!$has_default) {
// 是WC产品变体
if ( is_a( $product, 'WC_Product_Variable' ) ) {
// 获取当前产品的可用变体的数组
foreach ( $product->get_available_variations() as $key => $variation ) {
// 是否有库存
$is_in_stock = $variation['is_in_stock'];
$attributes = $variation['attributes'];
// 是的
if ( $is_in_stock ) {
// 设置键
foreach ( $attributes as $key => $attribute ) {
array_push($options, $attribute);
}
// 中断
break;
}
}
}
// 查找变量是否为数字
if ( count( $options ) > 0 ) {
// 已选择
$option_key = "";
foreach ( $options as $key => $option ) {
$i = 0;
while($i < count($args['options'])) {
if($option == $args['options'][$i]) {
$option_key = $i;
// 中断;
}
$i++;
}
// 中断;
}
$args['selected'] = $args['options'][$option_key];
} else {
$args['selected'] = "";
}
}
}
return $args;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
英文:
Managed to modify the above code I was using to find the solution
function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {
// Check the count of available options in dropdown
if ( count( $args['options'] ) > 0 ) {
// Initialize
$options = array();
// Get WC_Product_Variable Object
$product = $args['product'];
$has_default = count($product->get_default_attributes());
if(!$has_default) {
// Is a WC Product Variable
if ( is_a( $product, 'WC_Product_Variable' ) ) {
// Get an array of available variations for the current product
foreach ( $product->get_available_variations() as $key => $variation ) {
// Is in stock
$is_in_stock = $variation['is_in_stock'];
$attributes = $variation['attributes'];
// True
if ( $is_in_stock ) {
// Set key
foreach ( $attributes as $key => $attribute ) {
array_push($options, $attribute);
}
// Break
break;
}
}
}
// Finds whether a variable is a number
if ( count( $options ) > 0 ) {
// Selected
$option_key = "";
foreach ( $options as $key => $option ) {
$i = 0;
while($i < count($args['options'])) {
if($option == $args['options'][$i]) {
$option_key = $i;
// break;
}
$i++;
}
// break;
}
$args['selected'] = $args['options'][$option_key];
} else {
$args['selected'] = "";
}
}
}
return $args;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论