繁体   English   中英

在 WooCommerce WC_Product get_description() 方法中启用简码使用

[英]Enable Shortcode usage in WooCommerce WC_Product get_description() method

I am using the 2nd code snippet from Add product description to cart items in Woocommerce answer and added it to my function.php file to display the product description in the WooCommerce cart. 它工作得很好,但在我的产品描述中是这个插件https://wordpress.org/plugins/simple-divi-shortcode/的简码,它没有正确显示在购物车中。 改为显示短代码 [showmodule id="261"]。

您只需将产品描述嵌入 WordPress do_shortcode() function 中,例如:

add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_data', 10, 3);
function customizing_cart_item_data( $item_name, $cart_item, $cart_item_key ) {
    // The label
    $label = __( 'Description', 'woocommerce' );

    // Get the product description
    $description = $cart_item['data']->get_description();

    // For product variations when description is empty
    if( $cart_item['data']->is_type('variation') && empty( $description ) ){
        // Get the parent variable product object
        $product = wc_get_product( $cart_item['data']->get_parent_id() );
        // Get the variable product description
        $description = $product->get_description();
    }

    if( ! empty( $description ) ){
        $item_name .= '<p class="item-description" style="margin:12px 0 0;">
            <strong>'.$label.'</strong>: <br>' . do_shortcode( $description ) . '
        </p>';
    }
    return $item_name;
}

代码位于活动子主题(或活动主题)的 function.php 文件中。 测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM