• Resolved Bastian Fie?inger

    (@bastianfiessinger)


    All in all this plugin does a good job, but there are problems with variations.

    I updated the ut_filter_woo_coupon_is_valid_for_product method and enhanced it in a quick and dirty way to make it work with product variations:

    function ut_filter_woo_coupon_is_valid_for_product( $valid, $product, $instance, $values ) {
    
    	$options = get_post_meta( $instance->get_id(), 'ut_tax_enabled', true );
    
    	if ( $options && $options === 'yes' ) {	
    		$taxonomies = get_post_meta( $instance->get_id(), 'ut_products_taxonomies', true ); 
    
    		if ( ! $taxonomies || ! is_array( $taxonomies ) ) {
    			return false;
    		}
    		$taxonomy_objects = get_object_taxonomies( 'product', 'objects' );
    
    		if ( ! is_array( $taxonomy_objects ) ) {
    			return false;
    		}
    		$all_product_terms = [];
    		foreach ( $taxonomy_objects as $taxonomy_object ) {
    
    			$product_terms = get_the_terms( $product->get_id(), $taxonomy_object->name );
    
                            // check for variations
    			if (is_a($product, 'WC_Product_Variation')) {
    				$parent_product_terms = get_the_terms($product->get_parent_id(), $taxonomy_object->name);
    				foreach ( $parent_product_terms as $product_term ) {
    					array_push( $all_product_terms, $product_term->term_id );
    				}
    			}
    
    			if ( is_array( $product_terms ) && count( $product_terms ) > 0 ) {
    				foreach ( $product_terms as $product_term ) {
    					array_push( $all_product_terms, $product_term->term_id );
    				}
    			}
    		}
                    
                    // finally remove duplicates
    		$all_product_terms = array_unique($all_product_terms);
    
    		foreach ( $taxonomies as $taxonomy ) {
    
    			if ( in_array( $taxonomy, $all_product_terms ) ) {
    				return true;
    			}
    		}	
    		return false;
    	} 
    
    	return
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Bastian Fie?inger

    (@bastianfiessinger)

    edit: forgot the check for if ( is_array( $parent_product_terms ) && count( $parent_product_terms ) > 0 ) { right before pushing the parent product terms

    Thread Starter Bastian Fie?inger

    (@bastianfiessinger)

    for everyone needing a quick fix for this (keep in mind this solution is really quick and dirty but works in my situation – I’ll keep it on the plugin dev to extend it) just replace the content of /includes/coupon-checker.php with the follwing code:

    <?php
    /**
     * @param $valid
     * @param $product
     * @param $instance
     * @param $values
     *
     * @return bool
     */
    function ut_filter_woo_coupon_is_valid_for_product( $valid, $product, $instance, $values ) {
    
    	$options = get_post_meta( $instance->get_id(), 'ut_tax_enabled', true );
    
    	if ( $options && $options === 'yes' ) {	
    		$taxonomies = get_post_meta( $instance->get_id(), 'ut_products_taxonomies', true ); 
    
    		if ( ! $taxonomies || ! is_array( $taxonomies ) ) {
    			return false;
    		}
    		$taxonomy_objects = get_object_taxonomies( 'product', 'objects' );
    
    		if ( ! is_array( $taxonomy_objects ) ) {
    			return false;
    		}
    		$all_product_terms = [];
    		foreach ( $taxonomy_objects as $taxonomy_object ) {
    
    			$product_terms = get_the_terms( $product->get_id(), $taxonomy_object->name );
    
    			if (is_a($product, 'WC_Product_Variation')) {
    				$parent_product_terms = get_the_terms($product->get_parent_id(), $taxonomy_object->name);
    				if ( is_array( $parent_product_terms ) && count( $parent_product_terms ) > 0 ) {
    					foreach ( $parent_product_terms as $product_term ) {
    						array_push( $all_product_terms, $product_term->term_id );
    					}
    				}
    			}
    
    			if ( is_array( $product_terms ) && count( $product_terms ) > 0 ) {
    				foreach ( $product_terms as $product_term ) {
    					array_push( $all_product_terms, $product_term->term_id );
    				}
    			}
    		}
    
    		$all_product_terms = array_unique($all_product_terms);
    
    		foreach ( $taxonomies as $taxonomy ) {
    
    			if ( in_array( $taxonomy, $all_product_terms ) ) {
    				return true;
    			}
    		}	
    		return false;
    	} 
    
    	return $valid;
    }
    
    add_filter( 'woocommerce_coupon_is_valid_for_product', 'ut_filter_woo_coupon_is_valid_for_product', 10, 4 );
    
    
    Plugin Author Unreal Themes

    (@romchyk16)

    Latest version work with variable products.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Not working if Product is Variation’ is closed to new replies.