Last active
May 22, 2018 23:39
-
-
Save vtsara/f9f998d043abeb5a7f1700a9fd0a04dd to your computer and use it in GitHub Desktop.
Optional fee at checkout if certain categories are present in cart | woocommerce
This file contains 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 | |
// add optional catering delivery fee to cart page IF catering product is in cart | |
add_action('woocommerce_cart_totals_after_shipping', 'lg_catering_delivery_info_at_cart'); | |
function lg_catering_delivery_info_at_cart() { | |
// set category check flag to be false until we find a product in that category | |
$cat_check = false; | |
// check each cart item for catering category | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
$product = $cart_item['data']; | |
if ( has_term( 'catering', 'product_cat', $product->id ) ) { | |
$cat_check = true; | |
// break because we only need one "true" to matter here | |
break; | |
} | |
} | |
// if catering category is found, add optional delivery fee | |
if ( $cat_check ) { | |
global $woocommerce; | |
$product_id = 2090; // this is the delivery fee product | |
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { | |
$_product = $values['data']; | |
if ( $_product->id == $product_id ) | |
$found = true; | |
} | |
// if product not found, add it | |
if ( ! $found ): | |
?> | |
<tr class="lg-delivery-fee"> | |
<th>delivery available for <button onclick="document.getElementById('delivery-popup').style.display='block'"class="w3-button lg">downtown Raleigh only</button></th> | |
<td><a href="<?php echo do_shortcode('[add_to_cart_url id="2090"]'); ?>"><?php _e( 'add delivery fee (+$10)' ); ?> </a></td> | |
</tr> | |
<div id="delivery-popup" class="w3-modal"> | |
<div class="w3-modal-content"> | |
<div class="w3-container"> | |
<span onclick="document.getElementById('delivery-popup').style.display='none'" class="w3-button w3-display-topright">×</span> | |
<img src="http://lucettegrace.flywheelsites.com/wp-content/uploads/2018/04/DeliveryMap.jpg"> | |
</div> | |
</div> | |
</div> | |
<?php else: ?> | |
<tr class="lg-delivery-fee"> | |
<th><?php _e( 'delivery fee', 'woocommerce' ); ?></th> | |
<td>$10.00</td> | |
</tr> | |
<?php endif; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
adapted from this stackexchange response