添加购物车指示器,需要检查是否为空

时间:2019-01-11 20:47:42

标签: php wordpress

我对PHP不太熟悉,但是我设法使购物车指示器在我的网站上正常工作,因此它可以实时更新购物车总数,而不需要刷新页面(是的!小胜利)。

我目前正在尝试对此进行扩展,但缺少答案。我要检查的是检查购物车总计指标是否为“ 0”,如果是,我只是想隐藏总计div,或者向其中添加一个类,以便将其隐藏在那里。

到目前为止,这就是我所拥有的,感谢您的帮助!

#Packages
  library(ineq)
  library(ggplot2)
  library(scales)
  library(grid)


  #DATA SETS
  set.seed(1)
  data1<-sample(1000)

  data2<-c(1000:2000) # I want to put this data set into second lorenz curve



  # compute lorenz curve
  lcolc <- Lc(data1)
  # bring lorenz curve in another format easily readable by ggplot2
  # namely reverse the L column so that lorenz curve is mirrored on diagonal
  # p stays p (the diagonal)
  # Uprob contains the indices of the L's, but we need percentiles
  lcdf <- data.frame(L = rev(1-lcolc$L), p = lcolc$p, Uprob = c(1:length(lcolc$L)/length(lcolc$L)))

  # basic plot with the diagonal line and the L line
  p <- ggplot(lcdf, aes(y = Uprob, x = L)) + geom_line(colour = hcl(h=15, l=65, c=100)) + geom_line(aes(y = p, x = p))
  # compute annotation lines at 50 percent L (uses a heuristic)
  index  <- which(lcdf$L >= 0.499 & lcdf$L <= 0.501)[1]

  ypos <- lcdf$L[index]
  yposs <- c(0,ypos)
  xpos <- index/length(lcdf$L)
  xposs <- c(0,xpos)
  ypositions <- data.frame(y = xposs, x = c(ypos,ypos))
  xpositions <- data.frame(y = c(xpos,xpos), x = yposs)
  # add annotation line
  p <- p + geom_line(data = ypositions, aes(x = x, y = y), 
                     linetype="dashed") + geom_line(data = xpositions, aes(x = x, y = y), 
                                                    linetype="dashed") 
  # set axes and labels (namely insert custom breaks in scales)
  p <- p + scale_x_continuous(breaks=c(0, xpos,0.25,0.5,0.75,1),
                              labels = percent_format()) + scale_y_continuous(
                                labels = percent_format())
  # add minimal theme
  p <- p + theme_minimal() + xlab("Percent of Population") + ylab("Percent of Income") 
  # customize theme
  p <- p + theme(plot.margin = unit(c(0.5,1,1,1), "cm"), 
                 axis.title.x = element_text(vjust=-1),
                 axis.title.y = element_text(angle=90, vjust=0),
                 panel.grid.minor = element_blank(),
                 plot.background = element_rect(fill = rgb(0.99,0.99,0.99), linetype=0)) 
  # print plot
  p

1 个答案:

答案 0 :(得分:1)

您需要移动@gridReady="onGridReady" ,因为该函数将永远不会越过该位置,并检查return $fragments;是否等于0(当前将其设置为不等于)。

$cart_count

本质上,这将设置add_filter( 'woocommerce_add_to_cart_fragments', 'iconic_cart_count_fragments', 10, 1 ); function iconic_cart_count_fragments( $fragments ) { $fragments['.fa-shopping-cart'] = '<i class="fa fa-shopping-cart" class="header-cart-count">' . "<span class='cart-indicator'>" . WC()->cart->get_cart_contents_count() . '</span>'. '</i>'; $cart_count = WC()->cart->get_cart_contents_count(); if ( $cart_count === 0 ) { // Here's where I want to Hide .cart-indicator $fragments['.fa-shopping-cart'] = WHATEVER YOU WANT TO OUTPUT HERE } return $fragments; } 变量,但是如果$fragments等于0,则$cart_count将相应地更新。