woocommerce_available_variation过滤器在Woocommerce 3更新后不再有效

时间:2017-05-15 17:37:40

标签: php wordpress woocommerce

我为woocommerce编写的一些自定义代码在更新到woocommerce 3之后无法正常工作。在这种特殊情况下,它是woocommerce_available_variation过滤器。我用它根据产品属性动态插入产品变体描述。

截至目前,我收到此错误消息:

捕获致命错误:无法将类WCOLOGct_Download的对象转换为第376行/home/silent48/public_html/wp/wp-content/plugins/woocommerce/includes/wc-product-functions.php中的字符串

我需要对以下代码做些什么才能使其符合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;     
}   

1 个答案:

答案 0 :(得分:1)

这是两种方法.....我不在家,所以现在无法测试它们。首先,我们可以尝试更新您所拥有的内容以适应传递的数据数组的结构。在这两种情况下,我都试图检查变体是否分配了正确的属性,但这是我无法完全测试的部分,因此条件可能无法剪切/粘贴就绪。如果它不起作用,您可以查看var_dump( $attributes );或者如果您启用了错误日志记录error_log( json_encode( $attributes ) ),以查看属性键/值对确实是什么。

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;     
}   

接下来,我认为我们可以随时过滤变体描述:

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;
}

编辑更新了代码,使用与OP设置相匹配的属性和术语slu。