Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bryanrsebastian/73b1345ef057700c8ef618acff60bac9 to your computer and use it in GitHub Desktop.
Save bryanrsebastian/73b1345ef057700c8ef618acff60bac9 to your computer and use it in GitHub Desktop.
/**
* Remove the "Place Order" button if the
* Product Variant is not available selling world wide
*/
add_filter( 'woocommerce_order_button_html', 'disable_place_order_button_html' );
function disable_place_order_button_html( $button ) {
/* Exit if the screen is Dashboard and not Checkout */
if( is_admin() && ! is_checkout() )
return $button;
/* Get the Customer and Cart details in the Session */
$customerDetails = WC()->session->get( 'customer' );
$customerCart = WC()->session->get( 'cart' );
/* Check if there's Print Variant in the Cart */
$hasPrint = false;
foreach( $customerCart as $product ) {
if( $product['variation']['attribute_type'] == 'Print' )
$hasPrint = true;
}
/**
* Check if there's a Print Product Variant and if not AU Country
* then replace the "Place Order" button with the given return html string
*/
if( $hasPrint && $customerDetails['country'] != 'AU' )
return '<p style="display: table; margin: 0 0 0 auto; padding: 10px 20px; background: #9e9e9e; color: #e8e8e8; cursor: not-allowed; border-radius: 3px;">Sorry, Print Variation is not purchasable outside AU</p><a href="/cart" style="display: table; margin: 10px 0 0 auto; background: #e5b5cf; color: #fff; font-weight: 700; text-transform: uppercase; font-size: 14px; padding: 7px 15px; transition: all .2s; border-radius: 3px;">Update Cart</a>';
return $button;
}
/**
* Remove the "PayPal" button if the
* Product Variant is not available selling world wide
*/
add_filter( 'woocommerce_available_payment_gateways', 'disable_paypal' );
function disable_paypal( $available_gateways ) {
/* Exit if the screen is Dashboard and not Checkout */
if( is_admin() && ! is_checkout() )
return $available_gateways;
/* Get the Customer and Cart details in the Session */
$customerDetails = WC()->session->get( 'customer' );
$customerCart = WC()->session->get( 'cart' );
/* Check if there's Print Variant in the Cart */
$hasPrint = false;
foreach( $customerCart as $product ) {
if( $product['variation']['attribute_type'] == 'Print' )
$hasPrint = true;
}
/* Store the available Payment Gateways to avoid the permanent loosing of gateway */
$updatedGateways = [];
if( $hasPrint && $customerDetails['country'] != 'AU' ) {
foreach( $available_gateways as $key => $val ) {
/* Re-store the payment gateway */
if( $key != 'ppcp-gateway' )
$updatedGateways[$key] = $val;
}
} else {
/* Return the original Payment Gateways if the condition above is not satisfied */
$updatedGateways = $available_gateways;
}
return $updatedGateways;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment