Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/addf60b5f7c257306988c3177240c8ef to your computer and use it in GitHub Desktop.
Save FrancoStino/addf60b5f7c257306988c3177240c8ef to your computer and use it in GitHub Desktop.
This code, implemented as a WordPress action hook, retrieves and displays discount details for a WooCommerce product using the Advanced Discount Rules plugin. It fetches discount information, iterates through available discount rules, and outputs details like rule ID, start and end dates, and discount specifics.

Woo Discount Rules v2 - Get discount details against product

Preview:
add_action( 'wp_footer', function ()
{
    /** 
     * 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
     */
    $product_id = get_the_ID();
    $product    = wc_get_product( $product_id );

    error_log( 'Product ID: ' . print_r( $product_id, true ) );
    error_log( 'Product: ' . print_r( $product, true ) );

    if ( $product )
    {
        $price_html = $product->get_price_html();
        error_log( 'Price HTML: ' . print_r( $price_html, true ) );

        $discount = apply_filters( 'advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $price_html, $product, 1, 0, 'all', true );
        error_log( 'Discount: ' . print_r( $discount, true ) );

        if ( $discount !== false && isset( $discount[ 'total_discount_details' ] ) && !empty( $discount[ 'total_discount_details' ] ) )
        {
            $total_discounts = $discount[ 'total_discount_details' ];
            error_log( 'Total Discounts: ' . print_r( $total_discounts, true ) );

            foreach ( $total_discounts as $rule_id => $detail )
            {
                if ( class_exists( '\Wdr\App\Controllers\ManageDiscount' ) )
                {
                    $rules = \Wdr\App\Controllers\ManageDiscount::$available_rules;
                    error_log( 'Rules: ' . print_r( $rules, true ) );

                    $start_date = $end_date = array();
                    if ( !empty( $rules ) && is_array( $rules ) )
                    {
                        foreach ( $rules as $rule )
                        {
                            error_log( 'Current Rule: ' . print_r( $rule, true ) );

                            if ( $rule->rule->enabled == 1 && $rule_id == $rule->rule->id )
                            {
                                $start_date_timestamp   = $rule->rule->date_from;
                                $end_date_timestamp     = $rule->rule->date_to;
                                $format                 = "Y-m-d H:i:s";
                                $start_date[ $rule_id ] = date( $format, $start_date_timestamp );
                                $end_date[ $rule_id ]   = date( $format, $end_date_timestamp );

                                echo '<div style="display: block; margin: 10px 0;">';
                                echo '<pre>';
                                print_r( $rule );
                                echo '</pre>';
                                echo '<p>Rule ID: ' . esc_html( $rule_id ) . '</p>';
                                echo '<p>Start Date: ' . esc_html( $start_date[ $rule_id ] ) . '</p>';
                                echo '<p>End Date: ' . esc_html( $end_date[ $rule_id ] ) . '</p>';
                                echo '<p>Discount Details: ' . esc_html( print_r( $detail, true ) ) . '</p>';
                                echo '</div>';
                            }
                        }
                    }
                }
            }
        }
    }
} );
Associated Context
Type Code Snippet ( .php )
Associated Tags add_action wp_footer wc_get_product get_the_ID error_log apply_filters advanced_woo_discount_rules_get_product_discount_price_from_custom_price discounted_price total_discounts Framework: WooCommerce WordPress WooCommerce Discount Rules Product Price Advanced Discount Rules PHP eCommerce WordPress Hooks Custom Price Discount Calculation
💡 Smart Description This code snippet adds an action to the wp_footer page. It retrieves discount details of a product with custom price, calculates its discount based on their total discounts and returns it as array if there is no item in cart or false for get discount
This code, implemented as a WordPress action hook, retrieves and displays discount details for a WooCommerce product using the Advanced Discount Rules plugin. It fetches discount information, iterates through available discount rules, and outputs details like rule ID, start and end dates, and discount specifics.
🔎 Suggested Searches wp_footer add_action wp_footer get discount details
wp_footer apply_filters advanced_woo_discount_rules_get_product_discount_price_from_custom_price
wp_footer calculate discount from product price
wp_footer return array of discount details based on custom price
wp_footer set tax discounts
WooCommerce discount plugins
Advanced Discount Rules for WooCommerce
WordPress custom price display
get product discount programmatically WooCommerce
WooCommerce display discount details
Related Links https://developer.wordpress.org/reference/hooks/wp_footer/
Related People Davide Ladisa, Sergio Pellicani
Sensitive Information No Sensitive Information Detected
Shareable Link https://davideladisa.pieces.cloud/?p=712d41b314
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment