Skip to content
Advertisement

How to hide a link using php and css

I am trying to apply some css in my php for a test to see how to hide a button in my php file. It’s not happening at the moment and I am not sure why it is not removing the button.

HTML:

<a href="https://test,com" class="button" name="hpy_cs_continue" value="Continue Shopping">Continue Shopping</a>

PHP

verify_all_products_in_cart() {
    
    $shopping_cart_button = "<style type='text/css'>
                                a[name='hpy_cs_continue']{ 
                                display:none !important;
                                }
                                </style>";
  
    return $shopping_cart_button;
  
}

UPDATE

The above code is a test code to try and fix the main code below:

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>a[name="hpy_cs_continue"]{ display:none !important;}</style>';
  } else{
      $shopping_cart_button = '<style>a[name="hpy_cs_continue"]{ display:block !important;}</style>';
  }
   
    return $shopping_cart_button;
  
}

Advertisement

Answer

In your verify_all_products_in_cart function, you have several isues:

in_array needs 2 parameters, you’re only sending one

$products_in_cart_array &= in_array($cart_item['product_id']);

this is doing an assignment = not a check == / ===

if ($all_products = true){  

$all_products is overused being set in 3 places

$shopping_cart_button is a poorly named and unessicary variable with a lot of duplicated code


You could re-write this function to something like the below which should work for your needs (assuming wc_get_products returns an array of ids, if not it may take a bit of modifying):

add_action( 'woocommerce_after_cart', 'verify_all_products_in_cart' );

function verify_all_products_in_cart() {
    $display = 'block';
    $all_products_array  = wc_get_products(['return' => 'ids', 'limit' => -1]); // Im assuming this returns an array of ids for all products..
    $products_in_cart_array = [];
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $products_in_cart_array[]=$cart_item['product_id'];
    }
    if(count($all_products_array) !== count($products_in_cart_array)){
        $display = 'none';
    }
    return "<style>a[name='.'hpy_cs_continue'']{ display:$display !important;}</style>";
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement