6

您好我正在尝试让 woocommerce 中的顶级购物车自动更新数量和价格。

通过使用它作为模板,我已经让它在某种程度上起作用:

http://www.amberweinberg.com/developing-custom-woocommerce-themes/

问题是我需要它来使用 ajax 来改变 2 个元素而不仅仅是一个,

这是我在主题 fuctions.php 文件中使用的 html

                <div class="cartWrapper"> 
                <a href="#" title="Checkout">
                    <div id="cartsummary"><p>
                        <span class="cart-bubble cart-contents"><a class="cart-bubble cart-contents"><?php echo sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?></a>
                <?php if($woocommerce->cart->get_cart_url() != ''){ $cart=$woocommerce->cart->get_cart_url();}
                else {$cart = home_url().'/cart/';};
                ?></span> 
                    </div> 
                </a>
                <div id="carttotal">
                    <div id="cartprice">
                    <p>
                        <a class="cart-total"><?php echo $woocommerce->cart->get_cart_total() ?></a>
                    </p>
                    </div> 
                    <a class="button" href="#" title="Checkout" type="button">View Basket</a>
                </div>
            </div>

以及无需刷新即可自动更新购物车的代码:

    // Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');

function woocommerce_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>
    <a class="cart-bubble cart-contents"><?php echo sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?></a>
    <a class="cart-total"><?php echo $woocommerce->cart->get_cart_total() ?></a>
    <?php

    $fragments['a.cart-contents a.cart-total'] = ob_get_clean();

    return $fragments;

}

问题是,虽然这有效,但它会生成一长串购物车总数和购物车中的物品,我必须使用 css 样式 oveflow:hidden 在相关元素上隐藏这些列表。大概这是因为我错误地编码了ajax元素,有人能指出我正确的方向吗?

谢谢

4

1 回答 1

14

尝试这个:

函数.php

add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');

function woocommerce_header_add_to_cart_fragment( $fragments ) 
{
    global $woocommerce;
    ob_start(); ?>

    <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php echo $woocommerce->cart->get_cart_total(); ?></a>

    <?php
    $fragments['a.cart-contents'] = ob_get_clean();
    return $fragments;
}

购物车代码:

<div class="header_cart">
    <h5><a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php _e('Shopping Cart', 'home-shopper'); ?></a></h5>
    <div class="cart_contents">
        <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php echo $woocommerce->cart->get_cart_total(); ?></a>
    </div>
</div>
于 2013-05-18T14:18:03.347 回答