How we display discount percentage badge and sale badge separately on WooCommerce product? - slider

Want to display discount percentage badge on right hand side of product image and sale badge on left hand side of product image in products slider. So, Please Suggest some hooks for this functionality!
Tried to add the following hook but it will replace the existing sale badge with the discount percentage badge on shop page and also this hook is not working for the product slider on homepage.
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">'.$percentage.'%</span>'; return $output; }

you can use the following hook
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products
if( $product->is_on_sale() && ! is_admin() && $product->is_type('simple') ){
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
// Active price (the "Sale price" when on-sale)
$sale_price = (float) $product->get_price();
// "Saving Percentage" calculation and formatting
$precision = 0; // Max number of decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), $precision ) . '%';
// Append to the formated html price
$price .= sprintf( __('<p class="saved-sale">%s</p>', 'woocommerce' ), $saving_percentage );
}
return $price;
}

Related

Show max variable price in shop page [duplicate]

This question already has answers here:
Replace WooCommerce variable products price range with 'Up to' and the max price
(2 answers)
Closed 3 years ago.
I want to display the maximum variation price in woocommerce shop page under product title.
I have tried using this code but does not seem to work, only breaks my site instead.
add_filter( ‘woocommerce_variable_sale_price_html’, ‘con_show_max_variation_price_only’, 10, 2 );
add_filter( ‘woocommerce_variable_price_html’, ‘con_show_max_variation_price_only’, 10, 2 );
function con_show_max_variation_price_only( $price, $product ) {
// Main Variation Price
$prices = array( $product->get_variation_price( ‘max’, true ), $product->get_variation_price( ‘min’, true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( ‘%1$s’, ‘woocommerce’ ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
I think this is what you want. I tested the code with Envo Multipurpose theme.
// define the woocommerce_variable_price_html callback
function filter_woocommerce_variable_price_html( $wc_format_price_range, $instance ) {
$index = strpos($wc_format_price_range, "– ");
$wc_format_price_range = substr($wc_format_price_range, $index+7);
//This will remove the first price by dividing the standard string with dash
//You may add a prefix like, $wc_format_price_range = "Max Price: $wc_format_price_range";
return $wc_format_price_range;
};
// add the filter
add_filter( 'woocommerce_variable_price_html', 'filter_woocommerce_variable_price_html', 10, 2 );
I hope this will help you. Have a good day.

How to set a maximum stock level for total WooCommerce variations sold, disregarding individual variation stock levels

I'm wondering if we are able to hook into WooCommerce to set a maximum amount of stock that can be purchased for a variable product. Disregarding the individual variation stock levels once this maximum amount is reached.
For example, I have a variable product selling workshop groups. There are 4 variations, each with a stock level set at 100. This is because no group can have more than 100 people in. However, only 250 tickets are available for sale (not 400 that we might expect because of the 4x100 quantity).
So this works as far as the max 100 places per workshop group. We just need to somehow be able to limit the total stock level of all 4 variations to 250.
I had hoped enabling the parent product "Manage stock" option and setting this to 250 would work. But obviously, variations must override this. If we can hook into that and turn that back on even when variation stock management is in use that might be a nice way of solving this.
Thanks for any help.
I came up with a solution to my problem by doing the following:
Add 2 custom fields to the WooCommerce product page, which will store the max quantity of the total variations we can sell and also the max quantity of an individual variation. The code for this is:
// Modify WooCommerce Product Settings
add_action('woocommerce_product_options_inventory_product_data', 'wc_add_custom_field' );
function wc_add_custom_field() {
$fields = array('Total quantity' => 'total_quantity','Variation quantity' => 'variation_quantity');
$field_description = array('total_quantity' ='description','variation_quantity' ='description');
$field_placeholder = array('total_quantity' =>'e.g. 300','variation_quantity' =>'e.g. 100');
foreach ($fields as $key => $value) {
woocommerce_wp_text_input( array(
'id' => $value,
'label' => $key,
'description' => $field_description[$value],
'desc_tip' => 'true',
'placeholder' => $field_placeholder[$value]
) );
}
}
// Save Fields
add_action( 'save_post_product', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
update_post_meta( $post_id, 'total_quantity', $_POST['total_quantity'] );
update_post_meta( $post_id, 'variation_quantity', $_POST['variation_quantity'] );
}
Add cart/basket validation rules to stop customers being able to purchase products that exceed the value of the custom "total_quantity" field added above:
add_action( "woocommerce_add_to_cart_validation","sc_woocommerce_add_to_cart_validation", 1, 5 );
function sc_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id, $variations ) {
// Iterate through each variation and get the total stock remaining
$product_variable = new WC_Product_Variable($product_id);
$product_variations = $product_variable->get_available_variations();
settype($variation_stock_availability, "integer");
foreach ($product_variations as $variation) {
$variation_stock_availability = +(int)$variation['max_qty'];
}
$count_variations = count($product_variations);
$total_quantity = get_post_meta( $product_id, 'total_quantity', true );
$variation_quantity = get_post_meta( $product_id, 'variation_quantity', true );
// formula to test if any stock remaining based on sold variations
$formula = $count_variations * $variation_quantity;
$formula1 = (int)$formula + (int)$quantity;
$formula1 = $formula1 - $variation_stock_availability;
// Iterating through each cart item and use the current running quantity in the cart in the forumula
foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){
// count(selected category) quantity
$running_qty += (int) $cart_item['quantity'];
$formula2 = (int)$formula + (int)$running_qty;
$formula2 = $formula2 - $variation_stock_availability;
// More than allowed products in the cart is not allowed
if ($formula2 >= $total_places) {
wc_add_notice( sprintf( __( "Unfortunately there is no availability based on your selection", "donaheys" )), 'error' );
$passed = false;
return $passed;
}
}
// More than allowed products in the cart is not allowed
if ($formula1 >= $total_places) {
// Add the error
wc_add_notice( sprintf( __( "Unfortunately there is no availability based on your selection", "donaheys" )), 'error' );
$passed = false;
return $passed;
} else {
$passed = true;
return $passed;
}
$running_qty = 0;
}
The result of the above code ensures we are can set a maximum amount of stock that can be purchased for a variable product, disregarding the individual variation stock levels once this maximum amount is reached.

Prestashop 1.6 Product Showing wrong price combination when using custome group discount

Prestashop 1.6.1.10
When creating a product with combination and a customer group discount, the product page display the price without the discount.
But when you add the product to cart the discount is apply.
See a product with combination and group discount
I solved by the following code change in controllers\front\ProductController.php
add function:
protected function getGroupReduction() {
$id_customer = (isset($this->context->customer) ? (int)$this->context->customer->id : 0);
$id_group = (int)Group::getCurrent()->id;
$id_country = $id_customer ? (int)Customer::getCurrentCountry($id_customer) : (int)Tools::getCountry();
$group_reduction = GroupReduction::getValueForProduct($this->product->id, $id_group);
if ($group_reduction === false) {
$group_reduction = Group::getReduction((int)$this->context->cookie->id_customer) / 100;
}
return $group_reduction;
}
replace line 467:
$combinations[$row['id_product_attribute']]['price'] = (float)Tools::convertPriceFull($row['price'], null, Context::getContext()->currency, false);
with the following lines:
$group_reduction = $this->getGroupReduction();
$price = $row['price'] - $row['price']* $group_reduction;
$combinations[$row['id_product_attribute']]['price'] = (float)Tools::convertPriceFull($price, null, Context::getContext()->currency, false);

I want to disable cod when 100 cod orders is completed in prestashop

I want to disable the cod delivery feature in the order page of prestashop 1.6.3 when 100 /50 orders ( this will be a parameter) is completed per day.
How to disable this programmatically by finding out whether 100 cod is been completed .
I will modify the hookPayment() in cashondelivery module to do this :
public function hookPayment($params)
{
if (!$this->active)
return ;
global $smarty;
// Check if cart has product download
if ($this->hasProductDownload($params['cart']))
return false;
//Check whether the cod done exceeds the daily limit if yes dont display the cod option
$cod_limit = Configuration::get('PS_KITS_COD_DAILY_LIMIT');// number of cod
$sql = "select count(*) AS cod_count from ps_orders where module='cashondelivery' and date(date_add) = CURDATE() and ( current_state= 3 or current_state=4)";
if ($row = Db::getInstance()->getRow($sql)){
$cod_count = $row['cod_count'];
}
if ($cod_count >= $cod_limit){
return ;
}
$smarty->assign(array(
'this_path' => $this->_path, //keep for retro compat
'this_path_cod' => $this->_path,
'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/'
));
return $this->display(__FILE__, 'payment.tpl');
}

How set quantity to out of stock in Prestashop

i'd like that when my product quantity is equal to 1, the product is out of stock ( and not when quantity is equal to 0 ).
Is it possible ?
How ?
In PrestaShop 1.6 (tested and confirmed working in v1.6.0.14) you can accomplish this by the following method, where the website will always show an available stock quantity that is the real quantity minue one. If you have 6 in stock this modification will change your website to report the stock as 5 to your customers, when you have only 1 stock available, customers will see the product with 0 quantity and marked as out of stock.
Copy the file /classes/stock/StockAvailable.php to /override/classes/stock/StockAvailable.php.
Edit the file /override/classes/stock/StockAvailable.php as follows.
At lines 357-380 is the function AvailableStock::getQuantityAvailableByProduct() that will normally read like this (formatting may vary slightly):
public static function getQuantityAvailableByProduct( $id_product,
$id_product_attribute = null, $id_shop = null ) {
if( $id_product_attribute === null ) $id_product_attribute = 0;
$skey = 'StockAvailable::getQuantityAvailableByProduct_' . (int)$id_product . '-' .
(int)$id_product_attribute . '-' . (int)$id_shop;
if( !Cache::isStored( $key )) {
$query = new DbQuery();
$query->select( 'SUM(quantity)' );
$query->from( 'stock_available' );
if( $id_product !== null ) $query->where( 'id_product = ' . (int)$id_product );
$query->where( 'id_product_attribute = ' . (int)$id_product_attribute );
$query = StockAvailable::addSqlShopRestriction( $query, $id_shop );
Cache::store( $key, (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue( $query ));
}
return Cache::retrieve( $key );
}
Replace the last line of the function beginning with the keyword return to the following:
$iStockQty = Cache::retrieve( $key );
if( $iStockQty > 0 ) $iStockQty--;
return $iStockQty;
Delete the file /cache/class_index.php so that Prestashop automatically re-creates this file taking into account the new override file.
I also found the method self::removeProductFromStockAvailable($id_product) in StockAvailable.php to do some similar stuff.