php - WooCommerce 中特定产品类别的十进制数量步骤
问题描述
我想调整特定产品类别的数量步长以允许十进制数字(具体为 0.5 步)。有点像在Woocommerce 答案中的产品级别设置数量最小值、最大值和步长,但针对特定的小数步长和特定的产品类别。
欢迎任何帮助。
解决方案
为了使其适用于十进制数量步骤的特定产品类别,您不需要在产品级别进行设置......在以下代码中,您必须在第一个功能上设置您的产品类别(可以是术语 ID、slugs 或名称):
// custom conditional function (check for product categories)
function enabled_decimal_quantities( $product ){
$targeted_terms = array(12, 16); // Here define your product category terms (names, slugs ord Ids)
return has_term( $targeted_terms, 'product_cat', $product->get_id() );
}
// Defined quantity arguments
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 9000, 2 );
function custom_quantity_input_args( $args, $product ) {
if( enabled_decimal_quantities( $product ) ) {
if( ! is_cart() ) {
$args['input_value'] = 0.5; // Starting value
}
$args['min_value'] = 0.5; // Minimum value
$args['step'] = 0.5; // Quantity steps
}
return $args;
}
// For Ajax add to cart button (define the min value)
add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
if( enabled_decimal_quantities( $product ) ) {
$args['quantity'] = 0.5; // Min value
}
return $args;
}
// For product variations (define the min value)
add_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_price_html', 10, 3);
function filter_wc_available_variation_price_html( $data, $product, $variation ) {
if( enabled_decimal_quantities( $product ) ) {
$data['min_qty'] = 0.5;
}
return $data;
}
// Enable decimal quantities for stock (in frontend and backend)
remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');
代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。
推荐阅读
- matlab - 如何在实时信号的 FIR 带通滤波器中获得瞬态响应?
- google-cloud-platform - 如何阻止谷歌云虚拟机的存储类 pd-extreme?
- r - 绘制微积分函数
- python - 如何显着提高嵌套循环的速度?
- amazon-web-services - 如何使用 aws codepipeline 和 cloudformation 在 github 中仅部署更改的 lambda 函数?
- python - 有没有更好的方法来绘制轮廓并清除每一帧?
- ruby-on-rails - Sprocket 的要求如何工作?
- nestjs - 在nestjs中验证DTO
- assembly - MIPS 汇编语言中的 jal 指令不保存返回地址
- laravel - Laravel groupBy 选择多个字段