add_action( 'woocommerce_sale_flash', 'pancode_echo_sale_percent' );
/**
* Echo discount percent badge html.
*
* @param string $html Default sale html.
*
* @return string
*/
function pancode_echo_sale_percent( $html ) {
global $product;
/**
* @var WC_Product $product
*/
$regular_max = 0;
$sale_min = 0;
$discount = 0;
if ( 'variable' === $product->get_type() ) {
$prices = $product->get_variation_prices();
$regular_max = max( $prices['regular_price'] );
$sale_min = min( $prices['sale_price'] );
} else {
$regular_max = $product->get_regular_price();
$sale_min = $product->get_sale_price();
}
if ( ! $regular_max && $product instanceof WC_Product_Bundle ) {
$bndl_price_data = $product->get_bundle_price_data();
$regular_max = max( $bndl_price_data['regular_prices'] );
$sale_min = max( $bndl_price_data['prices'] );
}
if ( floatval( $regular_max ) ) {
$discount = round( 100 * ( $regular_max - $sale_min ) / $regular_max );
}
return '<span class="onsale">- ' . esc_html( $discount ) . '%</span>';
}
add_filter( 'woocommerce_add_to_cart_validation', 'one_cart_item_at_the_time', 10, 3 );
function one_cart_item_at_the_time( $passed, $product_id, $quantity ) {
if( ! WC()->cart->is_empty())
WC()->cart->empty_cart();
return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'one_cart_item_at_the_time', 10, 3);
function one_cart_item_at_the_time( $passed, $product_id, $quantity ) {
if(! WC()->cart->is_empty()) {
$cartId = WC()->cart->generate_cart_id($product_id);
$cartItemKey = WC()->cart->find_product_in_cart($cartId);
if ($cartItemKey) {
return $passed;
} else {
$woocommerce->cart->add_to_cart( $product_id );
return $passed;
}
}
}