{"id":15168222,"date":"2021-12-16T02:02:06","date_gmt":"2021-12-16T02:02:06","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/add-to-wishlist-button-3\/"},"modified":"2021-12-16T02:02:06","modified_gmt":"2021-12-16T02:02:06","slug":"add-to-wishlist-button-3","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/add-to-wishlist-button-3\/","title":{"rendered":"Add to wishlist button"},"content":{"rendered":"<p>I used the following code for showig the add to cart button on results in a search. I would like to show the wishlist button aside the add to cart button. How can i do it? <\/p>\n<p>add_filter(&#8216;asp_results&#8217;, &#8216;asp_add_to_cart_data&#8217;, 1, 1);<br \/>\nfunction asp_add_to_cart_data($results) {<br \/>\n    $product_add_to_cart_text   = &#8216;Add to cart&#8217;;<br \/>\n    $variation_add_to_cart_text = &#8216;Choose variation&#8217;;   \/\/ Leave it empty to not display at all<\/p>\n<p>    if (class_exists(&#8220;WooCommerce&#8221;)) {<br \/>\n        $_pf = new WC_Product_Factory();<br \/>\n        foreach ($results as &amp;$r) {<br \/>\n            if (<br \/>\n            $r-&gt;content_type = &#8220;cpt&#8221; &amp;&amp;<br \/>\n                in_array($r-&gt;post_type, array(&#8220;product&#8221;, &#8220;product_variation&#8221;))<br \/>\n            ) {<br \/>\n                $product = $_pf-&gt;get_product($r-&gt;id);<br \/>\n                $is_variable = $product-&gt;is_type( &#8216;variable&#8217; ) || $r-&gt;post_type == &#8216;product_variation&#8217;;<br \/>\n                $link = !$is_variable ? get_permalink(wc_get_page_id(&#8216;shop&#8217;)) : $product-&gt;get_permalink();<br \/>\n                $ajax = !$is_variable ? &#8216; ajax_add_to_cart&#8217; : &#8221;;<br \/>\n                $text = !$is_variable ? $product_add_to_cart_text : $variation_add_to_cart_text;<br \/>\n                if ( empty($text) )<br \/>\n                    continue;<br \/>\n                ob_start();<br \/>\n                ?&gt;<br \/>\n                &lt;div class=&#8221;woocommerce&#8221;&gt;<br \/>\n                    <a>&#8221;<br \/>\n                       data-quantity=&#8221;1&#8243;<br \/>\n                       class=&#8221;button product_type_simple add_to_cart_button&lt;?php echo $ajax; ?&gt;&#8221;<br \/>\n                       data-product_id=&#8221;&lt;?php echo $r-&gt;id; ?&gt;&#8221; data-product_sku=&#8221;&#8221;<br \/>\n                       rel=&#8221;nofollow&#8221;&gt;&lt;?php echo $text; ?&gt;<\/a><br \/>\n                &lt;\/div&gt;<br \/>\n                &lt;?php<br \/>\n                $button = ob_get_clean();<br \/>\n                $r-&gt;content .= $button;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    return $results;<br \/>\n}<br \/>\nadd_action(&#8216;wp_footer&#8217;, &#8216;asp_add_to_cart_handler&#8217;);<br \/>\nfunction asp_add_to_cart_handler() {<br \/>\n    ?&gt;<br \/>\n    &lt;script&gt;<br \/>\n    jQuery(function(t){if(&#8220;undefined&#8221;==typeof wc_add_to_cart_params)return!1;var a=function(){t(&#8220;.asp_r&#8221;).on(&#8220;click&#8221;,&#8221;.add_to_cart_button&#8221;,this.onAddToCart).on(&#8220;click&#8221;,&#8221;.remove_from_cart_button&#8221;,this.onRemoveFromCart).on(&#8220;added_to_cart&#8221;,this.updateButton).on(&#8220;added_to_cart&#8221;,this.updateCartPage).on(&#8220;added_to_cart removed_from_cart&#8221;,this.updateFragments)};a.prototype.onAddToCart=function(a){var o=t(this);if(o.is(&#8220;.ajax_add_to_cart&#8221;)){if(!o.attr(&#8220;data-product_id&#8221;))return!0;a.preventDefault(),o.removeClass(&#8220;added&#8221;),o.addClass(&#8220;loading&#8221;);var r={};t.each(o.data(),function(t,a){r[t]=a}),t(document.body).trigger(&#8220;adding_to_cart&#8221;,[o,r]),t.post(wc_add_to_cart_params.wc_ajax_url.toString().replace(&#8220;%%endpoint%%&#8221;,&#8221;add_to_cart&#8221;),r,function(a){a&amp;&amp;(a.error&amp;&amp;a.product_url?window.location=a.product_url:&#8221;yes&#8221;!==wc_add_to_cart_params.cart_redirect_after_add?t(document.body).trigger(&#8220;added_to_cart&#8221;,[a.fragments,a.cart_hash,o]):window.location=wc_add_to_cart_params.cart_url)})}},a.prototype.onRemoveFromCart=function(a){var o=t(this),r=o.closest(&#8220;.woocommerce-mini-cart-item&#8221;);a.preventDefault(),r.block({message:null,overlayCSS:{opacity:.6}}),t.post(wc_add_to_cart_params.wc_ajax_url.toString().replace(&#8220;%%endpoint%%&#8221;,&#8221;remove_from_cart&#8221;),{cart_item_key:o.data(&#8220;cart_item_key&#8221;)},function(a){a&amp;&amp;a.fragments?t(document.body).trigger(&#8220;removed_from_cart&#8221;,[a.fragments,a.cart_hash,o]):window.location=o.attr(&#8220;href&#8221;)}).fail(function(){window.location=o.attr(&#8220;href&#8221;)})},a.prototype.updateButton=function(a,o,r,e){(e=void 0!==e&amp;&amp;e)&amp;&amp;(e.removeClass(&#8220;loading&#8221;),e.addClass(&#8220;added&#8221;),wc_add_to_cart_params.is_cart||0!==e.parent().find(&#8220;.added_to_cart&#8221;).length||e.after(&#8216; <a href=\"'+wc_add_to_cart_params.cart_url+'\" title=\"'+wc_add_to_cart_params.i18n_view_cart+'\">&#8216;+wc_add_to_cart_params.i18n_view_cart+&#8221;<\/a>&#8220;),t(document.body).trigger(&#8220;wc_cart_button_updated&#8221;,[e]))},a.prototype.updateCartPage=function(){var a=window.location.toString().replace(&#8220;add-to-cart&#8221;,&#8221;added-to-cart&#8221;);t(&#8220;.shop_table.cart&#8221;).load(a+&#8221; .shop_table.cart:eq(0) &gt; *&#8221;,function(){t(&#8220;.shop_table.cart&#8221;).stop(!0).css(&#8220;opacity&#8221;,&#8221;1&#8243;).unblock(),t(document.body).trigger(&#8220;cart_page_refreshed&#8221;)}),t(&#8220;.cart_totals&#8221;).load(a+&#8221; .cart_totals:eq(0) &gt; *&#8221;,function(){t(&#8220;.cart_totals&#8221;).stop(!0).css(&#8220;opacity&#8221;,&#8221;1&#8243;).unblock(),t(document.body).trigger(&#8220;cart_totals_refreshed&#8221;)})},a.prototype.updateFragments=function(a,o){o&amp;&amp;(t.each(o,function(a){t(a).addClass(&#8220;updating&#8221;).fadeTo(&#8220;400&#8243;,&#8221;0.6&#8221;).block({message:null,overlayCSS:{opacity:.6}})}),t.each(o,function(a,o){t(a).replaceWith(o),t(a).stop(!0).css(&#8220;opacity&#8221;,&#8221;1&#8243;).unblock()}),t(document.body).trigger(&#8220;wc_fragments_loaded&#8221;))},new a});<br \/>\n    &lt;\/script&gt;<br \/>\n    &lt;?php<br \/>\n}<\/p>\n","protected":false},"template":"","class_list":["post-15168222","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/15168222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/15168222\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=15168222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}