この例(Woocommerceの配送方法とカートの合計に基づく動的情報)から作業を試みましたが、動的メッセージが2回表示されます。どんな助けでも大歓迎です。
基本的に私がやろうとしているのは、ユーザーの配送が配達エリア外にある場合にメッセージを表示することです(WooCommerceの「他のゾーンでカバーされていない場所」配送ゾーンを使用)。メッセージは、ユーザーが定義された配送ゾーンにいない場合にのみ正しく表示されますが、2倍になります。1つはajaxリフレッシュ内にあるようで、もう1つはその外にあるようです。それがどのように、またはなぜそれをしているのか正確にはわかりません。
//add message to CHECKOUT if outside delivery area
add_action( 'woocommerce_review_order_after_shipping', 'outside_delivery_checkout', 20);
function outside_delivery_checkout() {
global $woocommerce;
if ( did_action( 'woocommerce_review_order_after_shipping' ) >= 2 ) {
return once;
}
$chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];
$cart_subtotal = WC()->cart->subtotal;
if ($cart_subtotal && $chosen_shipping_method === 'free_shipping') {
echo '<div class="outside-delivery checkout"><b>PLEASE NOTE:</b><br /> Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge. <strong>Please refresh the page after updating your shipping settings.</strong></div>';
} elseif ($cart_subtotal && $chosen_shipping_method != 'free_shipping') {
// do nothing
}
}
この問題へのリンクは次のとおりです。https://redstarrolloffqc.com/dumpster-sizes-rates/-製品を追加して、チェックアウトページに移動します。
ここでの主な問題は、<div>
htmlタグを閉じた直後のhtml内に、<table>
コンテンツを含むhtmlタグを挿入しようとしていることです</tr>
…
そのため、表示されたHtmlは、表示されなくても損益分岐点になります。
代わりに、次のhtml構造を使用する必要があります。
<tr><td colspan="2"><div>Your formatted text (message)</div></td></tr>
現在、did_action()
条件はコードに影響を与えglobal $woocommerce
ず、必須ではありません(そしてしばらくの間古くなっています)。
このようにして、コンテンツは必要に応じてテーブル行に表示されます。
これはあなたの問題を確実に解決します…
したがって、コードは次のようになります。
//add message to CHECKOUT if outside delivery area
add_action( 'woocommerce_review_order_after_shipping', 'outside_delivery_checkout', 20);
function outside_delivery_checkout() {
$chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];
$cart_subtotal = WC()->cart->subtotal;
if ( $cart_subtotal && $chosen_shipping_method === 'free_shipping' ) {
$html = '<tr class="delivery-message"><td colspan="2"><div class="outside-delivery checkout">
<strong>' . __("PLEASE NOTE:") . '</strong><br />';
$html .= __("Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge.");
$html .= ' <strong>' . __("Please refresh the page after updating your shipping settings.") . '</strong>';
echo $html . '</div></td></tr>';
}
}
コードは、アクティブな子テーマ(またはアクティブなテーマ)のfunctions.phpファイルに入ります。テストされ、動作します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加