Add Badge to New products - wordpress

i'm trying to make a change on my Woocomerce Store. I want to add a badge to my New Products, my theme doesn't support this so i want to re-use a function that i find for Sale Products and Apply to New products but i'm not finding how to change it to make it work.
I have the next function that works for add percent to sale badge, i want just to modify for New products only adding the span with the class and text.
function custom_product_sale_flash( $output, $post, $product ) {
global $product;
if($product->is_on_sale()) {
if($product->is_type( 'variable' ) )
{
$regular_price = $product->get_variation_regular_price();
$sale_price = $product->get_variation_price();
} else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
$percent_off = (($regular_price - $sale_price) / $regular_price) * 100;
return '<span class="onsale">' . round($percent_off) . '% OFF</span>';
}
}
add_filter( 'woocommerce_sale_flash', 'custom_product_sale_flash', 11, 3 );
Thank you in advance.

You can use one of the actions used in the file templates/content-product.php to hook into the rendering of the HTML.
add_action('woocommerce_before_shop_loop_item_title', function() {
global $post;
$days = 5;
$offset = $days*60*60*24;
if ( get_post_time() < date('U') - $offset )
return '<span class="new-product">NEW</span>';
}
});

Related

How to add a step of 100 and inscribe in the quantity of goods? WooCommerce

I have a website. I wrote a code that adds "per 100g" to the product price (in the picture point 2). Here is the code:
add_filter( 'woocommerce_get_price_html', 'wb_change_product_html' );
function wb_change_product_html( $price ) {
$cat_arr = get_the_terms(get_the_ID(),'product_cat');
//return $a[0]->name;
$temp = 0;
foreach ($cat_arr as $singl_cat) {
if($singl_cat->name == 'perunit') {
$temp = 1;
}
}
if ( $temp ) {
$price_html = '<span class="amount">' . 'per unit \' ' . $price . '</span>';
} else {
$price_html = '<span class="amount">' . ' per 100g \ ' . $price . '</span>';
}
return $price_html;
}
I need to add an inscription to the "number of items added to the cart" (in the picture point 1).
The logic is that if I have a category "per unit", then everything remains by default. But if there is no category, then the inscription "price per X gram" is added to the quantity of goods, where X is the quantity of grams (minimum 100) and take a step of 100 grams.
I don't know how to do this, can anyone suggest?
It's pretty simple (from official docs https://docs.woocommerce.com/document/adjust-the-quantity-input-values/) - add this to your functions.php
/**
* Adjust the quantity input values
*/
add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 ); // Simple products
function jk_woocommerce_quantity_input_args( $args, $product ) {
$cat_arr = get_the_terms(get_the_ID(),'product_cat');
if( 'perunit' == $cat_arr[0]->name ) {
if ( is_singular( 'product' ) ) {
$args['input_value'] = 100; // Starting value (we only want to affect product pages, not cart)
}
$args['max_value'] = 1000; // Maximum value
$args['min_value'] = 100; // Minimum value
$args['step'] = 100; // Quantity steps
return $args;
}
}

Show discount percentage in WooCommerce sale button

I'm looking for a way to show the percentage of discount in the sale bubble in WooCommerce. Here is an image how the button looks now:
So, basically, the button will show: -20%
You should be able to hook into the woocommerce_sale_flash filter, grab the product object, work out the percentage and add it to the HTML.
Something like this:
add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_bubble' );
function add_percentage_to_sale_bubble( $html ) {
global $product;
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
$output =' <span class="onsale">VERKOOP -'.$percentage.'%</span>';
return $output;
}
Edit - Variable Products:
With variable products getting thrown into the mix, you will need to include a check using is_type('simple|variable') and adjust your calculations from there, like this:
add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_bubble', 20 );
function add_percentage_to_sale_bubble( $html ) {
global $product;
if ($product->is_type('simple')) { //if simple product
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ).'%';
} else { //if variable product
$percentage = get_variable_sale_percentage( $product );
}
$output =' <span class="onsale">-'.$percentage.'</span>';
return $output;
}
function get_variable_sale_percentage( $product ) {
//get variables
$variation_min_regular_price = $product->get_variation_regular_price('min', true);
$variation_max_regular_price = $product->get_variation_regular_price('max', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
$variation_max_sale_price = $product->get_variation_sale_price('max', true);
//get highest and lowest percentages
$lower_percentage = round( ( ( $variation_min_regular_price - $variation_min_sale_price ) / $variation_min_regular_price ) * 100 );
$higher_percentage = round( ( ( $variation_max_regular_price - $variation_max_sale_price ) / $variation_max_regular_price ) * 100 );
//sort array
$percentages = array($lower_percentage, $higher_percentage);
sort($percentages);
if ($percentages[0] != $percentages[1] && $percentages[0]) {
return $percentages[0].'% - '.$percentages[1].'%';
} else {
return $percentages[1].'%';
}
}

Woocommerce override Product Price with custom value on cart and checkout

i was wondering if someone had some experience with the following scenario.
I'm using WooCommerce to have my products listed, ready for sale. Some products (about 20) have a massive amount of variations, so i decided to have the product data in separated tables, and via SKU (simple query to get the data) to load them into woocommerce. So far, everything works great. These products are working fine. Their variations are showing up, and the user can select them, to add them to cart.
Thing is, i have setup these 20 products as single products, and gave them a price of $1, just to show the add to cart button (.single_add_to_cart_button), but when im selecting my variations (from the outside table/tables) the product gets added fine into the cart, but it shows for every product in that case, a price of $1, with all the custom fields included (which is nice). As far as i know, these custom fields are from the sessions). Below is all my code that im using in my functions.php (not mine, found on the net, fixed it to work with my case)
// Step 1: Add Data in a Custom Session, on ‘Add to Cart’ Button Click
add_action('wp_ajax_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
add_action('wp_ajax_nopriv_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
function wdm_add_user_custom_data_options_callback()
{
$product_id = $_POST['id'];
$product_category = $_POST['product_category'];
$custom_data_1 = $_POST['custom_data_1'];
$custom_data_2 = $_POST['custom_data_2'];
$custom_data_3 = $_POST['custom_data_3'];
$custom_data_4 = $_POST['custom_data_4'];
$custom_data_5 = $_POST['custom_data_5'];
session_start();
$_SESSION['custom_data_1'] = $custom_data_1;
$_SESSION['custom_data_2'] = $custom_data_2;
$_SESSION['custom_data_3'] = $custom_data_3;
$_SESSION['custom_data_4'] = $custom_data_4;
$_SESSION['custom_data_5'] = $custom_data_5;
die();
}
// Step 2: Add Custom Data in WooCommerce Session
add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,2);
if(!function_exists('wdm_add_item_data'))
{
function wdm_add_item_data($cart_item_data,$product_id)
{
global $woocommerce;
session_start();
$new_value = array();
if (isset($_SESSION['custom_data_1'])) {
$option1 = $_SESSION['custom_data_1'];
$new_value['custom_data_1'] = $option1;
}
if (isset($_SESSION['custom_data_2'])) {
$option2 = $_SESSION['custom_data_2'];
$new_value['custom_data_2'] = $option2;
}
if (isset($_SESSION['custom_data_3'])) {
$option3 = $_SESSION['custom_data_3'];
$new_value['custom_data_3'] = $option3;
}
if (isset($_SESSION['custom_data_4'])) {
$option4 = $_SESSION['custom_data_4'];
$new_value['custom_data_4'] = $option4;
}
if (isset($_SESSION['custom_data_5'])) {
$option5 = $_SESSION['custom_data_5'];
$new_value['custom_data_5'] = $option5;
}
if( empty($option1) && empty($option2) && empty($option3) && empty($option4) && empty($option5) )
return $cart_item_data;
else
{
if(empty($cart_item_data))
return $new_value;
else
return array_merge($cart_item_data,$new_value);
}
unset($_SESSION['custom_data_1']);
unset($_SESSION['custom_data_2']);
unset($_SESSION['custom_data_3']);
unset($_SESSION['custom_data_4']);
unset($_SESSION['custom_data_5']);
}
}
// Step 3: Extract Custom Data from WooCommerce Session and Insert it into Cart Object
add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
if(!function_exists('wdm_get_cart_items_from_session'))
{
function wdm_get_cart_items_from_session($item,$values,$key)
{
if (array_key_exists( 'custom_data_1', $values ) )
{
$item['custom_data_1'] = $values['custom_data_1'];
}
if (array_key_exists( 'custom_data_2', $values ) )
{
$item['custom_data_2'] = $values['custom_data_2'];
}
if (array_key_exists( 'custom_data_3', $values ) )
{
$item['custom_data_3'] = $values['custom_data_3'];
}
if (array_key_exists( 'custom_data_4', $values ) )
{
$item['custom_data_4'] = $values['custom_data_4'];
}
if (array_key_exists( 'custom_data_5', $values ) )
{
$item['custom_data_5'] = $values['custom_data_5'];
}
return $item;
}
}
// Step 4: Display User Custom Data on Cart and Checkout page
add_filter('woocommerce_checkout_cart_item_quantity','wdm_add_user_custom_option_from_session_into_cart',1,3);
add_filter('woocommerce_cart_item_price','wdm_add_user_custom_option_from_session_into_cart',1,3);
if(!function_exists('wdm_add_user_custom_option_from_session_into_cart'))
{
function wdm_add_user_custom_option_from_session_into_cart($product_name, $values, $cart_item_key )
{
if(count($values['custom_data_1']) > 0)
{
$return_string = $product_name . "";
$return_string .= "";
$return_string .= "SKU : " . $values['custom_data_1'] . "";
//$return_string .= "Code : " . $values['custom_data_2'] . "";
$return_string .= "Width : " . $values['custom_data_3'] . " cm";
$return_string .= "Height : " . $values['custom_data_4'] . " cm";
$return_string .= "Price : € " . $values['custom_data_5'] . "";
$return_string .= "";
return $return_string;
}
else
{
return $product_name;
}
}
}
// Step 5: Add Custom Data as Metadata to the Order Items
add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
if(!function_exists('wdm_add_values_to_order_item_meta'))
{
function wdm_add_values_to_order_item_meta($item_id, $values)
{
global $woocommerce,$wpdb;
$user_custom_values = $values['wdm_user_custom_data_value'];
if(!empty($user_custom_values))
{
wc_add_order_item_meta($item_id,'wdm_user_custom_data',$user_custom_values);
}
$custom_data_1 = $values['custom_data_1'];
if(!empty($custom_data_1))
{
wc_add_order_item_meta($item_id,'custom_data_1',$custom_data_1);
}
$custom_data_2 = $values['custom_data_2'];
if(!empty($custom_data_2))
{
wc_add_order_item_meta($item_id,'custom_data_2',$custom_data_2);
}
$custom_data_3 = $values['custom_data_3'];
if(!empty($custom_data_3))
{
wc_add_order_item_meta($item_id,'custom_data_3',$custom_data_3);
}
$custom_data_4 = $values['custom_data_4'];
if(!empty($custom_data_4))
{
wc_add_order_item_meta($item_id,'custom_data_4',$custom_data_4);
}
$custom_data_5 = $values['custom_data_5'];
if(!empty($custom_data_5))
{
wc_add_order_item_meta($item_id,'custom_data_5',$custom_data_5);
}
}
}
// Step 6: Remove User Custom Data, if Product is Removed from Cart
add_action('woocommerce_before_cart_item_quantity_zero','wdm_remove_user_custom_data_options_from_cart',1,1);
if(!function_exists('wdm_remove_user_custom_data_options_from_cart'))
{
function wdm_remove_user_custom_data_options_from_cart($cart_item_key)
{
global $woocommerce;
// Get cart
$cart = $woocommerce->cart->get_cart();
// For each item in cart, if item is upsell of deleted product, delete it
foreach( $cart as $key => $values)
{
if ( $values['wdm_user_custom_data_value'] == $cart_item_key )
unset( $woocommerce->cart->cart_contents[ $key ] );
}
}
}
i tried to include the following code
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 999; // This is the custom price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
it changes the price to 999, since its fixed, but in my case, i am not able to include the value
$custom_data_5 = $_POST['custom_data_5']; // thats my unit price which needs to replace the $1 value of the simple product value from the product
any help would be appreciated on my side, since I'm running out of options right now, and this solution seems to be to correct one for me.
Jan
all right, i think i got it done, searching and searching on stackoverflow.com of course ....
this i what i did :
function ipe_add_to_cart_link() {
global $product;
$custom_price = 30;
echo sprintf( '%s',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
$myCustomPrice, // Thats the value im getting from my add_to_cart_button - hidden
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
}
add_filter('woocommerce_loop_add_to_cart_link','ipe_add_to_cart_link');
function ipe_product_custom_price( $cart_item_data, $product_id ) {
if( isset( $_POST['myCustomPrice'] ) && !empty($_POST['myCustomPrice'])) {
$cart_item_data[ "myCustomPrice" ] = $_POST['myCustomPrice'];
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'ipe_product_custom_price', 99, 2 );
function ipe_apply_custom_price_to_cart_item( $cart_object ) {
if( !WC()->session->__isset( "reload_checkout" )) {
foreach ( $cart_object->cart_contents as $key => $value ) {
if( isset( $value["myCustomPrice"] ) ) {
$value['data']->price = $value["myCustomPrice"];
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'ipe_apply_custom_price_to_cart_item', 99 );
and placed everything in functions.php - worked like a charm. Hope that this solution is helping someone out there.
Jan

filter by price based on product category

I am developing site in WordPress using woo-commerce
I want to achieve the functionality that on single page price will be displayed by counting 50% discount on single product page
I had implemented the below code, it is working nicely on the simple products but not working on variable product
Please help me how can I achieve the custom price filtering on variable product
OR suggest me which filter to used to variable products
add_filter('woocommerce_get_price', 'custom_price_WPA111772', 10, 2);
function custom_price_WPA111772($price, $product) {
global $product;
$pid = get_the_ID();
$user = wp_get_current_user();
if(!is_user_logged_in()) return $price;
if( has_term( 'sale', 'product_cat' ,$product->ID) || has_term( 'Gift Ideas', 'product_cat' ,$product->ID))
{
$price = $price;
}
else
{
$price = $price * 50 / 100;
}
return $price;
}
I got the answer why my variable product is not showing custom price
i added this in function.php and its works
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}

woocommerce add custom price while add to cart

Hi I need to add an extra price to product price while add to cart.
http://url/warenkorb/?add-to-cart=1539&added_price=5.00
I have used the code following code to achive this.
add_filter( 'woocommerce_add_cart_item', 'c_other_options_add_cart_item', 20, 1 );
function c_other_options_add_cart_item( $cart_item ) {
if (isset($cart_item['_other_options'])) :
if( isset($cart_item['_other_options']['product-price']) )
$extra_cost = floatval($cart_item['_other_options']['product-price']);
$cart_item['data']->adjust_price( $extra_cost );
// here the real adjustment is going on...
endif;
return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', c_other_options_add_cart_item_data', 10, 2 );
function c_other_options_add_cart_item_data($cart_item_meta, $product_id){
global $woocommerce;
$product = new WC_Product( $product_id);
$price = $product->price;
if(empty($cart_item_meta['_other_options']))
$cart_item_meta['_other_options'] = array();
$cart_item_meta['_other_options']['product-price'] = esc_attr($_REQUEST['price']) - $price;
return $cart_item_meta;
}
It shows the modified price on add to cart page but not in the cart/checkout page. Please Help me to achieve this. Thanks in advance.
You have to use woocommerce_before_calculate_totals hook for this purpose,
like this
function calculate_extra_fee( $cart_object ) {
/* Extra fee */
$additionalPrice = 100;
foreach ( $cart_object->cart_contents as $key => $value ) {
/* if you want to add extra fee for particular product, otherwise remove the if condition */
if( $value['product_id'] == 100) {
$quantity = intval( $value['quantity'] );
$orgPrice = intval( $value['data']->price );
$value['data']->price = ( ( $orgPrice + $additionalPrice ) * $quantity );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_extra_fee', 1, 1 );

Resources