dimanche 28 février 2016

Target Specific Product when calling woocommerce_after_shop_loop_item

I am trying to change the add to cart functionality on the product archive/shop page on woocommerce based on a custom field using woocommerce_after_shop_loop_item. However if only even one item with the custom field exists on the page the code will effect all products even the ones without the custom field. And I completely understand why this is happening. My question is, is there any way to only apply the action to that one specific product.

/*STEP 1 - REMOVE ADD TO CART BUTTON ON PRODUCT ARCHIVE (SHOP) */

function remove_loop_button(){
 global $post;

 if($post->my_custom_field)
 {
  remove_action( 'woocommerce_after_shop_loop_item',
 'woocommerce_template_loop_add_to_cart', 10 );
 }

}
add_action('init','remove_loop_button');



/*STEP 2 -ADD NEW BUTTON THAT LINKS TO PRODUCT PAGE FOR EACH PRODUCT */

add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
 global $product, $post;

 if($post->my_custom_field)
 {
   $link = $product->get_permalink();
   echo do_shortcode('<a href="'.$link.'" class="button addtocartbutton">View Product</a>');
 }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire