【问题标题】:Custom cart notice based on user total purchased amount in Woocommerce基于用户在 Woocommerce 中购买的总金额的自定义购物车通知
【发布时间】:2019-04-06 07:15:14
【问题描述】:

我正在尝试根据以下答案代码在 Woocommerce 中根据用户总购买量显示自定义购物车通知:

Add a percentage discount based on customer total purchases sum in Woocommerce

它没有按我的意愿工作。

例如,如果客户下了 2 个订单:

  • 第一个订单是200
  • 二阶是122

所以总和是 200 + 122 = 322。但我总共得到 200。 我做错了什么?

这是我使用的代码:

 add_action( 'woocommerce_before_cart', 'vc' );

  function vc( ) {
// Only for logged in user
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_user_logged_in() )
    return;
$um = WC()->session->get( 'um' );
// If not get it and save it
if( empty($um) ){
    // ==> HERE goes the function to get customer's purchases total sum
    $um = get_customer_total_purchases_sum();
    // Save it in WC_Session
    WC()->session->set('um', $um);
}
 $vv=10000 - $um;
    if ( $um > 0 && $vv >0) {

    echo '<div class="woocommerce-message"><a href="' . get_permalink( 
       woocommerce_get_page_id( 'shop' ) ) . '" class="button wc-forward">Tiếp tục mua sắm</a>Bạn cần thêm ' . wc_price($vv) . ' để được.... </div>';
}
else { 
echo '......';
  }}

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce discount notice


    【解决方案1】:

    改用以下方法(我已经重新访问了一下get_customer_total_purchases_sum()函数)

    // Utililty function to get customer's total purchases sum
    function get_customer_total_purchases_sum() {
        $current_user_id = get_current_user_id(); // Current user ID
    
        if( $current_user_id == 0 ) return 0; // we return zero if customer is not logged in
    
        global $wpdb;
    
        // return the SQL query (paid orders sum)
        return $wpdb->get_var("SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}postmeta as pm
        INNER JOIN {$wpdb->prefix}postmeta as pm2 ON pm.post_id = pm2.post_id
        INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
        WHERE p.post_status LIKE 'wc-completed' AND p.post_type LIKE 'shop_order'
        AND pm.meta_key LIKE '_order_total' AND pm2.meta_key LIKE '_customer_user'
        AND pm2.meta_value LIKE '$current_user_id'");
    }
    
    // Display a custom notice
    add_action( 'template_redirect', 'total_purchase_custom_notification' );
    function total_purchase_custom_notification( ) {
    
        if ( is_wc_endpoint_url('order-received') && WC()->session->get( 'purchases_sum' ) ) {
            // We remove this session variable in thankyou page (if it still exist)
            WC()->session->__unset( 'purchases_sum' );
        }
        // On cart page we display a custom notice
        elseif( is_cart() ) {
            // Get customer's purchases total sum and set it in WC_Session
            if( ! WC()->session->get( 'purchases_sum' ) ){
                WC()->session->set('purchases_sum', get_customer_total_purchases_sum());
            }
    
            $total_purchases  = WC()->session->get( 'purchases_sum' );
    
            if ( $total_purchases == 0 ) return; // We exit (no purchases or non logged users)
    
            if ( ( 10000 - $total_purchases ) > 0 )
            {
                wc_add_notice( sprintf(
                    '<a class="button alt wc-forward" style="float:right" href="%s">%s</a> ' .
                    __( "You need an extra %s at all to get a...", "woocommerce" ),
                    get_permalink( wc_get_page_id( 'shop' ) ),
                    __( "Continue shopping", "woocommerce" ),
                    strip_tags( wc_price( 10000 - $total_purchases ) )
                ), 'notice');
            }
            else
            {
                wc_add_notice( __( "......", "woocommerce"), 'notice' );
            }
        }
    }
    

    此代码位于您的活动子主题(或主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 可将add_action( 'template_redirect', 替换为add_action( 'woocommerce_account_log-history_endpoint', 感谢任何帮助。
    • @LêNghĩa 我没听懂……这段代码用于购物车(甚至结帐),但没有使用我从未使用过的 'woocommerce_account_log-history_endpoint' 挂钩。
    • 我添加了我的自定义帐户菜单元素。
    • add_filter ( 'woocommerce_account_menu_items', 'xu', 40 ); function xu( $menu_links ){ $menu_links = array_slice( $menu_links, 0,3 , true ) + array( 'xu' =&gt; 'Xu của bạn' ) + array_slice( $menu_links, 3, NULL, true ); return $menu_links; } add_action( 'init', 'add_endpoint' ); function add_endpoint() { add_rewrite_endpoint( 'xu', EP_PAGES ); } add_action( 'woocommerce_account_xu_endpoint', 'xuxu' ); function xuxu() { //Custom cart notice based on user total purchased amount in Woocommerce }
    • @LêNghĩa 你最好问一个新问题,解释清楚详细的事情......评论区的代码不可读,更多没有解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2015-12-10
    • 2019-08-26
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多