Skip to content

Instantly share code, notes, and snippets.

@sarancartrabbit
sarancartrabbit / Discount rules v2: To show the default variant price for strikeout
Last active January 19, 2023 05:52
Discount rules v2: To show the default variant price for strikeout
add_filter('advanced_woo_discount_rules_strikeout_price_html', function ($html, $original_price, $discounted_price, $is_variable_product, $initial_price_html, $separator) {
global $product;
if ($is_variable_product && !empty($product) && is_object($product)) {
foreach ($product->get_available_variations() as $variation) {
$default_attributes = true;
foreach ($product->get_default_attributes() as $default_key => $default_value) {
if ($variation['attributes']['attribute_' . $default_key] != $default_value) {
$default_attributes = false;
break;
}
@sarancartrabbit
sarancartrabbit / Discount rules v2: To show the default variant price for strikeout
Created January 19, 2023 06:03
Discount rules v2: To show the default variant price for strikeout
add_filter('advanced_woo_discount_rules_strikeout_price_html', function ($html, $original_price, $discounted_price, $is_variable_product, $initial_price_html, $separator) {
global $product;
if ($is_variable_product && !empty($product) && is_object($product)) {
foreach ($product->get_available_variations() as $variation) {
$default_attributes = true;
foreach ($product->get_default_attributes() as $default_key => $default_value) {
if ($variation['attributes']['attribute_' . $default_key] != $default_value) {
$default_attributes = false;
break;
}
@sarancartrabbit
sarancartrabbit / Discount rules v2: To show the round off price
Last active January 19, 2023 11:45
Discount rules v2: To show the round off price
add_filter('advanced_woo_discount_rules_discount_prices_of_product', function ($discount_prices, $product, $quantity, $cart_item){
if (function_exists('wc_get_price_decimals')) {
$precision = 1;
if(isset($discount_prices['discounted_price'])){
$discount_prices['discounted_price'] = round($discount_prices['discounted_price'], $precision);
}
if(isset($discount_prices['discounted_price_with_tax'])){
$discount_prices['discounted_price_with_tax'] = round($discount_prices['discounted_price_with_tax'], $precision);
}
if(isset($discount_prices['discount_lines'])){
@sarancartrabbit
sarancartrabbit / Woo Discount v2: Show discount table only on product page
Created January 27, 2023 14:09
Woo Discount v2: Show discount table only on product page
add_action('wp_head', function() {
if(!is_single()){
?>
<style>.awdr-bulk-customizable-table{display:none}</style>
<?php
}
});
@sarancartrabbit
sarancartrabbit / Woo Discount Rules: For changing the sale tag text
Created February 1, 2023 10:03
Woo Discount Rules: For changing the sale tag text
add_filter('woocommerce_sale_flash', function ($html, $post, $_product) {
return '<span class="sale" style="background-color:#ddd;">Aktion!</span>';
},10,3);
@sarancartrabbit
sarancartrabbit / Discount rules v2:Discount rules v2: Product based get discount price
Created February 1, 2023 13:03
Discount rules v2: Product based get discount price
add_filter('wc_add_to_cart_message_html', function ($message, $products, $show_qty){
foreach ( $products as $product_id => $qty ) {
$product = wc_get_product( $product_id);
$product_price = $product->get_price();
$discount = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $product_price, $product, $qty, $product_price, 'discounted_price', false, true);
if($discount !== false) {
$message .= " Discounted price: ". $discount; //HERE YOU GET DISCOUNTED PRICE
}
}
return $message;
@sarancartrabbit
sarancartrabbit / Woo Discount v2: Get discount percentage of the product
Created February 3, 2023 04:51
Woo Discount v2: Get discount percentage of the product
add_filter('advanced_woo_discount_rules_strikeout_price_html', function ($html, $original_price, $discounted_price, $is_variable_product, $initial_price_html, $separator, $original_price_raw, $discounted_price_raw) {
if ($original_price != $discounted_price) {
if ($initial_price_html) {
$html = '<ins>' . $discounted_price . '</ins>' . $separator . '<del>' . $initial_price_html . '</del>';
} else {
$html = '<ins>' . $discounted_price . '</ins>' . $separator . '<del>' . $original_price . '</del>';
}
$percentage = 0;
if(!$is_variable_product){
if($original_price_raw >0)
@sarancartrabbit
sarancartrabbit / Discount rules v2: To show the both sale badges
Created February 10, 2023 11:21
Discount rules v2: To show the both sale badges
<?php
/**
* Product loop sale flash
*
* This template can be overridden by copying it to yourtheme/woocommerce/loop/sale-flash.php.
*
* HOWEVER, on occasion WooCommerce 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
@sarancartrabbit
sarancartrabbit / Discount rules v2: To show discount rule name in admin panel
Created February 16, 2023 12:49
Discount rules v2: To show discount rule name in admin panel
add_action('admin_menu', function () {
if (!is_admin()) return;
if(class_exists('\Wdr\App\Router')){
$admin = \Wdr\App\Router::$admin;
global $submenu;
add_submenu_page(
'woocommerce',
__('Woo Discount Rules', 'woo-discount-rules'),
__('Woo Discount Rules', 'woo-discount-rules'),
'manage_woocommerce', WDR_SLUG,
@sarancartrabbit
sarancartrabbit / Discount rules v2: To check the parent category in the product
Last active February 21, 2023 11:55
Discount rules v2: To check the parent category in the product
if(!function_exists('awdrcheckParentCategory')){
function awdrcheckParentCategory ($categories){
foreach($categories as $id) {
if ($term = get_term($id)) {
if ($parent_id = $term->parent) {
if (!in_array($parent_id, $categories)) {
$categories = array_merge($categories, awdrcheckParentCategory(array($parent_id)));
}
}
}