簡體   English   中英

WooCommerce,產品添加到購物車后顯示一些消息並更新標題中的購物車數量

[英]WooCommerce, show some message after product were added to cart and update quantity of cart in header

我已經嘗試過解決第二天的問題,但是我得到的只是這兩個問題之一-既沒有顯示“查看購物車”消息(我不知道在哪里/如何編輯),也只是在更新了購物車數量下一頁刷新,或者在將商品添加到購物車后刷新購物車數量,但不顯示任何消息。

為了在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>

我需要使兩個選項都起作用-在將項目添加到購物車后顯示一些成功消息,並更新購物車數量而不進行更新。

將產品添加到購物車后,請檢查以下代碼以獲取消息。

    /**
      * 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;
 }

希望這對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM