Exclude "sold individually" products in WooCommerce
Exclude "sold individually" products in WooCommerce
I'm using this small php snippet on my functions.php in order to add a small text before my product quantity field on the WooCommerce single product page:
add_action( 'woocommerce_before_add_to_cart_quantity', 'wp_echo_qty_front_add_cart' );
function wp_echo_qty_front_add_cart() {
echo '<div class="qty">Número de asistentes: </div>';
}
Works as a charm, but I would like to exclude from this all the products that are using the WooCommerce native "Sold individually" option.
Is there anyway to do exclude them just by adding something to functions.php?
You can use WC_Product::get_sold_individually(), which indicates whether a product is sold individually.
So you get:
function action_woocommerce_before_add_to_cart_quantity() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// NOT sold individually.
if ( ! $product->get_sold_individually() ) {
echo '<div class="qty">Número de asistentes: </div>';
}
}
}
add_action( 'woocommerce_before_add_to_cart_quantity', 'action_woocommerce_before_add_to_cart_quantity' );
January 7, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
January 7, 2024