Skip to content

Instantly share code, notes, and snippets.

View AshlinRejo's full-sized avatar
🏠
Working from home

Ashlin AshlinRejo

🏠
Working from home
View GitHub Profile
@AshlinRejo
AshlinRejo / set_discount_adjustment.php
Created May 26, 2023 04:42
Discount rules v2: Handle set discount on having low price for few item
/**
* To handle the low price product in set discount.
* Works from Discount rules v2.6.1
*/
add_filter('advanced_woo_discount_rules_matched_set_discount_range', function ($selected_bundle_range, $product, $rule, $cart_items, $is_cart){
if(class_exists('\Wdr\App\Helpers\Woocommerce')){
if($is_cart){
if(!empty($selected_bundle_range->type) && $selected_bundle_range->type == 'fixed_set_price'){
if(!empty($cart_items)){
$discounted_price = $selected_bundle_range->value;
@AshlinRejo
AshlinRejo / has_discount.php
Last active April 14, 2023 10:10
Discount rule v2: Check for any possible discount
if(!function_exists('isPossibleToHaveDiscountThroughWDR')) {
function isPossibleToHaveDiscountThroughWDR() {
$status = false;
if (class_exists('\Wdr\App\Controllers\DiscountCalculator')) {
$rules = \Wdr\App\Controllers\DiscountCalculator::$rules;
$cart = \Wdr\App\Helpers\Woocommerce::getCart();
foreach ($rules as $rule) {
if ($rule->isEnabled()) {
if ($rule->hasConditions()) {
$conditions_passed = $rule->isCartConditionsPassed($cart);
@AshlinRejo
AshlinRejo / Discount rules v2: Support style attribute in span tags
Created February 1, 2023 13:12
Discount rules v2: Support style attribute in span tags
add_filter('advanced_woo_discount_rules_allowed_html_elements_and_attributes', function($allowed_html) {
$allowed_html['span'] = array('style' => array(), 'class' => array());
return $allowed_html;
});
@AshlinRejo
AshlinRejo / Disocunt Rules v2: Apply sale place for non apply quantities while enabled discount apply from regular price.php
Last active January 23, 2023 10:21
Disocunt Rules v2: Apply sale place for non apply quantities while enabled discount apply from regular price.
add_filter('advanced_woo_discount_rules_discount_prices_of_product', function ($discount_prices, $product, $quantity, $cart_item){
$regular_price = $product->get_regular_price();
$sale_price = $product->get_price();
$default_discount = $regular_price - $sale_price;
$non_applied_qty = $discount_prices['discount_lines']['non_applied']['quantity'];
if(($default_discount) > 0 && $non_applied_qty > 0){
$discount_prices['initial_price'] = $sale_price;
$discount_prices['discounted_price'] = $discount_prices['discounted_price'] - (($default_discount*$non_applied_qty)/$quantity);
}
@AshlinRejo
AshlinRejo / Discount rules v2: Run cron hourly.php
Created November 30, 2022 12:24
Discount rules v2: Run cron hourly
add_filter('advanced_woo_discount_rules_scheduled_rebuild_on_sale_index_event_recurrence', function ($recurrence){
$recurrence = 'hourly';
return $recurrence;
}, 10, 1);
@AshlinRejo
AshlinRejo / Discount rule v2: Check subtotal less than condition even when cart has no item.php
Created November 29, 2022 13:25
Discount rule v2: Check subtotal less than condition even when cart has no item
add_filter('advanced_woo_discount_rules_is_conditions_passed', function($result, $rule_object, $rule){
$conditions = $rule_object->getConditions();
$cart = null;
if(function_exists('WC')){
$cart = WC()->cart->get_cart();
}
if(empty($cart) && !empty($conditions) && count((array)$conditions) == 1)
foreach ($conditions as $condition) {
$type = isset($condition->type) ? $condition->type : NULL;
if($type == 'cart_subtotal' && ($condition->options->operator == 'less_than_or_equal' || $condition->options->operator == 'less_than')){
@AshlinRejo
AshlinRejo / Discount rules: Enable onsale events which disable by theme Goya.php
Created November 26, 2022 09:46
Discount rules: Enable onsale events which disable by theme Goya
add_action('wp_loaded', function() {
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
}, 100);
// To override Cart Page Overrides (increase priority to higher)
add_action('wp_loaded', function() {
if(class_exists('\Wdr\App\Router')){
remove_action('woocommerce_before_calculate_totals', array(\Wdr\App\Router::$manage_discount, 'applyCartProductDiscount'), 1000);
add_action('woocommerce_before_calculate_totals', array(\Wdr\App\Router::$manage_discount, 'applyCartProductDiscount'), PHP_INT_MAX);
remove_all_filters('woocommerce_cart_item_price');
add_filter('woocommerce_cart_item_price', array(\Wdr\App\Router::$manage_discount, 'getCartPriceHtml'), 1000, 3);
if (has_filter('woocommerce_cart_item_subtotal', array(\Wdr\App\Router::$manage_discount, 'getCartProductSubtotalPriceHtml'))) {
add_filter('woocommerce_cart_item_subtotal', array(\Wdr\App\Router::$manage_discount, 'getCartProductSubtotalPriceHtml'), 10, 3);
}
@AshlinRejo
AshlinRejo / Discount rules v2: Fix for Mini cart doesn't refresh in Floating Sticky Cart for WooCommerce
Created November 21, 2022 06:35
Discount rules v2: Fix for Mini cart doesn't refresh in Floating Sticky Cart for WooCommerce
add_action('wp_footer','woo_disocunt_rules_slide_cart_fix');
function woo_disocunt_rules_slide_cart_fix(){
?>
<script type="text/javascript">
// Ready state
(function($){
$( document.body ).on( 'added_to_cart removed_from_cart', function(){
jQuery( document.body ).trigger( 'wc_fragment_refresh' );
jQuery( document.body ).trigger( 'wc_fragment_refresh' );
});
@AshlinRejo
AshlinRejo / Discount rule v2: Remove menu for specific user role
Created November 4, 2022 10:48
Discount rule v2: Remove menu for specific user role
add_action('admin_menu', function (){
$user_role_to_remove = 'administrator'; //Replace the role for which we need to remove menu
$user = (is_user_logged_in()) ? get_user_by('ID', get_current_user_id()) : NULL;
if (!empty($user) && isset($user->roles) && !empty($user->roles)) {
if(in_array($user_role_to_remove, $user->roles)){
global $submenu;
if (isset($submenu['woocommerce'])) {
remove_submenu_page('woocommerce', 'woo_discount_rules');
}
}