2

WooCommerce の記事の説明に自動テキストを作成して、「ストアでのみ入手可能な記事」を入れようとしています。

次のような関数の中に入れることを考えました:

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

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

ただし、これは、製品の簡単な説明に既に記載されているテキストを置き換えます。テキストを入れていない場合、何も表示されません。

商品の簡単な説明なしで「ストアでのみ入手可能な商品」というコードのテキストを入れることは可能ですか?

ありがとうございました。

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.cssファイルのクラス セレクターをターゲットにしてスタイルを設定できます。.product-message

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

必要に応じて独自のスタイル ルールを作成する必要があります。

于 2016-06-29T12:13:23.057 に答える