Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Add to cart for a specific form › Reply To: Add to cart for a specific form
January 24, 2025 at 9:09 am
#52617
Keymaster
Hi,
Yes, of course!
All you have to do is modify the fist hook and function like so:
add_filter('asp_results', 'asp_add_to_cart_data', 10, 2);
function asp_add_to_cart_data($results, $search_id) {
// Apply add to cart only to search ID = 1
if ( $search_id != 1 ) {
return $results;
}
$product_add_to_cart_text = 'Add to cart';
$variation_add_to_cart_text = 'Choose variation'; // Leave it empty to not display at all
if (class_exists("WooCommerce")) {
$_pf = new WC_Product_Factory();
foreach ($results as &$r) {
if (
$r->content_type == "pagepost" &&
in_array($r->post_type, array("product", "product_variation"))
) {
$product = $_pf->get_product($r->id);
$is_variable = $product->is_type( 'variable' ) || $r->post_type == 'product_variation';
$link = !$is_variable ? get_permalink(wc_get_page_id('shop')) : $product->get_permalink();
$ajax = !$is_variable ? ' ajax_add_to_cart' : '';
$text = !$is_variable ? $product_add_to_cart_text : $variation_add_to_cart_text;
if ( empty($text) )
continue;
ob_start();
?>
<div class="woocommerce">
<a href="<?php echo $link; ?>"
data-quantity="1"
class="button product_type_simple add_to_cart_button<?php echo $ajax; ?>"
data-product_id="<?php echo $r->id; ?>" data-product_sku=""
rel="nofollow"><?php echo $text; ?></a>
</div>
<?php
$button = ob_get_clean();
$r->content .= $button;
}
}
}
return $results;
}
This will apply the add to cart to search form with ID=1 only. You can change that on line 5 🙂