Basically I am trying to create a condition where if a product is equal to a specific subcategory which is 2 other categories (specifically garden & roof) in this case.
I am trying to make it where if the product is not equal to the housing category it will print the cart addition which is a add to cart button. This does not work for some reason, and also I need help with a continuation statement (else, elseif) for if the product is actually the housing category it will just do nothing and not print that small form but actually let that sites page still function and load.
<div class="cart_addition_form">
<?php
if ($product->get_categories() != "housing") {
$action = 'woocommerce_cart_addition_form';
} ?>
</div>
I tried many types of if, else loops etc with different ways of using the loops but so many of the times it would just make my site crash and not load that specific page... I am expecting it to where if it detects housing as a category that the customer opens it will not allow them to add to cart, but if it detects another two categories which could be garden or roof, it will allow the customer to see the add to cart function and use it.
$terms = $product->get_categories(); ///< it gives an array of
// i check if "housing" is an element of the array containing only category names
if (!in_array("housing", $terms) {
$action = 'woocommerce_cart_addition_form';
}
in_array: is used to check if an element exists in an array.
check https://woocommerce.github.io/code-reference/classes/WC-Abstract-Legacy-Product.html#method_get_categories for get_categories
has_term function will be best to make check your need. However I don't understand the other part related to the action thing, but for the category exists/linked check you should usehas_term
. You need to pass the terms slugs array or single term and taxonomy name and product id.
So the code will look like this:
<div class="cart_addition_form">
<?php
if ( has_term( ['housing'], 'product_cat', $product->get_id() ) ) {
$action = 'woocommerce_cart_addition_form';
}
?>
</div>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.