【问题标题】:WooCommerce, show some message after product were added to cart and update quantity of cart in headerWooCommerce,将产品添加到购物车后显示一些消息并在标题中更新购物车数量
【发布时间】:2017-12-05 11:42:29
【问题描述】:

我已经在第二天尝试解决这个问题,但我得到的只是这两个之一 - 它既没有显示“查看购物车”消息(我不知道在哪里/如何编辑),但只有更新下一页刷新购物车数量,或者添加商品后刷新购物车数量,但不显示任何消息。

为了在content-product.php 中显示没有购物车数量更新的消息,我使用了这段代码:

<div class="prod-item-add-to-cart">
    <?php

        echo apply_filters( 'woocommerce_loop_add_to_cart_link',
        sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
                esc_url( $product->add_to_cart_url() ),
                esc_attr( isset( $quantity ) ? $quantity : 1 ),
                esc_attr( $product->get_id() ),
                esc_attr( $product->get_sku() ),
                esc_attr( isset( $class ) ? $class : 'button' ),
                esc_html( $product->add_to_cart_text() )
            ),
        $product );

        ?>
    </div>

在标题中我有这个:

<?php echo sprintf ( WC()->cart->get_cart_contents_count() ); ?> <?php _e('pcs.', 'eg'); ?> / ~ <?php echo WC()->cart->get_cart_total(); ?>

要更新标题中的数量,但在这种情况下添加的购物车消息不显示,我在content-product.php 中使用了此代码:

<div class="prod-item-add-to-cart">
    <?php do_action('woocommerce_after_shop_loop_item'); ?>
</div>

我需要使这两个选项都起作用 - 在将商品添加到购物车后显示一些成功消息,并在不更新的情况下更新购物车数量。

【问题讨论】:

    标签: php ajax wordpress woocommerce


    【解决方案1】:

    将产品添加到购物车后,请查看以下代码以获取消息。

        /**
          * Custom Add To Cart Messages
          * Add this to your theme functions.php file
        **/
       add_filter( 'woocommerce_add_to_cart_message', 
              'custom_add_to_cart_message' );
        function custom_add_to_cart_message() {
    global $woocommerce;
        // Output success messages
        if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
            $return_to  = get_permalink(woocommerce_get_page_id('shop'));
            $message    = sprintf('<a href="%s" class="button">%s</a> %s', 
            $return_to, __('Continue Shopping &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
         else :
            $message    = sprintf('<a href="%s" class="button">%s</a> %s', 
              get_permalink(woocommerce_get_page_id('cart')), __('View 
              Cart &rarr;', 'woocommerce'), __('Product successfully added 
              to your cart.', 'woocommerce') );
         endif;
    
        return $message;
     }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2012-12-05
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 2014-02-06
      • 2013-05-02
      • 2021-08-22
      • 2011-12-19
      相关资源
      最近更新 更多