From 528712b38fd26d060d0b9cbd5f6a93d4bdcaa366 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Thu, 12 Sep 2024 16:40:00 +0530 Subject: [PATCH] `gspc-hidden-product-handler.php`: Added snippet to remove url and edit button for Hidden Catalog Products on Cart. --- .../gspc-hidden-product-handler.php | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 gs-product-configurator/gspc-hidden-product-handler.php diff --git a/gs-product-configurator/gspc-hidden-product-handler.php b/gs-product-configurator/gspc-hidden-product-handler.php new file mode 100644 index 00000000..e7e5adee --- /dev/null +++ b/gs-product-configurator/gspc-hidden-product-handler.php @@ -0,0 +1,88 @@ +_args = wp_parse_args( $args, array( + 'form_id' => false, + 'field_id' => false, + ) ); + + add_action( 'init', array( $this, 'init' ) ); + + } + + public function init() { + add_filter( 'woocommerce_cart_item_name', array( $this, 'remove_url_for_hidden_product' ), 10, 3 ); + add_filter( 'gspc_cart_item_edit_link', array( $this, 'maybe_remove_edit_button' ), 10, 2 ); + } + + /** + * Helper function to check if the product is hidden. + * + * @param array $cart_item The cart item data. + * + * @return bool True if the product is hidden, false otherwise. + */ + public function is_hidden_product( $cart_item ) { + $product = $cart_item['data']; + + if ( ! ( $product instanceof WC_Product ) ) { + return false; + } + + return ( $product->get_catalog_visibility() === 'hidden' ); + } + + /** + * Remove the URL for hidden products in the cart. + * + * @param string $product_name The product name. + * @param array $cart_item The cart item data. + * @param string $cart_item_key The cart item key. + * + * @return string Modified product name without link for hidden products. + */ + public function remove_url_for_hidden_product( $product_name, $cart_item, $cart_item_key ) { + if ( $this->is_hidden_product( $cart_item ) ) { + // Remove the link for hidden products + $product_name = strip_tags( $product_name ); + } + + return $product_name; + } + + /** + * Remove the edit button for hidden products in the cart. + * + * @param string $edit_link The edit link HTML. + * @param array $cart_item The cart item data. + * + * @return string Modified edit link (empty) if the product is hidden. + */ + public function maybe_remove_edit_button( $edit_link, $cart_item ) { + if ( $this->is_hidden_product( $cart_item ) ) { + // Remove the edit link for hidden products + return ''; + } + + return $edit_link; + } +} + +// Initialize the class with no specific arguments +new GW_Hidden_Product_Handler();