Last active
February 13, 2023 09:54
-
-
Save damiencarbery/7381a9d8934e4f0fa342a96f4445b91a to your computer and use it in GitHub Desktop.
Add option for add-on item to WooCommerce cart - A simple way to allow customers add gift wrap or other small add-on to their order. https://www.damiencarbery.com/2020/06/add-option-for-add-on-item-to-woocommerce-cart/
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 | |
/* | |
Plugin Name: Add option for add-on item to WooCommerce cart | |
Plugin URI: https://www.damiencarbery.com/2020/06/add-option-for-add-on-item-to-woocommerce-cart/ | |
Description: A simple way to allow customers add gift wrap or other small add-on to their order. | |
Author: Damien Carbery | |
Version: 0.3 | |
*/ | |
// Return product ID of the optional product. | |
function get_optional_product_id() { | |
return 7923; | |
} | |
// Add option to add Gift Tag/Ribbon to an order. | |
// Inspired by http://wordpress.stackexchange.com/a/128646/57684 | |
add_action('woocommerce_cart_totals_after_shipping', 'dcwd_gift_wrap_option_after_cart'); | |
//add_action('woocommerce_review_order_before_cart_contents', 'dcwd_gift_wrap_option_after_cart'); // Show in checkout. | |
function dcwd_gift_wrap_option_after_cart() { | |
// If you want to disable the gift wrap option when the cart | |
// subtotal is below a certain amount then uncomment the next 3 lines. | |
//if ( WC()->cart->subtotal_ex_tax < 40 ) { | |
// return; | |
//} | |
$enable_popup = true; // Whether to create a popup with a photo and description of the gift wrap option. | |
// If this code is used in the checkout then prettyPhoto will not work because the 'Your Order' section | |
// is reloaded with ajax and the prettyPhoto init code won't find the prettyPhoto link. | |
if ( is_checkout() ) { | |
add_action( 'wp_footer', 'dcwd_trigger_prettyphoto_in_checkout' ); | |
} | |
$found = false; | |
$product_cart_id = WC()->cart->generate_cart_id( get_optional_product_id() ); | |
// Returns an empty string, if the cart item is not found | |
$found = WC()->cart->find_product_in_cart( $product_cart_id ); | |
// Initialise these in case the product cannot be accessed. | |
$product_name = ''; | |
$product_short_description = ''; | |
$product_full_description = ''; | |
$gift_product = wc_get_product( get_optional_product_id() ); | |
if ( $gift_product ) { | |
$product_name = $gift_product->get_name(); | |
$product_image = wp_get_attachment_image_url( $gift_product->get_image_id(), 'woocommerce_single' ); | |
$product_short_description = get_the_excerpt( $gift_product->get_id() ); | |
$product_full_description = apply_filters( 'the_content', get_the_content( null, false, $gift_product->get_id() ) ); | |
$ribbon_product_price = $gift_product->get_price_html(); | |
} | |
else { | |
$product_image = esc_url( wc_placeholder_img_src( 'woocommerce_single' ) ); | |
$ribbon_product_price = '€1'; | |
} | |
if ( $enable_popup ) { | |
// Use prettyPhoto lightbox that is part of WooCommerce (though deprecated). | |
wp_enqueue_script( 'prettyPhoto' ); | |
wp_enqueue_script( 'prettyPhoto-init' ); | |
wp_enqueue_style( 'woocommerce_prettyPhoto_css' ); | |
// Or use the very old Thickbox that is part of WordPress. | |
//add_thickbox(); | |
$gift_wrap_markup = sprintf( '<a id="gift-tag-popup thickbox" data-rel="prettyPhoto" href="%s" title="%s">%s <span class="dashicons dashicons-info"></span></a>', | |
$product_image, $product_full_description, $product_name ); | |
} | |
else { | |
$gift_wrap_markup = $product_name; | |
} | |
?> | |
<tr class="ribbon"> | |
<th><?php echo $gift_wrap_markup; ?></th> | |
<td> | |
<?php | |
// If product not found, add it. | |
if ( ! $found ) { | |
?> | |
<a href="<?php echo do_shortcode(sprintf('[add_to_cart_url id="%d"]', get_optional_product_id())); ?>"><?php echo $product_short_description; ?></a> (+<?php echo $ribbon_product_price; ?>) | |
</tr> | |
<?php } else { ?> | |
Yes | |
<?php } | |
?></td> | |
</tr> | |
<?php | |
} | |
// Make 'Order notes' a required field if the gift wrap product is in the cart. | |
add_filter( 'woocommerce_checkout_fields', 'dcwd_make_order_notes_required' ); | |
function dcwd_make_order_notes_required( $fields ) { | |
if ( array_key_exists( 'order', $fields ) ) { | |
$product_cart_id = WC()->cart->generate_cart_id( get_optional_product_id() ); | |
if ( WC()->cart->find_product_in_cart( $product_cart_id ) ) { | |
$fields[ 'order' ][ 'order_comments' ][ 'required' ] = true; | |
} | |
} | |
return $fields; | |
} | |
// Add a message on checkout page to remind customers with the gift wrap to include info in the Order Notes field. | |
add_action( 'woocommerce_before_checkout_form', 'dcwd_order_notes_reminder' ); | |
function dcwd_order_notes_reminder() { | |
$product_cart_id = WC()->cart->generate_cart_id( get_optional_product_id() ); | |
if ( WC()->cart->find_product_in_cart( $product_cart_id ) ) { | |
?> | |
<script> | |
jQuery(function( $ ){ | |
if ( '' == $( '#order_comments' ).val() ) { | |
alert( "Don't forget to add your Gift Tag & Ribbon message to the 'Order notes' section." ); | |
} | |
}); | |
</script> | |
<?php | |
} | |
} | |
// If this code is used in the checkout then prettyPhoto will not work because the 'Your Order' section | |
// is reloaded with ajax and the prettyPhoto init code won't find the prettyPhoto link. | |
function dcwd_trigger_prettyphoto_in_checkout() { | |
?> | |
<script> | |
jQuery('body').on('updated_checkout', function(){ | |
jQuery("a.zoom, a[data-rel^='prettyPhoto']").prettyPhoto({ | |
hook: 'data-rel', | |
social_tools: false, | |
theme: 'pp_woocommerce', | |
horizontal_padding: 20, | |
opacity: 0.8, | |
deeplinking: false | |
}); | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment