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 / Discount rules v2: For getting discounted price of a product
Last active October 27, 2023 05:45
Discount rules v2: For getting discounted price of a product
/**
* Get the discount details of given product with custom price
* @param $price float/integer
* @param $product object (Product object Example: wc_get_product($product_id))
* @param $quantity int
* @param $custom_price float/integer (0 for calculate discount from product price)
* @param $return_details string (Default value 'discounted_price' accepted values = 'discounted_price','all')
* @param $manual_request boolean (Default value false: pass this as true for get discount even if there is no item in cart)
* @param $is_cart boolean (Default value true)
* @return array|float|int - is product has no discounts, returns $price;else return array of discount details based on $return_details
@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_table.php
Created October 19, 2022 11:18
Discount rules v2: Show only minimum range with + in discount table
<?php
/**
* Discount table
*
* This template can be overridden by copying it to yourtheme/advanced_woo_discount_rules/discount_table.php.
*
* HOWEVER, on occasion Discount rules will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
@AshlinRejo
AshlinRejo / Woo Email: WooCommerce pre-order date through custom code
Created February 26, 2019 07:11
Woo Email: WooCommerce pre-order date through custom code
<?php
/**
* Custom code shortcode
*
* This template can be overridden by copying it to yourtheme/plugin-folder-name/woo_mail/custom_code.php.
* @var $order WooCommerce order
* @var $email_id WooCommerce email id (new_order, cancelled_order)
* @var $sent_to_admin WooCommerce email send to admin
* @var $plain_text WooCommerce email format
* @var $email WooCommerce email object
@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 / Woo Discount: Get sale price from Woo Discount Rules
Created July 16, 2019 07:59
Woo Discount: Get sale price from Woo Discount Rules
function woocommerce_product_get_sale_price_from_woo_discount_rules($value, $product){
if(!is_admin()){
global $flycart_woo_discount_rules;
if(!empty($flycart_woo_discount_rules)){
$discounted_price = $flycart_woo_discount_rules->pricingRules->getDiscountPriceOfProduct($product);
if($discounted_price !== null){
$value = $discounted_price;
}
}
}
@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')){