Skip to content

Commit

Permalink
Merge pull request #311 from athemes/product-labels-pre-orders-tags-s…
Browse files Browse the repository at this point in the history
…election

Product labels & Pre-orders tags selection
  • Loading branch information
thenahidul authored Oct 22, 2024
2 parents 94c9310 + 244f0a4 commit dec4ffd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion inc/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ function merchant_is_product_excluded( $product_id, $args = array() ) {
$display_rule = $args['rules_to_display'] ?? $args['display_rules'] ?? $args['rules_to_apply'] ?? 'products';

// Exclude products
if ( in_array( $display_rule, array( 'all', 'all_products', 'categories', 'by_category' ), true ) ) {
if ( in_array( $display_rule, array( 'all', 'all_products', 'categories', 'by_category', 'tags', 'by_tags' ), true ) ) {
$excluded_product_ids = $args['excluded_products'] ?? array();
$excluded_product_ids = merchant_parse_product_ids( $excluded_product_ids );

Expand Down
17 changes: 15 additions & 2 deletions inc/modules/pre-orders/admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'id' => 'rules',
'type' => 'flexible_content',
'sorting' => true,
'duplicate' => true,
'accordion' => true,
'style' => Merchant_Pre_Orders::MODULE_ID . '-style default',
'button_label' => esc_html__( 'Add New Pre-Order', 'merchant' ),
Expand All @@ -46,8 +47,9 @@
'type' => 'select',
'title' => esc_html__( 'Trigger', 'merchant' ),
'options' => array(
'product' => esc_html__( 'Specific product', 'merchant' ),
'category' => esc_html__( 'Specific category', 'merchant' ),
'product' => esc_html__( 'Specific products', 'merchant' ),
'category' => esc_html__( 'Specific categories', 'merchant' ),
'tags' => esc_html__( 'Specific tags', 'merchant' ),
),
'default' => 'product',
),
Expand All @@ -70,6 +72,17 @@
'desc' => esc_html__( 'Select the category or categories for which the products will be available for pre-order.', 'merchant' ),
'condition' => array( 'trigger_on', '==', 'category' ),
),
array(
'id' => 'tag_slugs',
'type' => 'select_ajax',
'title' => esc_html__( 'Tags', 'merchant' ),
'source' => 'options',
'multiple' => true,
'options' => Merchant_Admin_Options::get_tag_select2_choices(),
'placeholder' => esc_html__( 'Select tags', 'merchant' ),
'desc' => esc_html__( 'Select the tag or tags for which the products will be available for pre-order.', 'merchant' ),
'condition' => array( 'trigger_on', '==', 'tags' ),
),
array(
'id' => 'discount_toggle',
'type' => 'switcher',
Expand Down
13 changes: 10 additions & 3 deletions inc/modules/pre-orders/class-pre-orders-main-functionality.php
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,10 @@ private static function is_valid_rule( $rule ) {
return false;
}

if ( 'tags' === $rule['trigger_on'] && empty( $rule['tag_slugs'] ) ) {
return false;
}

if ( isset( $rule['discount_toggle'] ) && $rule['discount_toggle'] === true ) {
if ( ! isset( $rule['discount_type'] ) ) {
return false;
Expand Down Expand Up @@ -1278,11 +1282,14 @@ public static function available_product_rule( $product_id ) {
$available_rule = $rule;
break;
}
if ( 'category' === $rule['trigger_on'] ) {
$terms = get_the_terms( $product_id, 'product_cat' );
if ( 'category' === $rule['trigger_on'] || 'tags' === $rule['trigger_on'] ) {
$taxonomy = $rule['trigger_on'] === 'category' ? 'product_cat' : 'product_tag';
$slugs = $rule['trigger_on'] === 'category' ? ( $rule['category_slugs'] ?? array() ) : ( $rule['tag_slugs'] ?? array() );

$terms = get_the_terms( $product_id, $taxonomy );
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $rule['category_slugs'], true ) ) {
if ( in_array( $term->slug, $slugs, true ) ) {
$available_rule = $rule;
break;
}
Expand Down
15 changes: 14 additions & 1 deletion inc/modules/product-labels/admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
'all_products' => esc_html__( 'All Products', 'merchant' ),
'specific_products' => esc_html__( 'Specific Products', 'merchant' ),
'by_category' => esc_html__( 'Specific Categories', 'merchant' ),
'by_tags' => esc_html__( 'Specific tags', 'merchant' ),
),
'default' => 'products_on_sale',
),
Expand Down Expand Up @@ -264,6 +265,18 @@
'condition' => array( 'display_rules', '==', 'by_category' ),
),

array(
'id' => 'product_tags',
'type' => 'select_ajax',
'title' => esc_html__( 'Tags', 'merchant' ),
'source' => 'options',
'multiple' => true,
'options' => Merchant_Admin_Options::get_tag_select2_choices(),
'placeholder' => esc_html__( 'Select tags', 'merchant' ),
'desc' => esc_html__( 'Select the product tags that will show the label.', 'merchant' ),
'condition' => array( 'display_rules', '==', 'by_tags' ),
),

array(
'id' => 'product_ids',
'type' => 'products_selector',
Expand All @@ -279,7 +292,7 @@
'title' => esc_html__( 'Exclude Products', 'merchant' ),
'multiple' => true,
'desc' => esc_html__( 'Exclude products from this label.', 'merchant' ),
'condition' => array( 'display_rules', 'any', 'all_products|by_category|featured_products|new_products|products_on_sale|out_of_stock' ),
'condition' => array( 'display_rules', 'any', 'all_products|by_category|by_tags|featured_products|new_products|products_on_sale|out_of_stock' ),
),

array(
Expand Down
15 changes: 9 additions & 6 deletions inc/modules/product-labels/class-product-labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,15 @@ public function get_labels( $product, $context = 'both' ) {
break;

case 'by_category':
$categories = $label['product_cats'] ?? array();
$categories = is_array( $categories ) ? $categories : (array) $categories;

if ( ! empty( $categories ) && $this->is_in_category( $product, $categories ) ) {
$product_labels_html .= $this->label( $label );
}
case 'by_tags':
$taxonomy = $display_rule === 'by_category' ? 'product_cat' : 'product_tag';
$slugs = $display_rule === 'by_category' ? ( $label['product_cats'] ?? array() ) : ( $label['product_tags'] ?? array() );
foreach ( $slugs as $slug ) {
if ( has_term( $slug, $taxonomy, $product->get_id() ) ) {
$product_labels_html .= $this->label( $label );
break;
}
}
break;

case 'out_of_stock':
Expand Down

0 comments on commit dec4ffd

Please sign in to comment.