【问题标题】:Woocommerce Shopping Cart IconWoocommerce 购物车图标
【发布时间】:2020-01-21 23:19:38
【问题描述】:

我的主导航中有一个 woocommerce 购物车图标。如果购物车中有物品,我希望该图标显示。如果不是,那将被删除。我已经尝试了下面的代码,但它并没有达到我的预期。想法?

if($items_txt = $count_value === 1 ){
$html .= '<span class="xoo-wsc-sc-count">1</span>';}
elseif($items_txt = $count_value === 0 ){
$html .= '';}

这里是完整的原始代码sn-p。

<?php

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
    die;
}

$sy_options = get_option('xoo-wsc-sy-options');//Style options
$options    = get_option('xoo-wsc-gl-options');
$count_type = isset( $options['bk-count-type']) ? $options['bk-count-type'] : 'qty_count'; //Count Type
$cart_items_total = wc_price(WC()->cart->subtotal);

$bk_cubi    = isset( $sy_options['bk-cubi']) ? $sy_options['bk-cubi'] : ''; // Custom basket icon
if( !$bk_cubi ){
    $bk_bit = isset( $sy_options['bk-bit']) ? $sy_options['bk-bit'] : 'xoo-wsc-icon-basket1'; // Basket Icon Type
}


$html  = '<a class="xoo-wsc-sc-cont">';

if( $bk_cubi ){
    $html .= '<img src="'.$bk_cubi.'" class="xoo-wsc-sc-icon">';
}
else{
    $html .= '<span class="xoo-wsc-sc-icon '.$bk_bit.'"></span>';
}

if($count_type == 'qty_count'){
    $count_value = WC()->cart->get_cart_contents_count();
}
elseif($count_type == 'item_count'){
    $count_value = count(WC()->cart->get_cart());
}

$items_txt = $count_value === 1 ? __('item','side-cart-woocommerce') : __('items','side-cart-woocommerce');
$html .= '<span class="xoo-wsc-sc-count">'.'</span>';
//$html .= '<span class="xoo-wsc-sc-count">'.$count_value.'</span>';
//$html .= '<span class="xoo-wsc-sc-total">'.$cart_items_total.'</span>';



$html .= '</a>';

echo $html;

【问题讨论】:

    标签: php woocommerce shopping-cart


    【解决方案1】:

    我想如果你改变这个,如果:

    if( $bk_cubi ){
        $html .= '<img src="'.$bk_cubi.'" class="xoo-wsc-sc-icon">';
    }
    else{
        $html .= '<span class="xoo-wsc-sc-icon '.$bk_bit.'"></span>';
    }
    

    收件人:

    if ($bk_cubi)
    {
        $html .= '<img src="'.$bk_cubi.'" class="xoo-wsc-sc-icon">';
    }
    else
    {
        if($count_value == 1 )
        {
            $html .= '<span class="xoo-wsc-sc-count">1</span>';
        }
    }
    

    你会得到你想要的。

    另外,单个= 是赋值运算符。 == 用于检查值是否相等。

    【讨论】:

      【解决方案2】:

      您可以使用 woocommerce_add_to_cart_fragments 挂钩来检查实时 woocommerce 购物车商品。

      add_filter( 'woocommerce_add_to_cart_fragments', 'woo_cart_icon_count' );
      function woo_cart_icon_count( $fragments ) {
      
         $cart_count = WC()->cart->cart_contents_count;
         if($cart_count > 0) {
           // your code here
         }
      
      }
      

      更多详情可以查看教程here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        相关资源
        最近更新 更多