2

I am trying to create an automatic text in the description of the WooCommerce articles and put "article only available in the store."

I thought about putting it inside a function like this:

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

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

But this replace the text that is already written in the short description of the product. If I have not put text, nothing appears.

Is possible put the text of the code "article only available in the store" without a short description of the product?

Thank you.

4

2 回答 2

2

所以你可以这样使用它:

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

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


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

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

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

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

您可以在活动主题文件中为它设置样式,例如style.cssclass selector :.product-message

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

您需要编写自己的样式规则才能根据需要获得它。

于 2016-06-29T12:13:23.057 回答
1

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

我在“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';}
}?>

然后我只需要选择产品的运输方式。

就是这样!

感谢您的回答

于 2016-07-12T09:28:35.053 回答