2016-06-29 78 views
2

我想創建的WooCommerce文章的描述的自動文本,並把「一文只在該店可用。」在WooCommerce產品的簡短描述文本自動

我想過把它的功能裏面是這樣的:

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

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

但這種替換已經被寫入了產品的簡短描述文字。如果我沒有放置文字,則不會顯示任何內容。

可以把代碼「的文章只在該店可用」的文本,而不該產品的簡短說明?

謝謝。

回答

2

所以,你可以使用這種方式:

add_filter('woocommerce_short_description', 'single_product_short_description', 10, 1); 
function single_product_short_description($post_excerpt){ 
    global $product; 

    if (is_single($product->id)) 
     $post_excerpt = '<p class="some-class">' . __("article only available in the store.", "woocommerce") . '</p>'; 

    return $post_excerpt; 
} 

通常這個代碼將覆蓋現有的簡短的說明文字中的單品頁,如果這個簡短的描述存在...


(更新) - 與您的評論

如果你想DISPLA Y本不重載摘錄(簡短描述),你可以用這種方式之前添加:

add_filter('woocommerce_short_description', 'single_product_short_description', 10, 1); 
function single_product_short_description($post_excerpt){ 
    global $product; 

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

你需要編寫自己的樣式規則得到它,只要你想。

+0

謝謝@LoicTheAztec,但我要求顯示,無需更換任何所著文字的方法。這可能嗎? –

+0

謝謝你的幫助非常@LoicTheAztec,我很感激。但我認爲我的英語不夠好......一位同事找到了解決這個問題的可能辦法。工作好我會讓你知道:) –

1

我更新說我發現了一個解決我的問題:

我在「產品」與名「的文章只在該店可用」和彈頭「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';} 
}?> 

然後我只有選擇產品的運輸進入方法。

就是這樣!

謝謝您的回答

相關問題