Forums › Conditional Payment Gateways for WooCommerce › Conditional payment gateways by variation shipping status
- Support forum for the WooCommerce Conditional Payment Methods.
- This topic has 3 replies, 2 voices, and was last updated 2 months, 1 week ago by
Taha.
-
AuthorPosts
-
February 7, 2025 at 3:16 pm #158787
Teodorescu Sebastian
ParticipantHello, I have a problem regarding the product. I sell shoes and the variations have multiple sizes, 40, 41 etc. One size is in stock and another is on backorder. I want to be able to select that specific variation and set it to only accept payment by card. When I bought the plugin originally, it used to let me do that, now I can’t select specific variations.
February 16, 2025 at 7:35 am #159288Hi Teodorescu,
Thanks for reaching out! I hope you’re having a great day.
Apologies for the delayed response. I just tested your scenario on my end, and I was able to set a specific variation of my product to accept only Cash on Delivery.
Please ensure that:
- You have enabled the option.
- If you’re using the excluded products feature, make sure that option is enabled as well.
I have recorded a video demonstration showing it working correctly. You can follow the same settings as I demonstrated. Let me know if it works for you. If it’s still not working, feel free to share your current settings, and I’ll check if anything is missing.
Looking forward to your update!
Best regards,
February 16, 2025 at 1:45 pm #159324Teodorescu Sebastian
ParticipantHello, thanks for your response. I want to disable cash on delivery for EVERY product variation that is on backorder. Right now I have to manually select each variation. Can you give me a solution for my problem? We have thousands of products…
February 19, 2025 at 9:27 am #159498Hi Teodorescu,
I hope you’re doing well.
You can achieve this using a snippet of code. However, please ensure you test it first on a staging site, as it may cause issues or even crash the site, especially since you have thousands of products. This script only runs when an admin is logged in and should be removed after the update.
add_action( 'init', 'disable_cod_for_backorder_variations' ); function disable_cod_for_backorder_variations() { if ( current_user_can( 'administrator' ) ) { // WP_Query to get all product variations $args = array( 'post_type' => 'product_variation', // Query for product variations 'posts_per_page' => - 1, // Get all variations 'post_status' => 'publish', // Only published variations 'fields' => 'ids', // Only retrieve variation IDs ); $variation_query = new WP_Query( $args ); // Retrieve the current excluded products from the database (for COD) $excluded_cod_products = get_option( 'alg_wc_cpg_product_excl', array() ); // If the 'cod' key doesn't exist, initialize it as an empty array if ( ! isset( $excluded_cod_products['cod'] ) ) { $excluded_cod_products['cod'] = array(); } // Loop through all product variations foreach ( $variation_query->posts as $variation_id ) { $variation = wc_get_product( $variation_id ); // Check if the variation is on backorder if ( $variation->is_on_backorder() ) { // Cast $variation_id to a string explicitly $variation_id_str = (string) $variation_id; // Add the variation ID (as a string) to the exclusion list for COD if it's not already there if ( ! in_array( $variation_id_str, $excluded_cod_products['cod'], true ) ) { $excluded_cod_products['cod'][] = $variation_id_str; } } } // Ensure all existing IDs in the array are strings (for consistency) $excluded_cod_products['cod'] = array_map( 'strval', $excluded_cod_products['cod'] ); // Save the updated exclusion list into the database update_option( 'alg_wc_cpg_product_excl', $excluded_cod_products ); } }
Please test this on your staging site first and let me know if it works for you.
Best regards,
WPFactory Support Team -
AuthorPosts
- You must be logged in to reply to this topic.