At the moment i am trying to get the product price including tax in a php file for my product feed. I have this code at the moment:
$_product = Mage::getModel('catalog/product')->load($productId);
$_priceIncludingTax = $this->helper('tax')
->getPrice($_product, $_product->getFinalPrice());
Problem is that since that of course the '$this->' part doesn't work so well from the file.
Anyone know how i can still get the price including tax in this file?
You can get a helper-instance in any file using:
Mage::helper('tax')
Your full code is:
$_product = Mage::getModel('catalog/product')->load($productId);
$_priceIncludingTax = Mage::helper('tax')
->getPrice($_product, $_product->getFinalPrice());
Thanks #Alex:
If the product has FinalPrice special price is the final price of the product to access the most serious tax base price:
$_product = Mage::getModel('catalog/product')->load($p->getId());
$_specialPriceIncTax = Mage::helper('tax')
->getPrice($_product, $_product->getFinalPrice());
$_priceTax = Mage::helper('tax')
->getPrice($_product, $_product->getPrice());
Related
I created a custom product type for WooCommerce. With this product type it is possible to connect an other product that is exist by ID.
WooCommerce reduce the stock quantity automatically if an order is placed and the payment is successful.
For example I added a product with ID 4082 to the cart with a quantity from 3.
After place this order WooCommerce updated the stock from product 4082 with -3.
Ok back to my custom product type. As I said it is possible to connect another product by ID.
For example I connect product 4082 with product ID 10988.
If a customer add product 4082 to the cart and placed the order I want reduce the stock quantity from product ID 10988 and not from 4082.
<?php
add_action('woocommerce_checkout_order_processed', 'stocktest');
function stocktest($order_id){
$order = wc_get_order( $order_id );
$order_item = $order->get_items();
foreach( $order_item as $product ) {
//for the topic I programmed the IDs hardcoded
if($product->ID == '4082'){
wc_update_product_stock( 10998, $product['qty'], 'decrease', '' );
}
}
}
?>
I tried the code above and the stock from ID 10998 is correctly decreased but also the stock from ID 4082 is decreased.
Do I use the wrong hook? And how can I make the function correctly?
Hope somebody can help me with this.
Thanks a lot
Does wc_update_product_stock( 4082, $product['qty'], 'increase', '' ); not work?
That way, the stock should be adjusted for the 4082 product.
Also, there might be a potential issue with your snippet. The $product variable refers to the product object of 4082 product, not the 10998 product.
Maybe try something like this?
<?php
add_action('woocommerce_checkout_order_processed', 'stocktest');
function stocktest($order_id){
$order = wc_get_order( $order_id );
$order_item = $order->get_items();
foreach( $order_item as $product ) {
//for the topic I programmed the IDs hardcoded
if($product->ID == '4082'){
$connected_qty = get_post_meta( 10988, '_stock', true );
wc_update_product_stock( 10998, $connected_qty, 'decrease', '' );
wc_update_product_stock( 4082, $product['qty'], 'increase', '' );
}
}
}
?>
Instead of using the custom field query, you could use the WC_Product class:
$connected_product = wc_get_product(10998);
$connected_qty = $connected_product->get_stock_quantity();
I did some research on your question. WooCommerce called a lot of functions to update the stock af order / payment. If an order is placed the items become an status reduce_stock yes or no.
If is yes go further and update the stock of this item. After this WooCommerce created the e-mails and ordernotes.
All this functions worked together please be careful with changing the code in the file wc-stock-functions.php
If you want change and try something do this for example on a local testserver.
This are some functions in the file
<?php
wc_maybe_reduce_stock_levels();
wc_maybe_increase_stock_levels();
wc_reduce_stock_levels();
wc_trigger_stock_change_notifications();
?>
I'm working with a custom product where customers can type their custom text that will be added to cart.
Depending on the size of the text, a different price will be set, the logic is done and I can see the new price in the product object.
It seems that I can change the product object with the new price but I get status 500 back from admin-ajax.php when I do $woocommerce->setup_product_data($product_id).
I have found several topics but none of them seems to work in my case.
I'm not able to update the cart with the new price.
Here is my ajax function in functions.php:
// Adjust new price
function applyNewPrice() {
global $woocommerce;
// From JS
$product_id = (int) $_POST['id'];
// From JS
$price = (float) $_POST['price'];
$product_data = get_post($product_id);
// Code returning status 500 here...
$product = $woocommerce->setup_product_data($product_data);
$product->set_price($price);
update_post_meta($product_id,'_price',$price);
update_post_meta($product_id,'_regular_price',$price);
$woocommerce->clear_product_transients( $product_id );
}
add_action('wp_ajax_applyNewPrice', 'applyNewPrice');
add_action('wp_ajax_nopriv_applyNewPrice', 'applyNewPrice');
The function you are trying to call does not exist. This is the function you need.
$product = wc_setup_product_data( $product_id );
No sure where you are coming up with those functions. I could not find this function either?
$woocommerce->clear_product_transients( $product_id );
I am not expert on WooCommerce, i cant find how to display price that calculate on it discount from coupon.
This code, display price after coupon, but also with shipping price - what i dont want, i want to display price after discount, before shipping and tax:
$woocommerce->cart->get_cart_total();
This code display the finnal price without discount and without shipping:
$woocommerce->cart->get_cart_subtotal();
So i tried something like that to get price after discount:
<?php
$totalp = $woocommerce->cart->get_cart_subtotal();
$totaldisc = $woocommerce->cart->get_total_discount();
$resultp = $totalp - $totaldisc;
echo ($resultp);
?>
But it show "0" number.
So, i guess there is simple function for what i want (i want to get total cart price after discount, before shipping), but i cant find this function.
Answer: i was need to use int's or floats without symbols, and the best way was to check WooCommerce docs here:
Woo - Docs , and to search for this variable and use it without () (not function). now it work :)
<?php
$first_number = $woocommerce->cart->subtotal;
$second_number = $woocommerce->cart->discount_total;
$sum_total = $first_number - $second_number;
print ($sum_total);
?>
Since you have the currency in the prices, you'll need to remove them.
I couldn't find WooCommerce code to just get the subtotal & discount without currency symbol, so let's just remove them manually:
function remove_currency($price) {
return (double) preg_replace('/[^0-9\.]+/', '', $price);
}
$totalp = remove_currency($woocommerce->cart->get_cart_subtotal());
$totaldisc = remove_currency($woocommerce->cart->get_total_discount());
$resultp = $totalp - $totaldisc;
echo $resultp;
This post is a bit old but I would like to inform anyone viewing that Woocommerce 3.x now uses:
WC()->cart
or you can use just
$cart
as long as you have the name_of_my_function( $cart ){} parameter callable.
Everything above is fine... I'm sure yet in hopes someone is not aware that $woocommerce global is no long in use.
There is also a way to loop foreach through the cart items (objects) to pick your price sub-totals using the cart_object.
foreach( $cart_obj->get_cart() as $key=>$value )
{
$priced = $value['subtotal'];
$new_price = $priced - ($predefined_referral_discount * 1);
$value['data']->set_price( $new_price );
}
Remember: name_of_my_function( $cart_obj ){} for this one.
Happy Coding
I'm running into a problem with getting attribute values for products that have been placed in a users cart.
I have the following code:
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
foreach ($session->getQuote()->getAllItems() as $item) {
$_product = Mage::getModel('catalog/product')->load($item->getId());
$attributeValue = $_product->getAttributeText('availability');
echo $attributeValue;
}
And it works fine for all products in the cart except the very last one. For example, I'm trying to get the value of an "availability" attribute I've created that can have only 1 of the following values "Backorder", "Preorder", "Out of Stock". If I have 3 items in my cart I can get the correct values for the first 2, however for the last item it just displays "No".
I have double checked each item to make sure all attributes are set correctly and it happens with any number of items in the cart.
Hopefully it's just a stupid mistake on my part.
Any help would be appreciated.
Thanks.
SOLVED:
Changing:
$_product = Mage::getModel('catalog/product')->load($item->getId());
to
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
Fixes the issue.
The problem is in that line:
$_product = Mage::getModel('catalog/product')->load($item->getId());
With $item->getId() you are getting ID of Mage_Sales_Model_Quote_Item and not Mage_Catalog_Model_Product. You have to use $item->getProductId() instead to get ID of actual product associated with current quote item.
I'm setting up a tracking code for an affiliate programs. Now we give different commissions. One for Food related products and one for Non-Food related products. These are also the tax classes we have (Food, Non Food).
I need to display the sub total for food products and a subtotal for non food products.
I use the following code but that doesn't work:
<?php
//Get Order Number & Order Total
$order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$grandamount = number_format($order->getGrandTotal(),2);
$coupon = $order->getCouponCode();
$amountfood = number_format($order->getSubtotal('Food'), 2);
$amountnonfood = number_format($order->getSubtotal('Non_Food'), 2);
$discount = number_format(0.00 - $order->getDiscountAmount(), 2);
?>
If I use $amountfood = number_format($order->getSubtotal(), 2); it does work for the subtotal including both the food and non food values.
Could someone please help me with that.
Thanks,
Daniel
I don't think this information is available directly: magento stores the subtotal and taxes in a global way, only the total, no detailed information.
What you could do, is fetch the ordered products, for each one get his tax class and store in an array the sale value.
Something like this:
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$subtotals = array();
foreach ($items as $_item) {
if (array_key_exists($subtotals[$_item->getTaxClassId()])) {
$subtotals[$_item->getTaxClassId()] += $_item->getRowTotal();
} else {
$subtotals[$_item->getTaxClassId()] = $_item->getRowTotal();
}
}
not sure if the "if" is needed though.
hope that helps