Automatic text in short description of WooCommerce products

后端 未结 2 745
不知归路
不知归路 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 = '<p class="some-class">' . __( "article only available in the store.", "woocommerce" ) . '</p>';
    
        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 = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>' . $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.

    0 讨论(0)
  • 2021-01-07 04:32

    I update to say I found a solution to my problem:

    I created a "shipping class" in "products" with the name "article only available in the store" and the slug "productshop".

    Then in (mytheme)/woocommerce/single-product/meta.php I have included:

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

    Then I have only to select the shipping of product into the method.

    And that's it!

    Thanks for your answers

    0 讨论(0)
提交回复
热议问题