OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Hide payment COD Gateway via Add customize Button on Single Product Page in Wordpress

  • Thread starter Thread starter Muhammad zubair Ahmad
  • Start date Start date
M

Muhammad zubair Ahmad

Guest
Hide COD payment From Checkout page

I want to add customize button on single product page in wordpress. When users click on customize button and then add product to cart then user go to check out page. In checkout page not show COD payment gateway without customize product thrn show cash on delivery(COD) on checkout page.

Code:
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');

Not works - does this code have Any mistake?
<p><strong>Hide COD payment From Checkout page</strong></p>
<p>I want to add customize button on single product page in wordpress. When users click on customize button and then add product to cart then user go to check out page. In checkout page not show COD payment gateway without customize product thrn show cash on delivery(COD) on checkout page.</p>
<pre><code>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');

</code></pre>
<p>Not works - does this code have Any mistake?</p>
Continue reading...
 

Latest posts

Top