Last active
October 31, 2018 17:00
-
-
Save nicomollet/6792b52a3d250196a2877277fcc53963 to your computer and use it in GitHub Desktop.
Product Open Pricing plugin: Customize Product Name with Price
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Product Open Pricing plugin: Customize Product Title with Price | |
* | |
* @param WC_Cart $cart | |
*/ | |
function custom_woocommerce_before_calculate_totals( $cart ) { | |
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) | |
return; | |
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) | |
return; | |
// Loop through cart items | |
foreach ( $cart->get_cart() as $cart_item ) { | |
if( empty($cart_item['alg_open_price'])){ | |
continue; | |
} | |
// Get an instance of the WC_Product object | |
$product = $cart_item['data']; | |
$original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title; | |
$new_name = 'Product with custom price of '.wp_filter_nohtml_kses(wc_price($cart_item['alg_open_price'])); | |
if( method_exists( $product, 'set_name' ) ) | |
$product->set_name( $new_name ); | |
else | |
$product->post->post_title = $new_name; | |
} | |
} | |
add_action( 'woocommerce_before_calculate_totals', 'custom_woocommerce_before_calculate_totals', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment