Skip to content

Instantly share code, notes, and snippets.

@thisissandip
Last active February 4, 2024 07:28
Show Gist options
  • Save thisissandip/629560ce1d7bcc4a146bfee3593cd261 to your computer and use it in GitHub Desktop.
Save thisissandip/629560ce1d7bcc4a146bfee3593cd261 to your computer and use it in GitHub Desktop.
The cart page features a "Share Cart" button, allowing admins to generate a unique cart URL for easy cart sharing with customers. Additionally, admins can manage generated cart URLs by clearing them from the "WC > Status > Tools" section.
<?php
/**
* Function to get cart data from session
*
*/
function get_cart_data_from_session(){
$cart_session = array(
'cart' => '',
'cart_totals' => '',
'applied_coupons' => '',
'coupon_discount_totals' => '',
'coupon_discount_tax_totals' => '',
);
foreach($cart_session as $key => $value) {
$cart_session[$key] = WC()->session->get( $key );
}
return $cart_session;
}
/**
* Function to extract hash from URL and update session
*
*/
function set_session_cart_from_url(){
if( isset($_REQUEST['share'])) {
$cart_url_hash = $_REQUEST['share'];
$cart_url_option_name = 'wc_cart_url_' . $cart_url_hash;
$cart_data = get_option($cart_url_option_name);
if ($cart_data) {
foreach($cart_data as $key => $value) {
WC()->session->set( $key, $value );
}
}
}
}
/**
* Function to add share cart button before cart
*
*/
function add_share_cart_button(){
if (current_user_can('manage_woocommerce')) {
if (isset($_POST['wc_cart_url'])) {
$session_cart_data = get_cart_data_from_session();
$cart_url_hash = wp_hash( serialize($session_cart_data) );
$cart_url_option_name = 'wc_cart_url_' . $cart_url_hash;
$cart_url = wc_get_cart_url() . '?share=' . $cart_url_hash;
update_option($cart_url_option_name, $session_cart_data);
wc_print_notice(__("Share the cart using this URL: $cart_url", 'woocommerce'), 'success');
} else {
?>
<form method="post">
<button type="submit" name="wc_cart_url"><?php _e('Share cart', 'woocommerce'); ?></button>
</form>
<?php
}
}
}
add_action( 'woocommerce_before_cart', 'add_share_cart_button' );
add_action('woocommerce_load_cart_from_session', 'set_session_cart_from_url', 1);
add_filter( 'woocommerce_debug_tools', 'add_clear_wc_cart_url_tool' );
/**
* Function for `woocommerce_debug_tools` filter.
*
*/
function add_clear_wc_cart_url_tool( $tools ) {
$wc_cart_url_tool = array(
'wc_cart_url_tool' => array(
'name' => 'Clear Generated Cart URLs',
'button' => 'Clear',
'desc' => 'This tool deletes all the cart urls saved in the database',
'callback' => 'clear_wc_cart_url',
'disabled' => false,
),
);
$tools = array_slice( $tools, 0, 2, true ) + $wc_cart_url_tool + array_slice( $tools, 2, NULL, true );
return $tools;
}
/**
* Callback function wc_cart_url_tool
*
*/
function clear_wc_cart_url() {
global $wpdb;
$all_cart_urls = $wpdb->get_results( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE 'wc_cart_url_%'" );
foreach( $all_cart_urls as $option ) {
delete_option( $option->option_name );
}
return 'All Cart URLs has been deleted!';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment