Skip to content

Instantly share code, notes, and snippets.

@odessy
Last active November 22, 2018 17:11
Show Gist options
  • Save odessy/4e95f2fc76ad64ffc0df8a2f685c2ad7 to your computer and use it in GitHub Desktop.
Save odessy/4e95f2fc76ad64ffc0df8a2f685c2ad7 to your computer and use it in GitHub Desktop.
Handsome checkout pages - remove products from cart if no purchase made
<?php
// Handsome checkout pages - remove products from cart if no purchase made
add_filter('wp', 'remove_hcc_cart_item');
function remove_hcc_cart_item() {
if (is_admin() || defined('DOING_AJAX')) {
return;
}
global $post;
$url = $_SERVER['REQUEST_URI'];
$order_pay_endpoint = get_option( 'woocommerce_checkout_order_pay_endpoint', 'order-pay' );
if(!empty( $url )
&& !is_handsome_checkout_url( $url )
&& mb_strpos( $url, '/' . $order_pay_endpoint . '/' ) === FALSE
&& !empty( $post ) && !has_shortcode( $post->post_content, 'wc_hcc_checkout' ) ) {
foreach( WC()->cart->cart_contents as $item_key => $item ){
if( isset($item['hcc_checkout_page_item']) ){
WC()->cart->remove_cart_item( $item_key );
}
}
}
}
add_filter('woocommerce_add_cart_item_data','hcc_add_item_data',1,2);
function hcc_add_item_data($cart_item_data, $product_id){
global $post;
$url = wp_get_referer();
if(empty( $post )){
$post_id = url_to_postid( $url );
$post = get_post( $post_id );
}
if( !empty( $url ) && is_handsome_checkout_url( $url )
|| !empty( $post ) && has_shortcode( $post->post_content, 'wc_hcc_checkout' ) ){
$cart_item_data['hcc_checkout_page_item'] = true;
}
return $cart_item_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment