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;
}
2
Answers
Simply apply the CSS directly to the element like that,
In your
verify_all_products_in_cart
function, you have several isues:in_array needs 2 parameters, you’re only sending one
this is doing an assignment
=
not a check==
/===
$all_products
is overused being set in 3 places$shopping_cart_button
is a poorly named and unessicary variable with a lot of duplicated codeYou 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):