【问题标题】:Adding price in woocommerce total在 woocommerce 总计中添加价格
【发布时间】:2014-09-16 16:05:04
【问题描述】:

在我的标题中,我已经有一个选项可以显示我的购物车中的商品数量,每次我点击添加到购物车按钮时它都会改变。我也有总金额,但是当我将产品添加到购物车时,这不会加起来。在我的标题中是

<?php if( in_array('woocommerce/woocommerce.php', get_option('active_plugins')) ):?>
    <p id="shop_links" class="cart_right"><?php _e('Cart:','theme') ?>
        <a class="link_cart" href="<?php echo $woocommerce->cart->get_cart_url(); ?>">
            <span>
            <?php 
                echo '<span class="items_count">' . $woocommerce->cart->cart_contents_count . '</span> ' ._n('item', 'items',  $woocommerce->cart->cart_contents_count ,'theme') . ' ' . '&mdash; ' . $woocommerce->cart->get_cart_total() ;
            ?>
            </span>
            <i class="icon-shoppingbag"></i>
        </a>
    </p>
<?php endif; ?>

我的按钮有 .add_to_cart_ajax 类,适用于购物车中商品数量的代码是

$('.add_to_cart_ajax').click(function(e){
    e.preventDefault();
    var $this = $(this);
    var product_id = $this.data('product_id');
    var added_title = $this.data('added_title');
    var $shop_links = $('#shop_links');
    var $cart_no = $shop_links.find('.link_cart span .items_count');
    var $cart_amount = $shop_links.find('.link_cart span .amount');
    var currentPrice = $this.parents('li.product').find('.price ins .amount , .price > .amount').text();
    var data = {
        action: 'theme_add_to_cart_wishlist',
        product_id: product_id,
        add_to: 'cart'
    };
    $.post(ajaxurl, data, function(response) {
        $cart_no.text(parseInt($cart_no.text())+1);
        $cart_amount.text(parseFloat($cart_amount.replace(/[.,](?=.*[.,])/g, "").replace(",", ".").text())+currentPrice.replace(/[.,](?=.*[.,])/g, "").replace(",", "."));
        if(!$this.hasClass('in-cart')){
            $this.addClass('in-cart').tipsy("hide").attr("original-title",added_title).tipsy("show");
        }
    });
});

但是$cart_amount 部分没有做我想做的事情(在我点击各种产品时添加价格)。我用console.log 进行了测试,当我点击添加到购物车按钮时,我得到了正确的价格。那我错过了什么?

【问题讨论】:

    标签: php jquery wordpress woocommerce


    【解决方案1】:

    我找到了各种各样的解决方案

    <?php if ( $product->is_in_stock() ) : ?>
        <?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
        <form class="cart" method="post" enctype='multipart/form-data'>
            <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
            <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
            <button type="submit" class="single_add_to_cart_button"><i class="shoppingcart"></i></button>
            <?php 
            if(in_array('yith-woocommerce-wishlist/init.php', apply_filters('active_plugins', get_option('active_plugins'))) ){
                echo do_shortcode('[yith_wcwl_add_to_wishlist]');
            }
            ?>
            <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
        </form>
        <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
    <?php endif; ?>
    

    这似乎很有效,由于某种原因,我无法使用 ajax 更新价格。这样就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      相关资源
      最近更新 更多