From 2e6aaa9ede710f38eaf786d822e0e073041eb882 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 19 Aug 2013 18:19:27 +0100 Subject: [PATCH] Hide password protected posts from loop, prevent adding to cart, hide product content. Closes #3617. --- includes/class-wc-query.php | 13 +++++++++++++ includes/wc-cart-functions.php | 16 ++++++++++++++++ readme.txt | 1 + templates/content-single-product.php | 5 +++++ 4 files changed, 35 insertions(+) diff --git a/includes/class-wc-query.php b/includes/class-wc-query.php index 01aace931ff..8b434a8f346 100644 --- a/includes/class-wc-query.php +++ b/includes/class-wc-query.php @@ -182,6 +182,8 @@ class WC_Query { add_filter( 'wp', array( $this, 'remove_posts_where' ) ); } + add_filter( 'posts_where', array( $this, 'exclude_protected_products' ) ); + // We're on a shop page so queue the woocommerce_get_products_in_view function add_action( 'wp', array( $this, 'get_products_in_view' ), 2); @@ -210,6 +212,17 @@ class WC_Query { return $where; } + /** + * Prevent password protected products appearing in the loops + * + * @param string $where + * @return string + */ + public function exclude_protected_products( $where ) { + $where .= " AND post_password = ''"; + return $where; + } + /** * wpseo_metadesc function. * diff --git a/includes/wc-cart-functions.php b/includes/wc-cart-functions.php index d593ca582a0..6e35cd16e0d 100644 --- a/includes/wc-cart-functions.php +++ b/includes/wc-cart-functions.php @@ -12,6 +12,22 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly +/** + * Prevent password protected products being added to the cart + * + * @param bool $passed + * @param int $product_id + * @return bool + */ +function woocommerce_protected_product_add_to_cart( $passed, $product_id ) { + if ( post_password_required( $product_id ) ) { + $passed = false; + wc_add_error( __( 'This product is protected and cannot be purchased.', 'woocommerce' ) ); + } + return $passed; +} +add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_protected_product_add_to_cart', 10, 2 ); + /** * WooCommerce clear cart * diff --git a/readme.txt b/readme.txt index 2a41306484f..e8b134a5e04 100644 --- a/readme.txt +++ b/readme.txt @@ -178,6 +178,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc * Feature - Separate options for countries you can ship to and sell to. * Feature - BACS supports multiple account details. * Feature - PayPal PDT support (as alternative to IPN). +* Feature - Handling for password protected products. * Tweak - Added pagination to tax rate screens. * Tweak - Added filter to check the 'Create account' checkbox on checkout by default. * Tweak - Update CPT parameters for 'product_variation' and 'shop_coupon' to be no longer public. diff --git a/templates/content-single-product.php b/templates/content-single-product.php index cb4593584e3..344f4985773 100644 --- a/templates/content-single-product.php +++ b/templates/content-single-product.php @@ -19,6 +19,11 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly * @hooked wc_print_messages - 10 */ do_action( 'woocommerce_before_single_product' ); + + if ( post_password_required() ) { + echo get_the_password_form(); + return; + } ?>
>