Untitled

 avatar
unknown
plain_text
2 years ago
1.2 kB
3
Indexable
function filter_woocommerce_available_payment_gateways( $gateways ) {
	if (is_admin()){
		return $gateways;
	}
    // Define the vendor IDs for which COD is allowed
    $allowed_vendors = array( 1, 2, 3 ); // Replace with actual vendor IDs

    // Flag to check if a product from the allowed vendor is in the cart
    $allowed_vendor_in_cart = false;

    // Check the cart contents
    foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
        // Get the product ID
        $product_id = $cart_item['product_id'];

        // Get the post_author (vendor) of the product
        $vendor_id = get_post_field( 'post_author', $product_id );

        // Check if the vendor ID is in the allowed list
        if ( in_array( $vendor_id, $allowed_vendors ) ) {
            $allowed_vendor_in_cart = true;
            break;
        }
    }

    // If no allowed vendor's product is in the cart, unset COD
    if ( !$allowed_vendor_in_cart && isset( $gateways['cod'] ) ) {
        unset( $gateways['cod'] );
    }

    return $gateways;
}

add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
Editor is loading...
Leave a Comment