Automatic text in short description of WooCommerce products

后端 未结 2 744
不知归路
不知归路 2021-01-07 03:58

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 in

2条回答
  •  北海茫月
    2021-01-07 04:26

    So you can use it this way:

    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 = '

    ' . __( "article only available in the store.", "woocommerce" ) . '

    '; return $post_excerpt; }

    Normally this code will override existing short description text in single product pages, if this short description exist…


    (update) - Related to your comment

    If you want to display this without overriding the excerpt (short description), you can add it before this way:

    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 = '

    ' . __( "Article only available in the store.", "woocommerce" ) . '

    ' . $post_excerpt; return $post_excerpt; }

    So you will get your message before, and after (if short description exist) the short description…

    You can style it targeting in your active theme style.css file the class selector .product-message, for example this way:

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

    You will need to write your own style rules to get it as you want.

提交回复
热议问题