How can I show total price at checkout page [OpenCart] - php

I would like to make some calculations at the checkout page. If for example the total price of all products is 80€, I want to show a message that says: "You can add more products on cart worth of 20€, so you can get a discount which activates when total cost is 100€".
To do this I have to get the total price as a variable and do this:
$tp = totalprice;
if ( $tp < 100 ) {
$tp_less = 100 - $tp;
}
echo 'You can add more products on cart worth of ' . $tp_less . '€.';
The problem here is that I don't know how to get the total price as a variable.
<?php echo $cart_total = $this->cart->getTotal(); ?>
I placed the above at the file /checkout.tpl but I get an error so it's not working. Actually, whenever I use any code that has 'this' inside it I get error:
"Notice: Undefined property: Template\Basic::$cart"

$this->cart->getTotal() - Calculates cart total including taxes
$this->cart->getSubTotal() - This calculates products subtotal, I think you need to use this.
$this->cart->getSubTotal() - call this function in your controller and store value in a variable and pass it to your tpl file.
Regarding your error:
By default cart library is included in opencart, Make sure cart library is included, check registry file
Hope this helps.

All I had to do was:
At file checkout.php
$data['total'] = $this->cart->getTotal();
At file checkout.tpl
echo $total

Related

How to add Custom option value on coupon rules Magneto 2

How to add Custom option value on coupon rules Magneto 2. I have a product which have some custom option like size, color etc. with different value, I want give discount on basic of size = 7'. I don't want use sku custom option.. Let me know how I fix it.
https://github.com/Turiknox/magento2-custom-total
Moulde I use for Discount but i have face a problem.
one Data I can't get cart information
$obj = $bootstrap->getObjectManager();
// Set the state (not sure if this is neccessary)
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
// Getting the object managers dependencies
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote();
$helper = $obj->get('\Magento\Checkout\Helper\Cart');
// Get quote and cart items collection
$quote = $helper->getQuote();
$quoteitems = $quote->getAllItems();
$coupon_code = 'Test23';
// Get cart contents
$cart= $helper->getCart();
foreach ($quoteitems as $item)
{
}
It goes to the infinity loop.
This one Run fast that's why here we can't load quote total, For Discount given you must set session core cookies which allow giving discount...

Using wc_get_product() with a PHP variable for product ID

I'm building out custom landing pages for products in WooCommerce and I'd like to get the product price amongst other things in order to display them on the landing page.
Each landing page has some custom fields which allow the WP Admin to add in content, for the landing page as well as the product ID, which will then be used to generate the product price, checkout URL etc..
I can't get the wc_get_product(); to work with my custom field or a variable built off that. It only works when I use a straight ID. I think there's something I'm not understanding about how variables work within PHP. Here's my code.
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// This line is where the problem is...
$_product = wc_get_product('$courseID');
// If I replace the line above with this line
// $_product = wc_get_product('7217');
// everything works great, but that does not let
// each landing page function based on the custom fields where the user determines
// the product ID they are selling on that landing page.
// Get's the price of the product
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
Update
I get the following error using wc_get_product( $courseID ); or get_product( $courseID );:
Fatal error: Call to a member function get_regular_price() on a non-object in ...
Update related to your recent comment. The 2 ways to explore:
1) Instead of you should try to use to get the product object (avoiding the error):
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Get an instance of the product object
$_product = new WC_Product($courseID);
2) Alternatively if this doesn't work, you should try to use get_post_meta() function to get the product price (or any product meta data) this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Get the product price (from this course ID):
$course_price = get_post_meta($courseID, '_regular_price', true);
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This time you should get displayed the price with one or the other solutions.
Update: May be Also you need to convert $courseID to an integer variable.
Because you need to use your variable $courseID inside wc_get_product() (without the 2 ') function this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Here
$_product = wc_get_product( $courseID );
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This should work now.
You can try this out :
$courseID = the_field('course_id');
$product = get_product( $courseID );
I figured out the answer after running through the possible solution routes that #LoicTheAztec supplied in his response. None of these worked and so I assumed something else was up.
I use Advanced Custom Fields to add custom fields in the back end and I was using ACF's the_field() in order to create my variable. That is incorrect usage of that function as it's designed to display the field, (it's basically using php's echo). To work with these custom field's you need to use ACf's get_field() which is to use it to store a value, echo a value and interact with a value.
Once I switched to setting my $courseID to this..
$courseID = get_field('course_id');
Everything worked. My code worked, and all #LoicTheAztec's code approaches also worked.

Get price of an order, by manufacturer or brand on MAGENTO

I am developing a module to receive commissions from companies that are selling in my Magento store. It is already working, but need to add a new feature that I'm not getting.
I need to add an extra value in the commission, if the application of any company is greater than "X"
I did in a way that did not work:
foreach ($order->getAllVisibleItems() as $key => $item)
{
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$price = $item->getPrice();
$valor_comissao = (int)$product->getAttributeText('comissao_vendedor')/100;
$comissao_valor2= $price-($price*$valor_comissao);
$qty = $item->getQtyOrdered();
$valor_pedido= ($price*$qty);
if($valor_pedido > 150) {
$comissao_valor= ($comissao_valor2*$qty-17);
} else {
$comissao_valor=($comissao_valor2*$qty-2);
}
$comissoes[] = array (
'Razao' => 'Produto '.$item->getName().' id '.$item->getProductId().' x '.$item->getQtyToInvoice().' sku: '. $item->getSku(),
'Login' => $product->getAttributeText('login'),
'ValorFixo' => $comissao_valor
);
}
then only if the product go from $ 150.00 the value is added and if you have two selections from costing $ 100.00, the value is not added. I need the value to be added if the entire order of the brand exceeds $ 150.00.
The logic that I'm trying to follow is as follows:
Taking the total value of the order
Filter by tags, ie total value of the order by brand
If the product is a brand that exceeded "x" and she has not yet received the additional value, add shipping
I wonder if someone has gone through something similar and could help me.
Grateful.
Taking the total value of the order
You can retrieve the total of the order via:
$order->getGrandTotal()
Filter by tags, ie total value of the order by brand
I would recommend using Magento's resource model object and building a query on it.
This post shows how this can be done: https://stackoverflow.com/a/5139391/3784145
note
Mage::getModel('catalog/product')->getCollection()
can be used instead of
Mage::getResourceModel('sales/order_collection')
Explanation on the difference between Model and Resource:
https://magento.stackexchange.com/a/4579
If the product is a brand that exceeded "x" and she has not yet received the additional value, add shipping
I don't really know what you mean with: If the product is a brand that exceeded "x"
But I think this would require another collection query.

WooCommerce Wordpress can't find total order after checkout/pyament

I use a custom payment, which I display in an iframe and pass on variables into a form.
I need to pass the total order without currency sign after checkout/payment.
I've found a way to pass the cart total before checkout/payment using
echo WC()->cart->total
On the checkout after payment page (thankyou.php) I've found
echo $order->get_formatted_order_total();
This will display the total order with a currency sign only on the thankyou.php page and will not pass onto any other page no matter what i tried (which isn't important as I can work with ether thankyou.php page or actual checkout page)
Full code looks like this inside a list
<?php _e( 'Total:', 'woocommerce' ); ?>
<strong><?php echo $order->get_formatted_order_total(); ?>
I've tried testing to see how wordpress stores order total both on thankyou.php page and on a seperate page to see if I can call the order total from the array after checkout/payment but I ether get a value of 0.00 or my page breaks
Code I tried so far:
echo $order->get_order_total();
this breaks the page
echo WC()->$order->get_order_total();
this breaks the page
echo $order->get_total();
this also breaks the page
calling total cart echo WC()->cart->total after checkout/payment will display 0 (which makes sense)
If it's the thank you page, then it's simple enough - you just need to use this and it should work without breaking the page.
In your templates/checkout/thankyou.php file, add this code:
if ( ! $order) {
echo 'No order object!';
} else if ($order->has_status( 'failed' ) {
echo 'Order failed for some reason.';
} else {
echo $order->get_total();
}
If you don't have the $order object, you can get it from the order id:
$order = wc_get_order( $order_id );
$order->get_total();
If you don't have the order id OR the $order object available, then we need more information to be able to help.

Calculating a percentage of the variable price in PHP Opencart

I have a pricing matrix where a shopper can choose to pay of the cost over a period. I need to display this per month price under the full price in Opencart. I have managed to get a formula to half work, but as soon as I use a variable in the calculation it does not work.
I have this calculation, which works correctly when using actual numbers.
9.4 is the percentage of the price(1799)
<?php
$mobi_price = (9.4 / 100) * 1799;
echo $mobi_price;
?>
The result is 169.10
What I need is for the price to be a variable i.e. not 1799 but $price, so that ir differs for each product.
However when I use the function like:
<?php
$mobi_price = (9.4 / 100) * $price;
echo $mobi_price;
?>
The result is 0 and should be 169.10.
Any Ideas?

Categories