Set a specific product price conditionally on woocommerce product & cart page

Set a specific product price conditionally on woocommerce product & cart page
603 Views
0
(0)

(Only for a defined product ID)

To make it work as you want to change the simple products prices only on single product pages (without altering archives product prices, related product prices, upsells and cross-sells) if any cart item doesn’t belong to a specific product category.

Then you will need all this following code:

<?php
 // Change displayed price on single product pages
 add_action( 'woocommerce_single_product_summary', 'cxc_change_simple_product_price_html', 9 );

 function cxc_change_simple_product_price_html() {
 	global $product;

      // Only for product ID 87
 	if ( $product->get_id() != 87 ) {
 		return;
 	} 

      // HERE set your product category term ID, slug or name
 	$product_category = 't-shirts';

      // Checking for the product category in cart items loop
 	foreach ( WC()->cart->get_cart() as $cart_item ) {
 		
 		if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) ){
 			
              return; // Product category found ==> We EXIT from this function
          }
      }

      remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
      add_action( 'woocommerce_single_product_summary', 'cxc_custom_simple_product_price_html', 10 );
  }

  function cxc_custom_simple_product_price_html(){
  	global $product;

      $addp = 10; // Here the price additional amount

      $price = wc_price( wc_get_price_to_display( $product ) + $addp ) . $product->get_price_suffix();

      echo '<p class="price">' . apply_filters( 'woocommerce_get_price_html', $price, $product ) . '</p>';
  }

  // Add custom calculated price to cart item data
  add_filter( 'woocommerce_add_cart_item_data', 'cxc_add_cart_simple_product_custom_price', 20, 2 );

  function cxc_add_cart_simple_product_custom_price( $cart_item_data, $product_id ){
    // Only for product ID 87
  	if ( $product_id != 87 ) {
  		return $cart_item_data;
  	}

    $product = wc_get_product( $product_id ); // The WC_Product Object

    // Only if product is not on sale
    if ( $product->is_on_sale() ) {
    	return $cart_item_data;
    }

    $price = (float) $product->get_price();

    // Set the custom amount in cart object
    $cart_item_data['new_price'] = $price + 10;

    return $cart_item_data;
}

// Set custom calculated price to cart
add_action( 'woocommerce_before_calculate_totals', 'cxc_set_cart_simple_product_custom_price', 20, 1 );

function cxc_set_cart_simple_product_custom_price( $cart ) {

	if ( is_admin() && ! defined( 'DOING_AJAX' ) ){

		return;

	}

	if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ){

		return;
	}

    // HERE set your product category term ID, slug or name
	$product_category = 't-shirts';

    // 1st cart items Loop: checking for the product category
	foreach ( $cart->get_cart() as $cart_item ) {

		if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) ){

              return; // Product category found ==> We EXIT from this function

          }
      }

      // 2nd Loop: Changing cart item prices (Product category not found)
      foreach ( $cart->get_cart() as $cart_item ) {

      	if( isset( $cart_item['new_price'] ) ) {

      		$cart_item['data']->set_price( $cart_item['new_price'] );

      	}

      }
  }
?>

Code goes in functions.php file of your active child theme (or active theme).

How useful was this blog?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this blog.

Leave a comment

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