-
-
Save bentasm1/c8fb033e2ea0784f052b7e4808b3ed6b to your computer and use it in GitHub Desktop.
WC Vendors - Removes paypalap on checkout page if vendor hasnt entered their PayPal email on their Dashboard.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* WC Vendors - Looks at the cart and, if something in the cart is sold by a vendor who hasn’t saved | |
* his PayPal account address in his Dashboard>Settings>Payment area, then the PayPal AP gateway included | |
* with WC Vendors is removed from the list of available gateways at checkout. | |
* Props to @dvrcthewrld for the code | |
*/ | |
function ps_filter_gateways($gateways) { | |
if ( is_checkout_pay_page() ) { | |
global $wp; | |
if ( isset( $wp->query_vars['order-pay'] ) ) { | |
$order_id = $wp->query_vars['order-pay']; | |
$order = wc_get_order($order_id); | |
if ($order) { | |
$order_items = $order->get_items('line_item'); | |
if ( ! empty( $order_items ) ) { | |
foreach ($order_items as $order_item) { | |
$productid = $order_item[ 'product_id' ]; | |
$vendor = get_post_field( 'post_author', $post_id ); | |
if ( ! empty($vendor) ) { | |
$ppaddress = get_user_meta( $vendor, 'pv_paypal', true ); | |
if (empty($ppaddress)) { | |
if (isset( $gateways['paypalap'] )) { | |
unset( $gateways['paypalap'] ); | |
break; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} else { | |
$cart_contents = WC()->cart->get_cart(); | |
if ( ! empty( $cart_contents ) ) { | |
foreach ( $cart_contents as $cart_item_key => $cart_item_data ) { | |
$product = $cart_item_data[ 'data' ]; | |
$productid = $product->id; | |
$vendor = get_post_field( 'post_author', $productid ); | |
if ( ! empty($vendor) ) { | |
$ppaddress = get_user_meta( $vendor, 'pv_paypal', true ); | |
if (empty($ppaddress)) { | |
if (isset( $gateways['paypalap'] )) { | |
unset( $gateways['paypalap'] ); | |
break; | |
} | |
} | |
} | |
} | |
} | |
} | |
return $gateways; | |
} | |
add_filter('woocommerce_available_payment_gateways','ps_filter_gateways'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment