• hi this is the best pricing plugin i had even used! just checking if the below can be done:

    set up a role discount with product filter for ‘VIP’ role customer, in product category pages and single product pages show “vip discount applied” next to the sale price.

    I checked the sample code and found “Check the active rules for the product” is quite relevant. But the thing is even if the user is not eligible for the role discount, the getActiveRulesForProduct still returning role discount rules.

    Thank you very much!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support thisisirene

    (@thisisirene)

    Hello, @28kids!

    Could you, please, replace the Role discount section to the Cart Condition “Role”? Here is an example: https://snipboard.io/YoJipa.jpg

    Thread Starter 28kids

    (@28kids)

    thank you i did and it works fine, just wondering how to display it like

    $100 $80 *vip price*

    in single product page?

    Plugin Author algol.plus

    (@algolplus)

    Could you share your current code ?

    Thread Starter 28kids

    (@28kids)

    add_action("woocommerce_single_product_summary", function () {
        global $product;
     
     function getProtectedValue($obj, $name) {
      $array = (array)$obj;
      $prefix = chr(0).'*'.chr(0);
      return $array[$prefix.$name];
    }
        $calculateProduct = function ($product) {
            $rules = [];
     
            if ($product instanceof WC_Product_Variable) {
                foreach ( $product->get_available_variations("obj") as $variation ) {
                    foreach (adp_functions()->getActiveRulesForProduct($variation, 1) as $rule) {
                        $rules[$rule->getId()] = $rule;
                    }
                }
            } elseif ($product instanceof WC_Product_Simple) {
                foreach (adp_functions()->getActiveRulesForProduct($product, 1) as $rule) {
                    $rules[$rule->getId()] = $rule;
                }
            }
     
            return $rules;
        };
     
        $rules = [];
        if ($product instanceof WC_Product_Grouped) {
            $children = array_filter(
                array_map(
                    'wc_get_product',
                    $product->get_children()
                ),
                'wc_products_array_filter_visible_grouped'
            );
     
            foreach ($children as $childProduct) {
                $rules = $calculateProduct($childProduct);
            }
        } else {
            $rules = $calculateProduct($product);
        }
     
        if (count($rules) === 0) {
            echo "";
        }
        else{
            foreach( $rules as $r ) {
    
           $check_role = $r->getRoleDiscounts();
                   foreach( $check_role as $c ) {
    
    	                if (in_array('vip', getProtectedValue($c,'roles'))) {
                            echo 'vip discount applied';
                            break;
    	                }
                   }
            }
        }
    });
    
    Plugin Support thisisirene

    (@thisisirene)

    @28kids, please, use this code:

    add_action("woocommerce_get_price_html", function ($priceHtml, $product) {
        function getProtectedValue($obj, $name) {
            $array = (array)$obj;
            $prefix = chr(0).'*'.chr(0);
            return $array[$prefix.$name];
        }
        $calculateProduct = function ($product) {
            $rules = [];
    
            if ($product instanceof WC_Product_Variable) {
                foreach ( $product->get_available_variations("obj") as $variation ) {
                    foreach (adp_functions()->getActiveRulesForProduct($variation, 1) as $rule) {
                        $rules[$rule->getId()] = $rule;
                    }
                }
            } elseif ($product instanceof WC_Product_Simple) {
                foreach (adp_functions()->getActiveRulesForProduct($product, 1) as $rule) {
                    $rules[$rule->getId()] = $rule;
                }
            }
    
            return $rules;
        };
    
        $rules = [];
        if ($product instanceof WC_Product_Grouped) {
            $children = array_filter(
                array_map(
                    'wc_get_product',
                    $product->get_children()
                ),
                'wc_products_array_filter_visible_grouped'
            );
    
            foreach ($children as $childProduct) {
                $rules = $calculateProduct($childProduct);
            }
        } else {
            $rules = $calculateProduct($product);
        }
    
        if (count($rules) === 0) {
            return $priceHtml;
        }
        else{
            foreach( $rules as $r ) {
    
                $check_role = $r->getRoleDiscounts();
                foreach( $check_role as $c ) {
    
                    if (in_array('vip', getProtectedValue($c,'roles'))) {
                        $priceHtml .= ' vip discount applied';
                        break;
                    }
                }
            }
        }
    
        return $priceHtml;
    }, PHP_INT_MAX, 2);
    Thread Starter 28kids

    (@28kids)

    the problem of the code is, even the customer is not eligible for the role discount, the price_html still return as ‘vip discount applied’

    Plugin Author algol.plus

    (@algolplus)

    please, share the screenshot of your rule.

    or better – submit system report as new ticket to https://algolplus.freshdesk.com/

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘display role discount label next to price’ is closed to new replies.