【问题标题】:Automatic text in short description of WooCommerce productsWooCommerce 产品的简短描述中的自动文本
【发布时间】:2018-11-30 22:32:44
【问题描述】:

我正在尝试在 WooCommerce 文章的描述中创建一个自动文本,并将“文章仅在商店中可用”。

我想过把它放在这样的函数中:

add_filter ('woocommerce_short_description', 'in_single_product', 10, 2);

function in_single_product () {
    echo '<p> article only available in the store. </ p>';
}

但这会替换已经写在产品简短描述中的文本。如果我没有输入文本,则不会出现任何内容。

是否可以将代码文本“仅在商店中可用”而不是产品的简短描述?

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:

    所以你可以这样使用它:

    add_filter( 'woocommerce_short_description', 'single_product_short_description', 10, 1 );
    function single_product_short_description( $post_excerpt ){
        global $product;
    
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
        if ( is_single( $product_id ) )
            $post_excerpt = '<p class="some-class">' . __( "article only available in the store.", "woocommerce" ) . '</p>';
    
        return $post_excerpt;
    }
    

    通常,如果此简短描述存在,此代码将覆盖单个产品页面中现有的简短描述文本……


    (更新)- 与您的评论相关

    如果您想在不覆盖摘录(简短描述)的情况下显示它,您可以在此之前添加它:

    add_filter( 'woocommerce_short_description', 'single_product_short_description', 10, 1 );
    function single_product_short_description( $post_excerpt ){
        global $product;
    
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
        if ( is_single( $product_id ) )
            $post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;
    
        return $post_excerpt;
    }
    

    因此,您将在简短描述之前和之后(如果存在简短描述)收到您的消息……

    您可以在活动主题style.css 文件中设置它的样式,并将其设置为类选择器.product-message,例如这样:

    .product-message {
        background-color:#eee;
        border: solid 1px #666;
        padding: 10px;
    }
    

    您需要编写自己的样式规则来获得所需的样式。

    【讨论】:

    • 谢谢@LoicTheAztec,但我正在寻找无需替换任何书面文字即可显示的方法。这可能吗?
    • 非常感谢@LoicTheAztec 的帮助,非常感谢。但我认为我的英语不够好......一位同事找到了解决问题的可能方法。做得好我会告诉你的:)
    【解决方案2】:

    我更新说我找到了解决问题的方法:

    我在“products”中创建了一个“shipping class”,名称为“article only available in the store”,名称为“productshop”。

    然后在 (mytheme)/woocommerce/single-product/meta.php 我已经包括:

    <?php
    $clase=$product->get_shipping_class();
    if ($clase=="productshop") {
    if (get_locale()=='en_US') {echo 'Product only available in store';}
    else {echo 'Producte només disponible a la botiga';}
    }?>
    

    然后我只需在方法中选择产品的运输方式。

    就是这样!

    感谢您的回答

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 2022-09-23
      相关资源
      最近更新 更多