Search code examples
phpwordpresswoocommerce

woocommerce_available_variation filter no longer working after Woocommerce 3 update


A few custom codes I've written for woocommerce aren't working properly after updating to woocommerce 3. In this particular case, it's the woocommerce_available_variation filter. I used it to dynamically insert product variation descriptions based on the product attributes.

As of now I am getting this error message:

Catchable fatal error: Object of class WC_Product_Download could not be converted to string in /home/silent48/public_html/wp/wp-content/plugins/woocommerce/includes/wc-product-functions.php on line 376

What would I need to do to the below code to make it compliant with 3.0?

add_filter( 'woocommerce_available_variation', 'change_variation_descriptions');
function change_variation_descriptions( $descriptions) {


global $post, $woocommerce;


    $basicmp3des = '<div class="licensedetails">
                    <li>-Delivered in mp3 format instantly after purchase</li>
                    <li>-Synchronization rights are granted</li>
                    <li>-One commercial use is permitted (ie: mixtape, album, etc)</li>
                    <li>-You may distribute up to 5000 profitable units</li>
                    <li>-Includes non-exclusive contract agreement (e-signed at checkout)</li>
                    <li>-Send me a quote to inquire about Exclusive License purchase!</li>
                    </div>';    
    

    

        
     foreach ( $descriptions as $description ) {
        $variation =  wc_get_formatted_variation($description, true );
                    
            
            if (strpos($variation, 'Basic License') !== false && strpos($variation, 'mp3') !== false ) {
                    $descriptions['variation_description'] = $basicmp3des;
            }
            
     }
     
            return $descriptions;     
}   

Solution

  • Here are two approaches..... I'm not at home so can't test them right now. First, we can try updating what you have to suit the structure of the data array being passed. In both cases, I'm trying to check if the variation has the correct attribute assigned, but that's the part I can't fully test so the condition may not be cut/paste ready. If it doesn't work, you can take a look at var_dump( $attributes ); or if you have error logging enabled error_log( json_encode( $attributes ) ) to see what the attribute key/value pairs really are.

    add_filter( 'woocommerce_available_variation', 'change_variation_descriptions', 10, 3 );
    function change_variation_descriptions( $data, $product, $variation ) {
    
        // Returns array of attribute name value pairs. Keys are prefixed with attribute_, as stored.
        $attributes = $variation->get_attributes();
    
        if( isset( $attributes['pa_license-options'] && 'basic-license' == $attributes['pa_license-options'] && isset( $attributes['pa_delivery-format'] ) && 'mp3' == $attributes['pa_delivery-format'] ) ) {
            $data['variation_description'] = '<div class="licensedetails">
                        <li>-Delivered in mp3 format instantly after purchase</li>
                        <li>-Synchronization rights are granted</li>
                        <li>-One commercial use is permitted (ie: mixtape, album, etc)</li>
                        <li>-You may distribute up to 5000 profitable units</li>
                        <li>-Includes non-exclusive contract agreement (e-signed at checkout)</li>
                        <li>-Send me a quote to inquire about Exclusive License purchase!</li>
                        </div>';    
    
        }
    
        return $data;     
    }   
    

    And next, I think we can just filter the variation description all the time:

    add_filter( 'woocommerce_product_get_description', 'kia_filter_description', 10, 2 );
    function kia_filter_description( $desc, $product ) {
        if( $product->is_type( 'variation' ) ) {
            // Returns array of attribute name value pairs. Keys are prefixed with attribute_, as stored.
            $attributes = $product->get_attributes();
    
            if( isset( $attributes['pa_license-options'] && 'basic-license' == $attributes['pa_license-options'] && isset( $attributes['pa_delivery-format'] ) && 'mp3' == $attributes['pa_delivery-format'] ) ) {
                $desc .= '<div class="licensedetails">
                        <li>-Delivered in mp3 format instantly after purchase</li>
                        <li>-Synchronization rights are granted</li>
                        <li>-One commercial use is permitted (ie: mixtape, album, etc)</li>
                        <li>-You may distribute up to 5000 profitable units</li>
                        <li>-Includes non-exclusive contract agreement (e-signed at checkout)</li>
                        <li>-Send me a quote to inquire about Exclusive License purchase!</li>
                        </div>';    
            }
    
        }
        return $desc;
    }
    

    EDIT Code updated to use attribute and term slugs matching the OP's setup.