Last active
May 30, 2019 11:17
-
-
Save celticwebdesign/17ea8731d1f707ee20c0884f30a5b9ad to your computer and use it in GitHub Desktop.
WordPress, Woocommerce, Cart page, add message and form (Gravity Forms) if customer country is not supported.
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
<?php | |
/** | |
Display a notice on the cart page requesting people with Countries not supported to email us. | |
*/ | |
// http://hookr.io/actions/woocommerce_after_shipping_calculator/# | |
function action_woocommerce_after_shipping_calculator( ) { | |
echo ' | |
<style> | |
.action_woocommerce_after_shipping_calculator {background: #eee;margin-top: 10px;padding: 10px;font-size: 1.4rem;line-height: normal;} | |
.action_woocommerce_after_shipping_calculator .message {} | |
.action_woocommerce_after_shipping_calculator .message p {margin-bottom: 10px;} | |
.action_woocommerce_after_shipping_calculator .message p:last-of-type {margin-bottom: 0;} | |
.action_woocommerce_after_shipping_calculator .message ul {margin:6px 0 10px;padding: 0 0 0 30px;} | |
.action_woocommerce_after_shipping_calculator .message ul li {} | |
.action_woocommerce_after_shipping_calculator .message .contact-link {color: #ef7c33;} | |
.action_woocommerce_after_shipping_calculator .message .contact-link:hover {cursor: pointer;color: #474546;text-decoration: underline;} | |
.action_woocommerce_after_shipping_calculator .message .cart_contents {display: none;} | |
.action_woocommerce_after_shipping_calculator .form {display: none;margin: 14px 0px 0px;} | |
.action_woocommerce_after_shipping_calculator .form .gform_body ul li {padding: 0 !important;margin: 0 0 4px;width: 100% !important;} | |
.action_woocommerce_after_shipping_calculator .form .gform_wrapper.gform_validation_error form > .validation_error { margin-bottom: 15px;margin-top: 20px;} | |
.action_woocommerce_after_shipping_calculator .form .gform_wrapper.gform_validation_error form .gform_body ul li.gfield.gfield_error {max-width: 100% !important;} | |
.action_woocommerce_after_shipping_calculator .form .gform_wrapper.gform_validation_error form .gform_body ul li.gfield.gfield_error .validation_message {padding: 4px 10px 6px;} | |
.action_woocommerce_after_shipping_calculator .form .gform_body ul li:last-child {margin-bottom: 0;} | |
.action_woocommerce_after_shipping_calculator .form .gform_body ul li .ginput_container {margin: 0;} | |
.action_woocommerce_after_shipping_calculator .form .gform_body ul li input, .action_woocommerce_after_shipping_calculator .form .gform_body ul li textarea {margin: 0;border-radius: 0;width: 100% !important;} | |
.action_woocommerce_after_shipping_calculator .form .gform_footer {position: static;} | |
.action_woocommerce_after_shipping_calculator .form .gform_footer input {width: 100% !important;text-transform: none;} | |
.action_woocommerce_after_shipping_calculator .form .gform_footer input:hover {background: #474546;color:#fff;} | |
.action_woocommerce_after_shipping_calculator .form .gform_footer img.gform_ajax_spinner {display: none !important;} | |
.action_woocommerce_after_shipping_calculator .form .gform_confirmation_wrapper { border-top: 1px solid #28a745!important;border-bottom: 1px solid #28a745!important;color: #28a745;text-align: center;padding: 10px;} | |
.action_woocommerce_after_shipping_calculator ::-webkit-input-placeholder {color: grey;} | |
.action_woocommerce_after_shipping_calculator ::-moz-placeholder {color: grey;} | |
.action_woocommerce_after_shipping_calculator :-ms-input-placeholder {color: grey;} | |
.action_woocommerce_after_shipping_calculator :-moz-placeholder {color: grey;} | |
</style> | |
'; | |
echo ' | |
<div class="action_woocommerce_after_shipping_calculator"> | |
<div class="message"> | |
<p>Currently you can only order online from:</p>'; | |
global $woocommerce; | |
$selling_countries_obj = new WC_Countries(); | |
// Get selling countries | |
// $selling_countries_obj_2 = $selling_countries_obj->get_allowed_countries(); | |
// print_r($selling_countries_obj_2); // [code] => name | |
// Get shipping countries | |
$shipping_countries_obj_2 = $selling_countries_obj->get_shipping_countries(); | |
// print_r($shipping_countries_obj_2); // [code] => name | |
echo '<ul>'; | |
foreach($shipping_countries_obj_2 as $k => $v) { | |
echo '<li class="'.$k.'">'.$v.'</li>'; | |
} | |
echo '</ul>'; | |
echo ' | |
<p>If you could like us to deliver to another country, we will handle your purchase directly.</p><p>Please <span class="contact-link">click here</span> to send us your order and we will contact you to take payment.</p>'; | |
echo '<div class="form">'.do_shortcode('[gravityform id=2 title=false description=false ajax=true tabindex=49]').'</div>'; | |
global $woocommerce; | |
$items = $woocommerce->cart->get_cart(); | |
$output = ""; | |
foreach($items as $item => $values) { | |
$_product = wc_get_product( $values['data']->get_id() ); | |
$price = get_post_meta($values['product_id'] , '_price', true); | |
$output .= $_product->get_title()."\nQuantity: ".$values['quantity']; | |
$output .= "\nPrice: ".$price."\n\n"; | |
} | |
echo "<div class='cart_contents'>".$output."</div>"; | |
echo ' | |
</div> | |
</div> | |
'; | |
echo ' | |
<script> | |
jQuery(document).ready(function($){ | |
jQuery(".contact-link").click(function(){ | |
jQuery("textarea#input_2_3").val( jQuery(".cart_contents").html() ); | |
jQuery(".form").toggle(); | |
}); | |
}); | |
</script> | |
'; | |
}; | |
// add the action | |
add_action( 'woocommerce_after_shipping_calculator', 'action_woocommerce_after_shipping_calculator', 10, 0 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment