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

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);

Related

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

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;
}

Multiple column orderBy in Laravel 8

I have an E-commerce website, I want to sort the products in product page by in-stock first so I did this and it works just fine:
$products = Product::orderByDesc('quantity')->get();
Now, I added a new column to the product table which is product_order that will take an integer so that I can custom sort the products, I tried the following code:
$products = Product::orderByDesc('quantity')->orderByDesc('product_order')->get();
The problem is that it only sorts them by quantity, the second orderByDesc is not effective, how can I make it effective?
I managed to solve the issue by doing the following:
$productsQty = Product::orderByDesc('product_order');
$products = $productsQty->orderByDesc('quantity')->get();
foreach ($products as $product) {
if($product->quantity <= 0) {
$product->product_order = 0;
$product->save();
}
}

How to add Product in Cart in Prestashop 1.6.7

I am trying to add product in Cart in Prestashop 1.6.7.
But it doesn't working.
Below is my code:
$context=Context::getContext();//new Cart();
$id_cart=$context->cookie->__get('id_cart');
$lan_id = $this->context->language->id;
$cur_id = $this->context->currency->id;
$products_ids=16571;
$cart=new Cart($id_cart);
$cart->id_currency=$cur_id;
$cart->id_lang=$lan_id;
$cart->updateQty(1,$products_ids, null, false);
But above code is not working.
Somehow i manage my code as per below:
global $context;
$context=Context::getContext();//new Cart();
$id_cart=$context->cookie->__get('id_cart');
$lan_id = $context->cookie->__get('id_lang');
$cur_id = $context->cookie->__get('id_currency');
$products_ids=16571;
$cart=new Cart($id_cart);
$cart->id_currency=$cur_id;
$cart->id_lang=$lan_id;
$this->context->cart->update();
$cart->updateQty(1,16571);
But there is one error for carrier
There are no carriers available that deliver to this address.
Try adding the following code at the end of your code.
$this->context->cart->save();
$this->context->cookie->id_cart = (int)$this->context->cart->id;
$this->context->cookie->write();
$this->context->cart->autosetProductAddress();
Hook::exec('actionAuthentication');
CartRule::autoRemoveFromCart($this->context);
CartRule::autoAddToCart($this->context);
I add some static Carrier ID in
classes/Cart.php
in getPackageList function at line no. approx. 1892
if (empty($product['carrier_list'])) {
$product['carrier_list'] = array(
'0' => 10,
'1' => 11
);
}

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');
}

Exclude products out of stock from list of new products - Prestashop

I am using a module that displays new added products in home page. I need to customize the module so that this list doesnt contain products sold. In other words, if a product is out of stock before the number of days it is condidered new is over, then do not show this product in list.
I can do it in view part by using {if $product.quantity < 0}{/if} but my goal is to perform it in controller. Here is my code:
function hookHome($params)
{
global $smarty, $cookie;
$nb = intval(Configuration::get('HOME_NEW_PRODUCTS_NBR'));
$rand = intval(Configuration::get('HOME_NEW_PRODUCTS_RANDOM'));
if ($rand == 1) {
$products = Product::getNewProducts(intval($cookie->id_lang), 0, $nb);
if ( $products )
{
shuffle($products);
array_slice($products, ($nb ? $nb : 10));
}
}
else
{
$products = Product::getNewProducts(intval($cookie->id_lang), NULL - 0, (intval($nb ? $nb : 4)), false, NULL, NULL);
}
$smarty->assign(array(
....
'products' => $products,
....
);
return $this->display(__FILE__, 'homenewproducts.tpl');
}
How can I override the class Product so that the method getNewProducts take into account excluding products out of stock?
Or at least, how can I remove from $products the products with quantity =0 using PHP?
Your help is appreciated.
Well, the solution I am using now is:
In product.php, I changed the sql queries in getNewProducts method inside of NewProductsController so that it takes into account if product is available in stock
I added AND 'quantity'!=0 in line 2062 and $sql->where('p.'quantity' != 0'); in line 2086 . Prestashop 1.6.0.6.
Of course, better override the classe Product.php than modifying it.
I hope it can help.