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
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?
CodePudding user response:
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();
}
});
?>