PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label coupon. Show all posts
Showing posts with label coupon. Show all posts

Sunday, February 6, 2022

[FIXED] Exclude variations with 2 specific attribute terms from coupon usage in Woocommerce

 February 06, 2022     coupon, custom-taxonomy, php, woocommerce, wordpress     No comments   

Issue

I need to prevent coupons being used if customer have any specific product variations in their cart with following attribute terms:

  • attribute_pa_style => swirly
  • attribute_pa_style => circle

I've looked through the Woocommerce scripts that apply to restricting specific products and specific categories, but can't figure it out with regard to attributes and all coupons.

Any help is appreciated.


Solution

This can be done using woocommerce_coupon_is_valid filter hook this way:

add_filter( 'woocommerce_coupon_is_valid', 'check_if_coupons_are_valid', 10, 3 );
function check_if_coupons_are_valid( $is_valid, $coupon, $discount ){
    // YOUR ATTRIBUTE SETTINGS BELOW:
    $taxonomy   = 'pa_style';
    $term_slugs = array('swirly', 'circle');

    // Loop through cart items and check for backordered items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        foreach( $cart_item['variation'] as $attribute => $term_slug ) {
            if( $attribute === 'attribute_'.$taxonomy && in_array( $term_slug, $term_slugs ) ) {
                $is_valid = false; // attribute found, coupons are not valid
                break; // Stop and exit from the loop
            }
        }
    }
    return $is_valid;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.



Answered By - LoicTheAztec
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, January 27, 2022

[FIXED] Add coupon names and percentage to WooCommerce view order details and email notifications

 January 27, 2022     coupon, email-notifications, orders, woocommerce, wordpress     No comments   

Issue

I used Add the Coupon Code names to Woocommerce View Order details and email notifications to inspire my snippet.

I'm trying to expand the snippet to also include the coupon percentage amount in brackets, if the coupon type is a percentage discount.

This is what it should look like:

enter image description here

Here is my attempt. Any ideas if this is correct:

add_filter( 'woocommerce_get_order_item_totals', 'add_coupons_codes_line_to_order_totals_lines', 10, 3 );
function add_coupons_codes_line_to_order_totals_lines( $total_rows, $order, $tax_display ) {
    // Exit if there is no coupons applied
    if( sizeof( $order->get_used_coupons() ) == 0 )
        return $total_rows;

    $new_total_rows = []; // Initializing

    foreach($total_rows as $key => $total ){
        $new_total_rows[$key] = $total;

        if( $key == 'discount' ){
            // Get applied coupons
            $applied_coupons = $order->get_used_coupons();
            
        if( $applied_coupons->discount_type == 'percent'){
             // Get applied coupon percentge
            $applied_coupons_percentage = $applied_coupons->coupon_amount;
            }

            // Insert applied coupon codes in total lines after discount line
            $new_total_rows['coupon_codes'] = array(
                'label' => __('Applied coupons:', 'woocommerce'),
                'value' => implode( ', ', $applied_coupons '<p> ({$applied_coupons_percentage}%)</p>' ),
            );
        }
    }

    return $new_total_rows;
}

I use this to display the coupon percentage on the cart page. I tried to incorporate this into my snippet above:

function my_coupon_percentage_cart($value, $coupon)
{
    if($coupon->discount_type == 'percent' && !empty($coupon->coupon_amount))
    {
        $amt = "<br><br><p><em><strong>{$coupon->coupon_amount}% OFF on Your Order</strong></em></p>";   
    }

    return $value.$amt;
}
add_filter('woocommerce_cart_totals_coupon_html','my_coupon_percentage_cart',10,2);


Solution

Besides the fact that your code contains mistakes, it also contains some outdated code

  • get_used_coupons() is deprecated since 3.7.0 - Replaced with better named method to reflect the actual data being returned.
  • $coupon->discount_type has been replaced with $coupon->get_discount_type()
  • $coupon->coupon_amount has been replaced with $coupon->get_amount()

So you get:

function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
    // Exit if there is no coupons applied
    if ( sizeof( $order->get_coupon_codes() ) == 0 ) return $total_rows;

    $new_total_rows = []; // Initializing

    foreach( $total_rows as $key => $total ) {
        $new_total_rows[$key] = $total;

        if ( $key == 'discount' ) {
            // Get used coupon codes only
            $coupon_codes = $order->get_coupon_codes();
            
            // Loop through WC_Order_Item_Coupon objects
            foreach ( $coupon_codes as $index => $coupon_code ) {
                // Get an instance of the WC_Coupon Object
                $coupon = new WC_Coupon( $coupon_code );

                // Discount type = percent & amount NOT empty
                if ( $coupon->get_discount_type() == 'percent' && ! empty ( $coupon->get_amount() ) ) {
                    $coupon_codes[$index] = $coupon_code . ' (' . $coupon->get_amount() . '%)';
                }
            }
            

            // Insert applied coupon codes in total lines after discount line
            $new_total_rows['coupon_codes'] = array(
                'label' => __( 'Applied coupons:', 'woocommerce' ),
                'value' => implode( ', ', $coupon_codes ),
            );
        }
    }

    return $new_total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );


Answered By - 7uc1f3r
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing