Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Adding an option to exclude products from selected product categories from applying the Call for Price feature. #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions includes/admin/class-wc-call-for-price-settings-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,7 @@
* @since 1.0.0
*/
class Alg_WC_Call_For_Price_Settings_General {
/**
* Id.
*
* @var $id
* @since 3.0.0
*/
public $id = '';

/**
* Desc.
*
* @var $desc
* @since 3.0.0
*/
public $desc = '';
/**
* Constructor.
*
Expand Down Expand Up @@ -76,6 +62,7 @@ public function get_terms( $args ) {
'hide_empty' => false,
);
}

global $wp_version;
if ( version_compare( $wp_version, '4.5.0', '>=' ) ) {
$_terms = get_terms( $args );
Expand All @@ -93,6 +80,19 @@ public function get_terms( $args ) {
return $_terms_options;
}

/**
* Get product
*/
public function get_product() {
$_products = wc_get_products( array( 'posts_per_page' => -1 ) );

if ( ! empty( $_products ) && ! is_wp_error( $_products ) ) {
foreach ( $_products as $_product ) {
$_product_options[ $_product->id ] = $_product->name;
}
}
return $_product_options;
}
/**
* Get_settings.
*
Expand Down Expand Up @@ -302,6 +302,36 @@ public function get_settings() {
),
);

$exclude_product = array(
array(
'title' => __( 'Exclude Product/Categories', 'woocommerce-call-for-price' ),
'type' => 'title',
'id' => 'alg_wc_call_for_price_exclude_options',
),
array(
'title' => __( 'Select Product(s)', 'woocommerce-call-for-price' ),
'desc' => __( 'Exclude product', 'woocommerce-call-for-price' ),
'id' => 'alg_call_for_price_exclude_product',
'default' => '',
'type' => 'multiselect',
'class' => 'chosen_select',
'options' => $this->get_product( '_product' ),
),
array(
'title' => __( 'Select Categories(s)', 'woocommerce-call-for-price' ),
'desc' => __( 'Exclude categories', 'woocommerce-call-for-price' ),
'id' => 'alg_call_for_price_exclude_product_cat',
'default' => '',
'type' => 'multiselect',
'class' => 'chosen_select',
'options' => $this->get_terms( 'product_cat' ),
),
array(
'type' => 'sectionend',
'id' => 'alg_call_for_price_exclude_options',
),
);

$advanced_settings = array(
array(
'title' => __( 'Advanced Options', 'woocommerce-call-for-price' ),
Expand Down Expand Up @@ -342,9 +372,8 @@ public function get_settings() {
),
);

return array_merge( $plugin_settings, $general_settings, $button_settings, $force_settings, $advanced_settings );
return array_merge( $plugin_settings, $general_settings, $button_settings, $force_settings, $exclude_product, $advanced_settings );
}

}

endif;
Expand Down
68 changes: 36 additions & 32 deletions includes/class-wc-call-for-price.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,14 @@
* @since 1.0.0
*/
class Alg_WC_Call_For_Price {
/**
* Woocommerce version.
*
* @var $is_wc_below_3_0_0
* @since 3.0.0
*/
public $is_wc_below_3_0_0 = '';

/**
* Constructor.
*
* @version 3.2.3
*/
public function __construct() {

if ( 'yes' === get_option( 'alg_wc_call_for_price_enabled', 'yes' ) ) {
// Class properties.
$this->is_wc_below_3_0_0 = version_compare( get_option( 'woocommerce_version', null ), '3.0.0', '<' );
Expand Down Expand Up @@ -102,7 +96,6 @@ public function __construct() {
add_filter( 'woocommerce_show_variation_price', '__return_true', PHP_INT_MAX );
}
}
add_action( 'admin_enqueue_scripts', array( $this, 'alg_call_for_price_setting_script' ) );
}

/**
Expand All @@ -116,24 +109,6 @@ public function hide_main_variable_price_on_single_product_page( $price_html ) {
return ( is_product() ? '' : $price_html );
}

/**
* Enqueue JS script for deactivate plugin.
*
* @version 3.6.0
* @since 3.6.0
*/
public static function alg_call_for_price_setting_script() {
$plugin_url = plugins_url() . '/woocommerce-call-for-price';
wp_register_script(
'tyche',
$plugin_url . '/includes/js/tyche.js',
array( 'jquery' ),
'3.6.0',
true
);
wp_enqueue_script( 'tyche' );
}

/**
* Get the value of a call for price for zero price products setting.
*
Expand Down Expand Up @@ -292,7 +267,6 @@ public function alg_cfp_empty_price_products_stock_management( $short_desc ) {
}
}
return $short_desc;

}

/**
Expand Down Expand Up @@ -398,15 +372,25 @@ public function make_empty_price_by_product_price( $price, $_product ) {
* @since 3.2.0
*/
public function make_empty_price_per_taxonomy( $price, $_product ) {

$exclude_products = get_option( 'alg_call_for_price_exclude_product', array() );
if ( in_array( $_product->id, $exclude_products ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Warning: Not using strict comparison for in_array; supply true for third argument (WordPress.PHP.StrictInArray.MissingTrueStrict).

return $price;
}
foreach ( array( 'product_cat', 'product_tag' ) as $taxonomy ) {
$term_ids = get_option( 'alg_call_for_price_make_empty_price_' . $taxonomy, '' );
if ( ! empty( $term_ids ) ) {
$product_id = ( $this->is_wc_below_3_0_0 ? $_product->id : ( $_product->is_type( 'variation' ) ? $_product->get_parent_id() : $_product->get_id() ) );
$product_terms = get_the_terms( $product_id, $taxonomy );
$product_id = ( $this->is_wc_below_3_0_0 ? $_product->id : ( $_product->is_type( 'variation' ) ? $_product->get_parent_id() : $_product->get_id() ) );
$product_terms = get_the_terms( $product_id, $taxonomy );
$exclude_categories = get_option( 'alg_call_for_price_exclude_product_cat', array() );
if ( ! empty( $product_terms ) ) {
foreach ( $product_terms as $product_term ) {
if ( in_array( (string) $product_term->term_id, $term_ids, true ) ) {
return '';
if ( in_array( $product_term->term_id, $exclude_categories ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Warning: Not using strict comparison for in_array; supply true for third argument (WordPress.PHP.StrictInArray.MissingTrueStrict).

return $price;
} else {
if ( in_array( (string) $product_term->term_id, $term_ids, true ) ) {
return '';
}
}
}
}
Expand Down Expand Up @@ -441,6 +425,17 @@ public function make_empty_price_out_of_stock( $price, $_product ) {
* @since 3.1.0
*/
public function get_variation_prices_hash( $price_hash, $_product, $display ) {
$exclude_categories = get_option( 'alg_call_for_price_exclude_product_cat', array() );
$exclude_products = get_option( 'alg_call_for_price_exclude_product', array() );
$categories = get_the_terms( $_product->id, 'product_cat' );
foreach ( $categories as $cat ) {
if ( in_array( $cat->term_id, $exclude_categories ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Warning: Not using strict comparison for in_array; supply true for third argument (WordPress.PHP.StrictInArray.MissingTrueStrict).

return $price_hash;
}
}
if ( in_array( $_product->id, $exclude_products ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Warning: Not using strict comparison for in_array; supply true for third argument (WordPress.PHP.StrictInArray.MissingTrueStrict).

return '';
}
$price_hash['alg_call_for_price'] = array(
'force_all' => get_option( 'alg_call_for_price_make_all_empty', 'no' ),
'force_out_of_stock' => apply_filters( 'alg_call_for_price', 'no', 'out_of_stock' ),
Expand Down Expand Up @@ -487,6 +482,7 @@ public function hide_variation_price_required_placeholder() {
* @return The price as per different conditons.
*/
public function fetch_product_price_if_zero_or_empty( $price, $_product ) {

$is_cfp_for_zero_price_enabled = $this->alg_wc_cfp_setting_for_zero_priced_product();
if ( 'no' === $is_cfp_for_zero_price_enabled ) {
if ( '0' === $price ) {
Expand Down Expand Up @@ -517,7 +513,12 @@ public function fetch_product_price_if_zero_or_empty( $price, $_product ) {
* @since 3.0.3
*/
public function make_empty_price( $price, $_product ) {
return $this->fetch_product_price_if_zero_or_empty( $price, $_product );
$exclude_products = get_option( 'alg_call_for_price_exclude_product', array() );
if ( in_array( $_product->id, $exclude_products ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Warning: Not using strict comparison for in_array; supply true for third argument (WordPress.PHP.StrictInArray.MissingTrueStrict).

return $price;
} else {
return $this->fetch_product_price_if_zero_or_empty( $price, $_product );
}
}

/**
Expand Down Expand Up @@ -620,6 +621,7 @@ public function on_empty_price( $price, $_product ) {
$product_type = ( $_product->is_type( 'external' ) ) ? 'external' : 'simple';
}
} else {

$_product_id = ( $_product->is_type( 'variation' ) ) ? $_product->get_parent_id() : $_product->get_id();
if ( $_product->is_type( 'variation' ) ) {
$current_filter = 'woocommerce_variation_empty_price_html';
Expand All @@ -638,6 +640,7 @@ public function on_empty_price( $price, $_product ) {
if ( 'per_product' === $product_type ) {
$view = 'all_views';
} else {

$view = 'single'; // default.
if ( 'woocommerce_variation_empty_price_html' === $current_filter ) {
$view = 'variation';
Expand All @@ -658,6 +661,7 @@ public function on_empty_price( $price, $_product ) {
return $price;
}
}

if ( 'single' === $view || 'variation' === $view ) {
// Label for product page.
$label = get_option(
Expand Down