php - 解决了!将 Ajax 添加到顶部横幅
问题描述
下面的解决方案
我目前正在开发一个 WooCommerce 网站。我想添加一个顶部横幅,它显示与其购物车总数相关的不同文本。我创建的 PHP 代码效果很好,但只有在刷新后才会调整。
<?php
$total = WC()->cart->cart_contents_total;
$free = '40';
$needed = $free - $total;
if ($total == "0") {
echo "Kostenloser Versand ab <span style='color:#ff4f33;'>40€</span> in DE";
} elseif ($total < "40") {
echo "Nur noch <span style='color:#ff4f33;'> $needed €</span> vom kostenlosen Versand entfernt (DE)";
} else {
echo "Glückwunsch! Der Versand erfolgt in DE kostenlos!";
}
?>
Woo 官方文档提供了创建 ajax 购物车的代码
https://gist.github.com/woogists/c0a86397015b88f4ca722782a724ff6c
https://gist.github.com/woogists/9a16fd2d0c982e780a5de89c30cbbd25
我的想法是,可能是在上面的链接中插入我的代码来代替给定的代码,顶部横幅的内容会在不刷新网站的情况下进行调整。现在它有时在某些配置中有点工作,但只有一些文本显示两次的奇怪故障。
/**
* Show cart contents / total Ajax
*/
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<span class="cart-customlocation" <?php $total = WC()->cart->cart_contents_total; $free = '40'; $needed = $free - $total; if ($total == "0") { echo "Kostenloser Versand ab <span style='color:#ff4f33;'>40€</span> in DE"; } elseif ($total < "40") { echo "Nur noch <span style='color:#ff4f33;'> $needed €</span> vom kostenlosen Versand entfernt (DE)"; } else { echo "Glückwunsch! Der Versand erfolgt in DE kostenlos!"; } ?></span>
<?php
$fragments['span.cart-customlocation'] = ob_get_clean();
return $fragments;
}
有人能告诉我这个想法是否有效,我必须找到一个新的解决方案/完全重写代码吗?
找到了解决方案!
这通过代码片段:
/**
* Show cart contents / total Ajax
*/
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<span class="cart-customlocation" <?php if (WC()->cart->total == "0") { echo "<span class='cart-customlocation'>Kostenloser Versand ab <span style='color:#ff4f33;'>40€</span> in DE</span>"; } elseif (WC()->cart->cart_contents_total < "40") { echo "<span class='cart-customlocation'>Nur noch <span style='color:#ff4f33;'>" . (number_format(40 - WC()->cart->get_total( 'raw' ), 2, ',', ' ')) . "€</span> vom kostenlosen Versand entfernt (DE)</span>"; } else { echo "<span class='cart-customlocation'>Glückwunsch! Der Versand erfolgt in DE kostenlos!</span>"; } ?></span>
<?php
$fragments['span.cart-customlocation'] = ob_get_clean();
return $fragments;
}
这在网站本身
<?php
if (WC()->cart->total == "0") {
echo "<span class='cart-customlocation'>Kostenloser Versand ab <span style='color:#ff4f33;'>40€</span> in DE</span>";
} elseif (WC()->cart->cart_contents_total < "40") {
echo "<span class='cart-customlocation'>Nur noch <span style='color:#ff4f33;'>" . (number_format(40 - WC()->cart->get_total( 'raw' ), 2, ',', ' ')) . "€</span> vom kostenlosen Versand entfernt (DE)</span>";
} else {
echo "<span class='cart-customlocation'>Glückwunsch! Der Versand erfolgt in DE kostenlos!</span>";
}
?>
解决方案
推荐阅读
- spring-boot - 目标不可达,标识符 [clientsView] 解析为 null
- android - 如何从 android 小部件使 onclick 到另一个活动
- prometheus - 计数器的普罗米修斯最大限制
- oracle - 在 Oracle 中将一个复合主键更新为空
- javascript - 检测移动用户和桌面是否相同
- android - NestedScrollView 在滚动时不听
- css - 如何在 React js 中使用引导程序使 Grid 响应?
- javascript - 使用R从js源或HTML标签中提取值?
- r - 计算测试集的 AUC(R 中的 keras 模型)
- oracle - 我可以在 PL/SQL 中返回一个函数吗?