`在结账页面隐藏货到付款付款
我想在 WordPress 的单个产品页面上添加自定义按钮。当用户单击自定义按钮然后将产品添加到购物车时,用户会转到结帐页面。在结帐页面不显示 COD 支付网关,无需自定义产品,在结帐页面上显示货到付款 (COD)。
function display_customize_button_and_field() {
echo '<p id="customize-instruction" style="margin-bottom: 5px;">' . __('If you wants to customize the product, click here:', 'woocommerce') . '</p>';
echo '<button id="customize-button" type="button" style="margin-bottom: 10px;">' . __('Customize', 'woocommerce') . '</button>';
echo '<div id="custom-field-container" style="display:none;">';
woocommerce_form_field( 'custom_field_name', array(
'type' => 'textarea',
'class' => array('custom-field-class form-row-wide'),
'label' => __('Message', 'woocommerce'),
'placeholder' => __('Write Details here', 'woocommerce'),
), '' );
echo '</div>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'display_customize_button_and_field' );
// Validate custom field
function validate_custom_field($passed, $product_id, $quantity) {
if ( isset($_POST['custom_field_name']) && !empty($_POST['custom_field_name']) && empty(trim($_POST['custom_field_name'])) ) {
wc_add_notice( __('Please enter a value for the custom field.'), 'error' );
return false;
}
return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'validate_custom_field', 10, 3);
// Save custom field
function save_custom_field($cart_item_data, $product_id) {
if (isset($_POST['custom_field_name']) && !empty($_POST['custom_field_name'])) {
$cart_item_data['custom_field_name'] = sanitize_textarea_field($_POST['custom_field_name']);
}
return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data', 'save_custom_field', 10, 2);
// Display custom field in cart
function display_custom_field_in_cart($item_data, $cart_item) {
if (isset($cart_item['custom_field_name'])) {
$item_data[] = array(
'name' => __('Custom Field'),
'value' => $cart_item['custom_field_name']
);
}
return $item_data;
}
add_filter('woocommerce_get_item_data', 'display_custom_field_in_cart', 10, 2);
// Add custom field value to order meta
function add_custom_field_to_order_meta($item_id, $values, $cart_item_key) {
if (isset($values['custom_field_name'])) {
wc_add_order_item_meta($item_id, 'Custom Field', $values['custom_field_name']);
}
}
add_action('woocommerce_add_order_item_meta', 'add_custom_field_to_order_meta', 10, 3);
// Display custom field in admin order
function display_custom_field_in_admin_order($item_id, $item, $product) {
if ($custom_field_value = wc_get_order_item_meta($item_id, 'Custom Field')) {
echo '<p><strong>' . __('Custom Field') . ':</strong> ' . $custom_field_value . '</p>';
}
}
add_action('woocommerce_admin_order_data_after_order_details', 'display_custom_field_in_admin_order', 10, 3);
// Enqueue custom script
function enqueue_custom_script() {
if ( is_product() ) {
wp_enqueue_script( 'customize-product', get_template_directory_uri() . '/js/customize-product.js', array('jquery'), null, true );
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_script' );
// Filter available payment gateways on checkout page
function disable_cod_for_customized_product_on_checkout($available_gateways) {
// Check if a product is being customized
if (isset($_POST['custom_field_name']) && !empty($_POST['custom_field_name'])) {
// Unset Cash on Delivery
unset($available_gateways['cod']);
}
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'disable_cod_for_customized_product_on_checkout');
此代码不起作用任何错误`
我想
$_POST
在您的钩子 woocommerce_available_payment_gateways
中将不可用以禁用支付网关。无论如何,尝试将可用的 paymnet 网关过滤器回调替换为下面的代码片段。
// Filter available payment gateways on checkout page
function disable_cod_for_customized_product_on_checkout( $available_gateways ) {
// Loop through available cart items.
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Get the cart item meta data.
$item_meta = $cart_item['data']->get_meta_data();
// Loop through meta data.
foreach ( $item_meta as $meta ) {
// Check if a product is being customized.
if ( 'custom_field_name' === $meta->key && ! empty( $meta->value ) ) {
// Unset Cash on Delivery.
if ( isset( $available_gateways['cod'] ) ) {
unset( $available_gateways['cod'] );
}
break 2; // Break the loop as condition satisfied.
}
}
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'disable_cod_for_customized_product_on_checkout' );