每次 WooCommerce 购物车为空时,如何让脚本运行?

时间:2021-03-12 02:34:59

标签: javascript php wordpress woocommerce

在 WooCommerce 中,在空购物车挂钩上运行以下函数。因此,从技术上讲,每次 woocommerce 购物车为空时,它都会运行。如果我在购物车为空时转到 directlinkdotcom/cart/,则该脚本运行良好。但是,如果我从购物车中删除项目以使其在有东西后清空,则脚本不会按预期执行。我做错了什么?

function mdoulos_show_custom_empty_cart() {
?>
      <script type="text/javascript" defer>
          let cartTitle = document.querySelector('.entry-title');
          let progressBar = document.querySelector('.pbar-cart');
          let customHeading = document.querySelector('.empty-heading');
          let customDescription = document.querySelector('.empty-description');
          let customTruck = document.querySelector('.empty-truck');
          let cartCategories = customTruck.nextElementSibling;
          let standardNotice = customTruck.nextElementSibling.nextElementSibling;

          cartTitle.style.display = "none";
          progressBar.style.display = "none";
          standardNotice.style.display = "none";
          customHeading.style.display = "block";
          customDescription.style.display = "block";
          customTruck.style.display = "block";
          cartCategories.style.display = "block";
          
      </script>


 <?php
    
}

remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
add_action( 'woocommerce_cart_is_empty', 'mdoulos_show_custom_empty_cart', 10 );

2 个答案:

答案 0 :(得分:1)

我认为您可能会错过删除项目时在幕后发生的 AJAX 功能。在页面加载时运行代码与在删除某些产品并最终以空购物车结束后运行的代码之间存在差异。这解释了您在页面刷新时注意到的内容。

除了您已经在使用的代码之外,您可能还需要这样的东西:

function my_cart_updated( $item_id ) {

    // check if cart contents are empty
    if ( WC()->cart->get_cart_contents_count() == 0 ) {
        
        // do whatever you want here
        // ...
    }
}

// add the action
add_action( 'woocommerce_cart_item_removed', 'my_cart_updated' );

我认为这样的事情可能会奏效:

function my_cart_updated( $item_id ) {

    // check if cart contents are empty
    if ( WC()->cart->get_cart_contents_count() == 0 ) {
    ?>

      <script type="text/javascript" defer>
          let cartTitle = document.querySelector('.entry-title');
          let progressBar = document.querySelector('.pbar-cart');
          let customHeading = document.querySelector('.empty-heading');
          let customDescription = document.querySelector('.empty-description');
          let customTruck = document.querySelector('.empty-truck');
          let cartCategories = customTruck.nextElementSibling;
          let standardNotice = customTruck.nextElementSibling.nextElementSibling;

          cartTitle.style.display = "none";
          progressBar.style.display = "none";
          standardNotice.style.display = "none";
          customHeading.style.display = "block";
          customDescription.style.display = "block";
          customTruck.style.display = "block";
          cartCategories.style.display = "block";
      </script>

    <?php
    }
}

// add the action
add_action( 'woocommerce_cart_item_removed', 'my_cart_updated' );

答案 1 :(得分:0)

您是否正在尝试删除购物车的功能并让您的商店将“添加到购物车”按钮重定向到结帐?