我們在網站商店部分的產品頁面上添加了一個自定義購買按鈕,用于我通過 alpha 和 beta 函式的可選欄位定義為目錄的一系列產品。
現在,問題是這個按鈕顯示在所有產品中,即使是那些簡單定義的產品,實際上,該系列產品顯示了兩個購買按鈕,一個是添加到 WooCommerce 自己的購物車的按鈕,另一個是另一個是我們放置的購買按鈕。我們給予
注意這段代碼
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>
我們將購買按鈕的顯示放在最后,這就是出現此問題的原因
現在,當我們想在條件中顯示按鈕時,我們得到一個語法錯誤'<'
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();
}
?>
你認為問題出在哪里?
uj5u.com熱心網友回復:
這是更正后的代碼:
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();
}
});
?>
您還可以像這樣回顯按鈕:
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();
}
});
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/523052.html
