Created
June 11, 2025 04:44
-
-
Save rajeshsingh520/3ed14a605daa38392d5b062cd7edef60 to your computer and use it in GitHub Desktop.
remove product from cart based on pickup location
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
| class pisol_custom_20250611{ | |
| static $instance = null; | |
| public static function get_instance() { | |
| if (self::$instance === null) { | |
| self::$instance = new self(); | |
| } | |
| return self::$instance; | |
| } | |
| private function __construct() | |
| { | |
| add_filter('pisol_dtt_disable_location_filtering_by_product', '__return_true'); | |
| add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts')); | |
| add_action('woocommerce_before_calculate_totals', array($this, 'before_calculate_totals')); | |
| } | |
| public function enqueue_scripts() { | |
| if ( is_checkout() ) { | |
| $js = <<<JS | |
| jQuery(function($) { | |
| jQuery(document).on('change', 'select[name="pickup_location"], input[name="pickup_location"]', function() { | |
| $(document.body).trigger('update_checkout'); | |
| }); | |
| }); | |
| JS; | |
| wc_enqueue_js( $js ); | |
| } | |
| } | |
| public function before_calculate_totals($cart) { | |
| if (isset($_REQUEST['wc-ajax']) && $_REQUEST['wc-ajax'] === 'update_order_review') { | |
| if (!empty($_POST['post_data'])) { | |
| // parse it | |
| parse_str($_POST['post_data'], $checkout_fields); | |
| if (isset($checkout_fields['pickup_location']) && !empty($checkout_fields['pickup_location'])) { | |
| $pickup_location = sanitize_text_field($checkout_fields['pickup_location']); | |
| WC()->session->set('pickup_location', $pickup_location); | |
| } else { | |
| WC()->session->set('pickup_location', ''); | |
| } | |
| $pickup_location = WC()->session->get('pickup_location'); | |
| $product_wise_location = pisol_pccdt_available_pickup_location::getAvailablePickupLocations(); | |
| if(is_array($product_wise_location) && !empty($product_wise_location)){ | |
| if(isset($product_wise_location['product_location']) && is_array($product_wise_location['product_location'])){ | |
| foreach($product_wise_location['product_location'] as $product_id => $locations){ | |
| if(!in_array($pickup_location, $locations)){ | |
| WC()->cart->remove_cart_item( $this->find_cart_item_key_by_product_id( $product_id ) ); | |
| wc_add_notice( sprintf(__('The product %s is not available for the selected pickup location.', 'your-textdomain'), get_the_title($product_id)), 'error' ); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| function find_cart_item_key_by_product_id($product_id) { | |
| foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { | |
| if ($cart_item['product_id'] == $product_id || $cart_item['variation_id'] == $product_id) { | |
| return $cart_item_key; | |
| } | |
| } | |
| return false; | |
| } | |
| } | |
| pisol_custom_20250611::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment