【问题标题】:Enable Shortcode usage in WooCommerce WC_Product get_description() method在 WooCommerce WC_Product get_description() 方法中启用简码使用
【发布时间】:2020-07-08 14:46:00
【问题描述】:

我正在使用来自 Add product description to cart items in Woocommerce 答案的第二个代码 sn-p 并将其添加到我的 function.php 文件中,以在 WooCommerce 购物车中显示产品描述。它很好用,但在我的产品描述中是来自这个插件https://wordpress.org/plugins/simple-divi-shortcode/ 的短代码,它没有正确显示在购物车中。改为显示短代码 [showmodule id="261"]。

【问题讨论】:

  • [showmodule id="261"] 总是在描述中的相同位置吗?比如开头还是结尾?
  • 是的,它总是在顶部的相同位置。 @LoicTheAztec 代码解决了这个问题。谢谢。
  • @StuartHingston 如果您希望我得到通知,最好在我的答案评论区发表评论。

标签: php wordpress woocommerce product cart


【解决方案1】:

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

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 文件中。经过测试并且可以工作。

【讨论】:

  • 谢谢。这很好用。有没有办法在购物车中隐藏产品标题?我不能用 CSS 来定位它,因为它没有类,所以描述也会受到影响。
  • 好的,是的,我做到了,但无法让它工作。我现在将发布一个新问题。谢谢。
  • @StuartHingston 抱歉,只需将$item_name .= '&lt;p class="item-description" 替换为$item_name = '&lt;p class="item-description"(删除= 之前的.),这样就不会显示产品名称。
猜你喜欢
  • 2019-03-25
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 2019-09-03
  • 1970-01-01
  • 2019-08-08
相关资源
最近更新 更多