实现WooCommerce特定产品支持用户自定义定价功能
解决WooCommerce自定义定价礼品卡的三个问题
针对你创建自定义定价礼品卡时遇到的三个问题,以下是修改后的完整代码,一次性解决所有问题:
修改说明
1. 禁止分类页0元下单
通过移除目标产品在分类页的「添加到购物车」按钮,强制用户进入产品页输入自定义价格后再下单。
2. 限制输入框仅支持数值
将输入框类型从text改为number,并设置最小金额和步长,同时在验证环节添加数值有效性检查,确保输入的是正数。
3. 规范隐藏产品页0元价格
使用WooCommerce官方钩子woocommerce_get_price_html替换CSS隐藏,直接修改产品价格的显示内容,更符合开发规范。
完整代码
// 1. 在产品页添加自定义价格输入框 add_action( 'woocommerce_before_add_to_cart_button', 'custom_giftcard_price_input', 9 ); function custom_giftcard_price_input() { global $product; // 替换为你的礼品卡产品ID $target_product_id = 241982; if ( $target_product_id !== $product->get_id() ) return; woocommerce_form_field( 'set_price', array( 'type' => 'number', // 改为数值输入框 'required' => true, 'label' => '设置礼品卡金额 ' . get_woocommerce_currency_symbol(), 'min' => 0.01, // 最小金额,避免0元 'step' => 0.01, // 支持小数金额 'custom_attributes' => array( 'placeholder' => '请输入金额' ) )); } // 2. 验证自定义价格的有效性 add_filter( 'woocommerce_add_to_cart_validation', 'custom_giftcard_price_validation', 9999, 3 ); function custom_giftcard_price_validation( $passed, $product_id, $qty ) { $target_product_id = 241982; if ( $target_product_id !== $product_id ) return $passed; if ( !isset( $_POST['set_price'] ) || empty( $_POST['set_price'] ) ) { wc_add_notice( '请输入礼品卡金额', 'error' ); $passed = false; } elseif ( !is_numeric( $_POST['set_price'] ) || (float)$_POST['set_price'] <= 0 ) { wc_add_notice( '请输入有效的正数金额', 'error' ); $passed = false; } return $passed; } // 3. 将自定义价格存入购物车项数据 add_filter( 'woocommerce_add_cart_item_data', 'custom_giftcard_store_price_data', 9999, 2 ); function custom_giftcard_store_price_data( $cart_item, $product_id ) { $target_product_id = 241982; if ( $target_product_id !== $product_id ) return $cart_item; $cart_item['set_price'] = (float)sanitize_text_field( $_POST['set_price'] ); return $cart_item; } // 4. 修改购物车中的产品价格 add_action( 'woocommerce_before_calculate_totals', 'custom_giftcard_update_cart_price', 9999 ); function custom_giftcard_update_cart_price( $cart ) { if ( is_admin() && !defined( 'DOING_AJAX' ) ) return; if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return; $target_product_id = 241982; foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) { $product = $cart_item['data']; if ( $target_product_id !== $product->get_id() ) continue; $cart_item['data']->set_price( $cart_item['set_price'] ); } } // 5. 隐藏产品页的0元价格显示 add_filter( 'woocommerce_get_price_html', 'custom_giftcard_hide_zero_price', 9999, 2 ); function custom_giftcard_hide_zero_price( $price_html, $product ) { $target_product_id = 241982; if ( $target_product_id === $product->get_id() && $product->get_price() == 0 ) { return ''; // 直接返回空,不显示价格 // 也可以返回自定义文本,比如:return '<span class="giftcard-price">自定义金额</span>'; } return $price_html; } // 6. 移除分类页的添加到购物车按钮(强制用户进入产品页) add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_giftcard_remove_archive_add_to_cart', 9999, 2 ); function custom_giftcard_remove_archive_add_to_cart( $link, $product ) { $target_product_id = 241982; if ( $target_product_id === $product->get_id() ) { // 替换为"查看产品"按钮,或者直接返回空 return '<a href="' . get_permalink( $product->get_id() ) . '" class="button">设置礼品卡金额</a>'; } return $link; }
使用说明
- 将代码添加到你的主题
functions.php文件中,或者使用自定义代码插件(如Code Snippets)。 - 确保将代码中的
241982替换为你的礼品卡产品实际ID。 - 可以根据需求调整输入框的提示文本、最小金额等参数。
内容的提问来源于stack exchange,提问作者Sjors
相关产品推荐
相关产品推荐

