我正在尝试在 Woocommerce 产品页面中添加自定义输入字段。输入字段将采用 pin 码。
// Add custom input field to WooCommerce product page
add_action('woocommerce_before_add_to_cart_button', 'add_custom_input_field');
function add_custom_input_field() {
echo '<div class="custom-input-field">
<label for="custom_pincode_field">Enter Pincode:</label>
<input type="text" id="custom_pincode_field" name="custom_pincode_field" placeholder="Enter your pincode" required>
</div>';
}
// Validate the custom pincode input field
add_filter('woocommerce_add_to_cart_validation', 'validate_custom_pincode_field', 10, 3);
function validate_custom_pincode_field($passed, $product_id, $quantity) {
if (empty($_POST['custom_pincode_field'])) {
wc_add_notice(__('Please enter your pincode.'), 'error');
return false;
}
return $passed;
}
我本来希望在产品页面上看到这个输入字段,但它没有出现在那里。我尝试将此代码添加到 function.php 文件中,但它不起作用。
检查 Elementor 的版本(如果您正在使用它)。我有一个显示产品的模板,其中包含价格,并且在该价格内包含 PHP 过滤器的输入。该输入消失了。
我将 Elementor 插件版本降级到 3.24.0 和 Hello Elementor,它再次开始工作。