woocommerce filter

Woocommerce Filter

Remove product link on checkout page

Using add filter woocommerce_cart_item_name we can modify woocommerce product name with condition check for checkout page.

//remove product link on checkout page
add_filter('woocommerce_cart_item_name', 'custom_filter_wc_cart_item_remove_link', 10, 3);
function custom_filter_wc_cart_item_remove_link($product_name, $cart_item, $cart_item_key)
{
   if (is_checkout()) {
      $wccart = new WC_Cart();
      $name = apply_filters('woocommerce_cart_item_remove_link', sprintf(
' ',
esc_url($wccart->get_remove_url($cart_item_key)), __('Remove this item', 'woocommerce'), esc_attr($cart_item['product_id']), esc_attr($cart_item['data']->get_sku())
), $cart_item_key);
      return $name.$product_name;
   }
}

add only one product in cart and redirect to checkout page

To fulfill client requirements that only one product is able to add in cart no matter client how many times add product.

//add only one product in cart and redirect to checkout page
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );
function check_if_cart_has_product( $valid, $product_id, $quantity ) {
    if(!empty(WC()->cart->get_cart()) && $valid){
        foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
       if( $product_id == $_product->id ) {
           unset(WC()->cart->cart_contents[$cart_item_key]);
           return wc_get_checkout_url();
       }
   }
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *