Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

syntax error '<' on wordpress custom field

We have added a custom purchase button on the product page for the store section of the site for a series of products that I have defined as a catalog through the optional field of alpha and beta functions.

Now, the problem is that this button is displayed in all products, even those that are simply defined, and in practice, two purchase buttons are displayed for that series of products, one is the button to add to WooCommerce’s own shopping cart, and the other is the purchase button that we have placed. we gave

Pay attention to this code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

add_action( 'woocommerce_after_single_product_summary', function()
{
    global $post;
    if( get_post_meta($post->ID, 'alpha', true) != '' ) {
        $url = returnalphaUrl($post->ID);
    }
    elseif ( get_post_meta($post->ID, 'beta', true) != '' ) {
        $url = returnbetaUrl($post->ID);
    }
    else {
        $url = returnUrl();
    }
    
?>
    <button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">
     Buy Now
    </button>

We put the display of the buy button last, that’s why this problem occurs

Now, when we want to display the button in the condition, we get a syntax error ‘<‘

add_action( 'woocommerce_after_single_product_summary', function()
{
    global $post;
    if( get_post_meta($post->ID, 'alpha', true) != '' ) {
        $url = returnalphaUrl($post->ID);
        <button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">
        Buy Now
        </button>
    }
    elseif ( get_post_meta($post->ID, 'beta', true) != '' ) {
        $url = returnbetaUrl($post->ID);
        <button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">
        Buy Now
        </button>
    }
    else {
        $url = returnUrl();
    }
    
?>

Where do you think the problem is?

>Solution :

Here’s the corrected code:

add_action( 'woocommerce_after_single_product_summary', function(){
    global $post;
    if( get_post_meta($post->ID, 'alpha', true) != '' ) {
        $url = returnalphaUrl($post->ID); ?>
        <button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">Buy Now</button>
    <?php } else if( get_post_meta($post->ID, 'beta', true) != '' ) {
        $url = returnbetaUrl($post->ID); ?>
        <button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">Buy Now</button>
    <?php } else {
        $url = returnUrl();
    }
});
?>

You can also Echo the buttons like this:

add_action( 'woocommerce_after_single_product_summary', function(){
    global $post;
    if( get_post_meta($post->ID, 'alpha', true) != '' ) {
        $url = returnalphaUrl($post->ID); 
        echo '<button id="Btn" onclick="window.location.href="'.$url.'">Buy Now</button>';
    } else if( get_post_meta($post->ID, 'beta', true) != '' ) {
        $url = returnbetaUrl($post->ID);
        echo '<button id="Btn" onclick="window.location.href="'.$url.'">Buy Now</button>';
    } else {
        $url = returnUrl();
    }
});
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading