• Resolved Joelle

    (@joelle)


    I’m adding a function to display a message after the product short description. But I only want it to show if the product has a dependency. I’m not sure what I need to add to my function to do that… if ________________ (do my function). Can you point me in the right direction?

    Though, looking at it now, I suppose I could just add your custom message field instead of my generic note below. Thoughts? ??

    (I do know that it appears along the top when you click “add to cart”, but we’d like it to do display under the short description all the time on products that require it, so the user knows beforehand.)

    Thank you!

    add_filter('woocommerce_short_description','sf_add_text_short_descr');
    function sf_add_text_short_descr($description){
    	if ( is_singular('product') ) {
    $text="<div class='dependency-box'>This item can only be added to your cart if you are purchasing or have already purchased a <a href='#'>Herd Share Membership</a>. Not sure? <a href='#'>Log in</a>!</div>";
    return $description.$text;
    }
    }
    • This topic was modified 4 years, 8 months ago by Joelle.
    • This topic was modified 4 years, 8 months ago by Joelle.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Joelle

    (@joelle)

    Hey there,

    I actually ended up going with this option, since there’s a bug in WC that seems to add the woocommerce_short_description after the variable products, too. Anyway, I did some digging in your plugin and came up with this, which seems to work.

    add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 2 );
    function custom_single_product_summary(){
        global $product;
    
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
        add_action( 'woocommerce_single_product_summary', 'custom_single_excerpt', 20 );
    }
    
    function custom_single_excerpt(){
        global $post, $product;
    
        $short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
    	$notice = $product->get_meta( '_dependency_notice', true );
    
        if ( ! $short_description )
            return;
    
    	if ($notice == TRUE) {
        // The custom text
        $custom_text = "<div class='dependency-box'>This item can only be added to your cart if you are purchasing or have already purchased a <a href='#'>Herd Share Membership</a>. Not sure? <a href='#'>Log in</a>!</div>";
    
        ?>
        <div class="woocommerce-product-details__short-description">
            <?php echo $short_description . $custom_text; // WPCS: XSS ok. ?>
        </div>
        <?php
    }
    }
    Thread Starter Joelle

    (@joelle)

    Actually, that was wrong. I was stabbing in the dark. While it DOES work, it also doesn’t show anything on products without a dependency, so I’m not sure what to do.

    • This reply was modified 4 years, 8 months ago by Joelle.
    • This reply was modified 4 years, 8 months ago by Joelle.
    Plugin Author Jason Kytros

    (@jasonkytros)

    Hey @joelle,

    Thanks for reaching out!

    You may use either the WC_Product_Dependencies::get_tied_product_ids( $product ) or the WC_Product_Dependencies::get_tied_category_ids( $product ) function to check if a specific product has any product/category dependencies. Both these functions should return an empty array if the product doesn’t have any dependencies.

    Let us know if this helps!

    Cheers,
    Jason Kytros
    Support Engineer | SomewhereWarm SMPC | https://somewherewarm.com/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditional’ is closed to new replies.