首页 > 解决方案 > 解决了!将 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€&lt;/span> in DE";
} elseif ($total < "40") {
echo "Nur noch <span style='color:#ff4f33;'> $needed €&lt;/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€&lt;/span> in DE"; } elseif ($total < "40") { echo "Nur noch <span style='color:#ff4f33;'> $needed €&lt;/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€&lt;/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, ',', ' ')) . "€&lt;/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€&lt;/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, ',', ' ')) . "€&lt;/span> vom kostenlosen Versand entfernt (DE)</span>";
} else {
echo "<span class='cart-customlocation'>Glückwunsch! Der Versand erfolgt in DE kostenlos!</span>";
}
?>

标签: phpwordpressif-statementwoocommercewordpress-theming

解决方案


推荐阅读