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

feat: allow shop standard fields to be included/excluded from coupons #233

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
53 changes: 53 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ public static function init() {
add_action('woocommerce_order_details_before_order_table', __NAMESPACE__ . '\WooCommerce::woocommerce_order_details_before_order_table');
}

// Exclude coupon for producs with custom shopstandards fields marked as exclude.
add_filter('woocommerce_coupon_get_discount_amount', __NAMESPACE__ . '\WooCommerce::apply_product_shop_standard_field_validations_to_coupon',11,5);

}

/**
Expand Down Expand Up @@ -345,6 +348,56 @@ public static function register_acf() {
'instruction_placement' => 'label',
'active' => 1,
]);

acf_add_local_field_group([
'key' => 'acf_shop_standard_coupon_exclude',
'title' => __('Shop standard fields to exclude from coupon', Plugin::L10N),
'fields' => [
[
'key' => 'acf_shop_standard_coupon_validation',
'name' => 'acf_shop_standard_coupon_validation',
'label' => __('Validate Shop standard field on coupon.',Plugin::L10N),
'type' => 'repeater',
'layout' => 'table',
'instructions' => __('List of shop standard fields to validate.', Plugin::L10N),
'button_label' => __('Add validation', Plugin::L10N),
'sub_fields' => [
[
'key' => 'acf_shop_standard_product_field',
'name' => 'acf_shop_standard_product_field',
'label' => __('Shop standard product field', Plugin::L10N),
'type' => 'select',
'choices' => woocommerce::get_product_fields(),
'allow_null' => 0,
'multiple' => 0,
'ui' => 1,
],
[
'key' => 'acf_shop_standard_include_or_exclude',
'name' => 'acf_shop_standard_include_or_exclude',
'label' => __('Coupon validaton for field.', Plugin::L10N),
'type' => 'select',
'choices' => [
'INCLUDE' => 'INCLUDE',
'EXCLUDE' => 'EXCLUDE',
],
'allow_null' => 0,
'multiple' => 0,
'ui' => 1,
],
],
],
],
'location' => [
[
[
'param' => 'post_type',
'operator' => '==',
'value' => 'shop_coupon',
],
],
],
]);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions src/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -1548,4 +1548,50 @@ public static function woocommerce_order_details_before_order_table($order) {
}
}

/**
* Validates if the product cart item has a field marked as EXCLUDE in the coupon and applies the discount accordingly.
*
* @return int Discount value for the cart item.
*/
public static function apply_product_shop_standard_field_validations_to_coupon($discount, $discounting_amount, $cart_item, $single, $coupon) {

// Gets the coupon id.
$coupon_id = $coupon->get_id();

// Gets the repeater 'acf_shop_standard_coupon_validation' value list.
$shop_standards_fields = get_field('acf_shop_standard_coupon_validation', $coupon_id);

$exclude_shop_standard_fields = [];

// Gets the shop standard fields of the coupon marked as EXCLUDE.
if (!empty($shop_standards_fields) && is_array($shop_standards_fields)) {
foreach ($shop_standards_fields as $field) {
// Get sub field values.
$acf_shop_standards_product_field = $field['acf_shop_standard_product_field'];
$included_or_excluded = $field['acf_shop_standard_include_or_exclude'];

if($included_or_excluded === 'EXCLUDE'){
$exclude_shop_standard_fields[] = $acf_shop_standards_product_field;
}
}
}

// Get product fields matching with coupon fields marked as EXCLUDE.
$product_id = $cart_item['product_id'];
$product = wc_get_product($product_id);
$product_fields = [];

foreach ($exclude_shop_standard_fields as $field) {
$product_shop_standard_field=$product->get_meta($field,true);
if($product_shop_standard_field && wc_string_to_bool($product_shop_standard_field)){
$product_fields[] = $field;
}
}

if (!empty($product_fields)) {
return 0;
}
return $discount;
}

}