22/**
33 * Maximum Products per User for WooCommerce - Core Class.
44 *
5- * @version 4.2.4
5+ * @version 4.4.0
66 * @since 1.0.0
77 * @author WPFactory
88 */
@@ -121,6 +121,15 @@ class Alg_WC_MPPU_Core extends Alg_WC_MPPU_Dynamic_Properties_Obj {
121121 */
122122 public $ options ;
123123
124+ /**
125+ * shown_term_notices.
126+ *
127+ * @since 4.4.0
128+ *
129+ * @var array
130+ */
131+ public $ shown_term_notices = array ();
132+
124133 /**
125134 * Constructor.
126135 *
@@ -1618,7 +1627,7 @@ function apply_placeholders( $message ) {
16181627 /**
16191628 * output_notice.
16201629 *
1621- * @version 4.2 .0
1630+ * @version 4.4 .0
16221631 * @since 2.0.0
16231632 * @todo [maybe] customizable notice type in `wc_add_notice()`?
16241633 */
@@ -1647,12 +1656,25 @@ function output_notice( $args = null ) {
16471656 $ msg = $ args ['msg ' ];
16481657 $ return = $ args ['return ' ];
16491658 $ this ->get_notice_placeholders ( $ product_id , $ limit , $ bought_data , $ in_cart_plus_adding , $ adding , $ term );
1650- $ message = ( $ msg ? $ msg : get_option ( 'wpjup_wc_maximum_products_per_user_message ' ,
1651- sprintf (
1652- __ ( '[alg_wc_mppu_customer_msg bought_msg="%s" not_bought_msg="%s"] ' ),
1653- __ ( "You can only buy maximum %limit% of %product_title% (you've already bought %bought%). " , 'maximum-products-per-user-for-woocommerce ' ),
1654- __ ( "You can only buy maximum %limit% of %product_title%. " , 'maximum-products-per-user-for-woocommerce ' )
1655- ) ) );
1659+ $ message = (
1660+ $ msg ? $ msg :
1661+ ( $ term ?
1662+ get_option ( 'wpjup_wc_maximum_products_per_term_message ' ,
1663+ sprintf (
1664+ __ ( '[alg_wc_mppu_customer_msg bought_msg="%s" not_bought_msg="%s"] ' ),
1665+ __ ( "You can only buy maximum %limit% of %term_name% (you've already bought %bought%). " , 'maximum-products-per-user-for-woocommerce ' ),
1666+ __ ( "You can only buy maximum %limit% of %term_name%. " , 'maximum-products-per-user-for-woocommerce ' )
1667+ )
1668+ ) :
1669+ get_option ( 'wpjup_wc_maximum_products_per_user_message ' ,
1670+ sprintf (
1671+ __ ( '[alg_wc_mppu_customer_msg bought_msg="%s" not_bought_msg="%s"] ' ),
1672+ __ ( "You can only buy maximum %limit% of %product_title% (you've already bought %bought%). " , 'maximum-products-per-user-for-woocommerce ' ),
1673+ __ ( "You can only buy maximum %limit% of %product_title%. " , 'maximum-products-per-user-for-woocommerce ' )
1674+ )
1675+ )
1676+ )
1677+ );
16561678 $ message = $ this ->apply_placeholders ( do_shortcode ( $ message ) );
16571679 if ( ! $ return ) {
16581680 if ( ! empty ( $ output_function ) ) {
@@ -1851,29 +1873,14 @@ function check_quantities_for_product( $_product_id, $args ) {
18511873 ) {
18521874 return apply_filters ( 'alg_wc_mppu_check_quantities_for_product ' , false , $ this , $ args );
18531875 }
1854- // Block - default mechanism
1855- if ( 'yes ' === apply_filters ( 'alg_wc_mppu_local_enabled ' , 'no ' ) && 0 != ( $ max_qty = $ this ->get_max_qty ( array ( 'type ' => 'per_product ' , 'product_or_term_id ' => $ product_id , 'user_id ' => $ current_user_id ) ) ) ) {
1856- // Per product
1857- $ bought_data = $ this ->get_user_already_bought_qty ( $ maybe_translated_product_id , $ current_user_id , true );
1858- $ user_already_bought = $ bought_data ['bought ' ];
1859- if ( ( $ user_already_bought + $ cart_item_quantity ) > $ max_qty ) {
1860- if ( $ do_add_notices ) {
1861- $ this ->output_notice ( array (
1862- 'product_id ' => $ product_id ,
1863- 'limit ' => $ max_qty ,
1864- 'bought_data ' => $ bought_data ,
1865- 'output_function ' => $ output_function ,
1866- 'in_cart_plus_adding ' => $ cart_item_quantity ,
1867- 'adding ' => $ adding ,
1868- 'notice_type ' => $ notice_type ,
1869- 'return ' => $ return_notices
1870- ) );
1871- }
1872- return apply_filters ( 'alg_wc_mppu_check_quantities_for_product ' , false , $ this , $ args );
1873- }
1874- } else {
1875- // Per taxonomy
1876- $ validated_by_term = false ;
1876+
1877+ $ is_valid = true ;
1878+ $ validated_by_term = false ;
1879+ // Per taxonomy
1880+ if (
1881+ 'yes ' === apply_filters ( 'alg_wc_mppu_product_tag_enabled ' , 'no ' ) ||
1882+ 'yes ' === apply_filters ( 'alg_wc_mppu_product_cat_enabled ' , 'no ' )
1883+ ) {
18771884 foreach ( array ( 'product_cat ' , 'product_tag ' ) as $ taxonomy ) {
18781885 if ( ! $ validated_by_term && 'yes ' === apply_filters ( 'alg_wc_mppu_ ' . $ taxonomy . '_enabled ' , 'no ' ) ) {
18791886 $ terms = get_the_terms ( $ parent_product_id , $ taxonomy );
@@ -1885,7 +1892,7 @@ function check_quantities_for_product( $_product_id, $args ) {
18851892 $ bought_data = $ this ->get_user_already_bought_qty ( $ term ->term_id , $ current_user_id , false );
18861893 $ user_already_bought = $ bought_data ['bought ' ];
18871894 if ( ( $ user_already_bought + $ cart_item_quantity_all ) > $ max_qty ) {
1888- if ( $ do_add_notices ) {
1895+ if ( $ do_add_notices && empty ( $ this -> shown_term_notices [ $ term -> term_id ] ) ) {
18891896 $ this ->output_notice ( array (
18901897 'product_id ' => $ product_id ,
18911898 'limit ' => $ max_qty ,
@@ -1897,41 +1904,82 @@ function check_quantities_for_product( $_product_id, $args ) {
18971904 'term ' => $ term ,
18981905 'return ' => $ return_notices
18991906 ) );
1907+
1908+ $ this ->shown_term_notices [ $ term ->term_id ] = true ;
19001909 }
1901- return apply_filters ( ' alg_wc_mppu_check_quantities_for_product ' , false , $ this , $ args ) ;
1910+ $ is_valid = false ;
19021911 }
19031912 }
19041913 }
19051914 }
19061915 }
19071916 }
1908- if (
1909- ! $ validated_by_term &&
1910- (
1911- ( 'yes ' === get_option ( 'wpjup_wc_maximum_products_per_user_global_enabled ' , 'no ' ) && 0 != ( $ max_qty = $ this ->get_max_qty ( array ( 'type ' => 'all_products ' , 'user_id ' => $ current_user_id ) ) ) ) ||
1912- ( 'yes ' === get_option ( 'alg_wc_mppu_formula_enabled ' , 'no ' ) && 0 != ( $ max_qty = $ this ->get_max_qty ( array ( 'type ' => 'formula ' , 'user_id ' => $ current_user_id ) ) ) )
1917+ }
1918+
1919+ // Block - default mechanism
1920+ if (
1921+ 'yes ' === apply_filters ( 'alg_wc_mppu_local_enabled ' , 'no ' ) &&
1922+ 0 != (
1923+ $ max_qty =
1924+ $ this ->get_max_qty (
1925+ array (
1926+ 'type ' => 'per_product ' ,
1927+ 'product_or_term_id ' => $ product_id ,
1928+ 'user_id ' => $ current_user_id
1929+ )
19131930 )
1914- ) {
1915- $ bought_data = $ this ->get_user_already_bought_qty ( $ maybe_translated_product_id , $ current_user_id , true );
1916- $ user_already_bought = $ bought_data ['bought ' ];
1917- if ( ( $ user_already_bought + $ cart_item_quantity ) > $ max_qty ) {
1918- if ( $ do_add_notices ) {
1919- $ this ->output_notice ( array (
1920- 'product_id ' => $ product_id ,
1921- 'limit ' => $ max_qty ,
1922- 'bought_data ' => $ bought_data ,
1923- 'notice_type ' => $ notice_type ,
1924- 'output_function ' => $ output_function ,
1925- 'in_cart_plus_adding ' => $ cart_item_quantity ,
1926- 'adding ' => $ adding ,
1927- 'return ' => $ return_notices
1928- ) );
1929- }
1930- return apply_filters ( 'alg_wc_mppu_check_quantities_for_product ' , false , $ this , $ args );
1931+ )
1932+ ) {
1933+ // Per product
1934+ $ bought_data = $ this ->get_user_already_bought_qty ( $ maybe_translated_product_id , $ current_user_id , true );
1935+ $ user_already_bought = $ bought_data ['bought ' ];
1936+ if ( ( $ user_already_bought + $ cart_item_quantity ) > $ max_qty ) {
1937+ if ( $ do_add_notices ) {
1938+ $ this ->output_notice ( array (
1939+ 'product_id ' => $ product_id ,
1940+ 'limit ' => $ max_qty ,
1941+ 'bought_data ' => $ bought_data ,
1942+ 'output_function ' => $ output_function ,
1943+ 'in_cart_plus_adding ' => $ cart_item_quantity ,
1944+ 'adding ' => $ adding ,
1945+ 'notice_type ' => $ notice_type ,
1946+ 'return ' => $ return_notices
1947+ ) );
1948+ }
1949+ $ is_valid = false ;
1950+ }
1951+ } else if (
1952+ (
1953+ 'yes ' === get_option ( 'wpjup_wc_maximum_products_per_user_global_enabled ' , 'no ' ) &&
1954+ 0 != ( $ max_qty = $ this ->get_max_qty ( array (
1955+ 'type ' => 'all_products ' ,
1956+ 'user_id ' => $ current_user_id
1957+ ) ) )
1958+ ) ||
1959+ (
1960+ 'yes ' === get_option ( 'alg_wc_mppu_formula_enabled ' , 'no ' ) &&
1961+ 0 != ( $ max_qty = $ this ->get_max_qty ( array ( 'type ' => 'formula ' , 'user_id ' => $ current_user_id ) ) )
1962+ )
1963+ ) {
1964+ $ bought_data = $ this ->get_user_already_bought_qty ( $ maybe_translated_product_id , $ current_user_id , true );
1965+ $ user_already_bought = $ bought_data ['bought ' ];
1966+ if ( ( $ user_already_bought + $ cart_item_quantity ) > $ max_qty ) {
1967+ if ( $ do_add_notices ) {
1968+ $ this ->output_notice ( array (
1969+ 'product_id ' => $ product_id ,
1970+ 'limit ' => $ max_qty ,
1971+ 'bought_data ' => $ bought_data ,
1972+ 'notice_type ' => $ notice_type ,
1973+ 'output_function ' => $ output_function ,
1974+ 'in_cart_plus_adding ' => $ cart_item_quantity ,
1975+ 'adding ' => $ adding ,
1976+ 'return ' => $ return_notices
1977+ ) );
19311978 }
1979+ $ is_valid = false ;
19321980 }
19331981 }
1934- return apply_filters ( 'alg_wc_mppu_check_quantities_for_product ' , true , $ this , $ args );
1982+ return apply_filters ( 'alg_wc_mppu_check_quantities_for_product ' , $ is_valid , $ this , $ args );
19351983 }
19361984
19371985 /**
0 commit comments