【问题标题】:How to remove continue shopping button from woo commerce cart?如何从 woocommerce 购物车中删除继续购物按钮?
【发布时间】:2020-11-01 19:58:01
【问题描述】:

如果购物车包含所有产品,我正在尝试从 woo 商务购物车中删除继续购物按钮。它没有删除按钮,所以只需要帮助微调我做错的事情。我以为我的代码可以工作,而我的 php 调试器由于某种奇怪的原因目前无法工作。

继续购物按钮的元素是:name="hpy_cs_continue"

代码如下:

add_action( 'woocommerce_after_cart', 'verify_all_products_in_cart' );
    
function verify_all_products_in_cart() {
    
    $all_products = false;
    $all_products_array  = wc_get_products( array( 'return' => 'ids', 'limit' => -1 ) );
    $products_in_cart_array = array();
    
  // Loop over $cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {    
    $products_in_cart_array &= in_array($cart_item['product_id']);
}
    if($all_products_array == $products_in_cart_array){
        $all_products = true;
    } else{
        $all_products = false;
    }
                                  
  if ($all_products = true){
      $shopping_cart_button = '<style>[name="hpy_cs_continue"]{ display:none;}</style>';
  } else{
      $shopping_cart_button = '<style>[name="hpy_cs_continue"]{ display:block;}</style>';
  }
   
    return $shopping_cart_button;
  
}

【问题讨论】:

标签: php wordpress woocommerce


【解决方案1】:

查看您的代码,您的 in_array() 需要 2 个参数;你的代码有一个。

如果您正在开发,我建议您将define('WP_DEBUG', true); 添加到您的 wp-config.php 文件中以捕获类似的内容。作为调试过程的一部分,如果您的 &lt;style&gt; 块始终呈现 display: block;,它应该表明您的 if 语句始终产生错误。

我还建议使用 if ( ! array_diff(...) ) 而不是循环遍历数组,或者 if must 循环遍历它们。实际上,我不确定比较像这样的 2 个数组会如何表现,但即使您只是更改了它,我也会推荐 if ( count(...) === count(...) )。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-12
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    相关资源
    最近更新 更多