Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #790055

    allcal
    Participant

    I would like to be able to place a shortcode/text on product pages below the product description/info but before the “Related Products”. But the shortcode/text would be unique for each product so I can’t just inject it on the template file. Is there a way to accomplish this?

    #790406

    Nico
    Moderator

    Hi There,

    Thanks for writing in! Regretfully, at this time I am not entirely certain what it is you would like to accomplish based on the information given in your post. If you wouldn’t mind providing us with a little more clarification on what it is you’re wanting to do (perhaps some screenshots), we’ll be happy to provide you with a response once we have a better understanding of the situation.

    #791442

    allcal
    Participant

    I attached a screen shot of http://printmorespendless.com/allcal/product/pt9600-3/

    I put a red box and arrow where I would like to add Woocommerce shortcodes specific to the product.

    #792301

    Rad
    Moderator

    Hi there,

    Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.

    Add this code to your child theme’s functions.php

    add_action( 'woocommerce_after_single_product_summary', 'product_special_shortcode', 20 );
    
    function product_special_shortcode () {
    
    $shortcode = get_post_meta ( get_the_ID(), 'shortcode_content', true );
    
    echo empty( $shortcode ) ? '' : do_shortcode( $shortcode );
    
    }

    Then edit your product and add a custom field named shortcode_content and its value should be your shortcode specific to that product.

    You should able to enable custom fields by going to Screen Options on the very top of your product in admin, then toggle on Custom fields. Then scroll down and find the custom field section.

    Thanks!

    #792303

    Lely
    Moderator

    Hi There,

    Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.

    Then add the following on your child theme’s functions.php file.

    function custom_product_shortcode() {
    
    global $product;
    $id = $product->id;
    if($id==2422){
      echo "<h1>Custom content</h1>".do_shortcode('[SHORTCODE-HERE]'); 
    }
    
    }
    add_action( 'woocommerce_after_single_product_summary', 'custom_product_shortcode', 18);
    
    

    Further customizations from here would be getting into custom development, which is outside the scope of support we can offer. If you need more in depth changes, you may wish to consult with a developer. X is quite extensible with child themes, so there are plenty of possibilities. Thanks for understanding.

    #800343

    allcal
    Participant

    Both the responses worked in getting the content to show in the correct area. However, the shortcode doesn’t show the right information. For example, the shortcode [products category=”tze-tapes” per_page=”4″ columns=”2″] on http://printmorespendless.com/allcal/product/tze-b51-2/ doesn’t show just the product category tze-tapes and it is in one column not two. Can this be fixed?

    #801048

    Rad
    Moderator

    Hi there,

    That shortcode is built-in from woocommerce plugin and there is no way for us to customize it. You may contact woocommerce community or author.

    But based from here https://docs.woothemes.com/document/woocommerce-shortcodes/, there is no category filter for [products] shortcode.

    Though, the columns should work. Would you mind providing your site’s URL and login credential in private reply? I like to check it why the columns behave differently.

    Thanks!

    #855506

    allcal
    Participant

    Is there a way to apply a css class to the shortcode added in the custom field named “shortcode_content”?

    #856030

    Lely
    Moderator

    Hi There,

    Unfortunately, class property is not available by default on Woocommerce shortcode. We can add it by customizing the woocommerce template for shortcode but that would outside the scope of our support. You may wish to consult a developer to achieve this or you may give us more clarification on why you want to add class by describing what you want to achieve so we can see if there’s a workaround.

    Always,
    X

    #994574

    allcal
    Participant

    Your php code works great. Example product: http://printmorespendless.com/allcal/product/tze-111

    Currently I have text “Related Products” in the custom fields value above the woocommerce shortcode. However is there a way to add to your php to have text that says “Related Product For (product title):” with an php call of the title of the product depending on which product page you are on?

    #994989

    Lely
    Moderator

    Hi There,

    We can do with this line of code:

    echo "<h1>Related Product For ".$product->post->post_title."</h1>";
    Depending where you want it to display, we can add it like this:

    
    function custom_product_shortcode() {
    
    global $product;
    $id = $product->id;
    if($id==2422){
      echo "<h1>Custom content</h1>".do_shortcode('[SHORTCODE-HERE]'); 
    echo "<h1>Related Product For ".$product->post->post_title."</h1>"; 
    }
    
    }
    add_action( 'woocommerce_after_single_product_summary', 'custom_product_shortcode', 18);

    Hope this helps.

    #995734

    allcal
    Participant

    Sorry for the confusion, I was referring to Rad’s code:

    add_action( ‘woocommerce_after_single_product_summary’, ‘product_special_shortcode’, 20 );

    function product_special_shortcode () {

    $shortcode = get_post_meta ( get_the_ID(), ‘shortcode_content’, true );

    echo empty( $shortcode ) ? ” : do_shortcode( $shortcode );

    }

    I tried adding the

    echo “<h1>Related Product For “.$product->post->post_title.”</h1>”;

    to that code and didn’t seems to output the title correctly?

    #995881

    Thai
    Moderator

    Hi There,

    Please try with this code:

    add_action( 'woocommerce_after_single_product_summary', 'product_special_shortcode', 20 );
    
    function product_special_shortcode () {
    
      global $product;
      $shortcode = get_post_meta ( get_the_ID(), 'shortcode_content', true );
    
      if( !empty( $shortcode ) ){
        echo "<h1>Related Product For ".$product->post_title."</h1>";
        echo do_shortcode( $shortcode );
      }
    
    }

    Hope it helps 🙂