Dear I've a Pizza site and I'm using Woocommerce plugin. I want to apply some price check on attributes.
My Question is
I've a Pizza product which attributes are Size and Meat. Size(Small,large) Meat(Chicken, donair meat etc).
I've applied a check on attributes that when a customer select a product size, small and one piece of meat, the price will be $10 on default rate. But I want to apply this logic here that, when a customer select a more than one piece of meat (Extra topping), Then I want to add $2 on total price like ($10 + $2 = 12).
please help me??
Please use this action for adding the extra price on woocommerce cart.Now you need to work on the commented loop code and apply the simple login.Now I have added a summary for how you will work.
I have use it and now it is working on my local system.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$productid = $value['product_id'];
$variationArr =$value['variation']; //it is an array have the value : meat and size i.e chicken and small
//In every loop you can count the number of products .
//Note: In array($variationArr),It have all the active attribute of this product which is created by admin in woocommece.
//so you can search it by loop;
/*
* Like
* foreach($variationArr as $k=>$v)
* {
* //here is the variation product size
* }
*
*/
//Now increase the price.
$price = $value['data']->price;
$extraPrice = 2;
$newPrice = $price + $extraPrice;
$value['data']->price = $newPrice;
}
}
Related
How I can get variation parent product ID.
EXAMPLE:
I have product with ID 35
and this product has two variations colors - red (ID 351), black (ID 352)
My code:
$product = wc_get_product(get_the_ID()); //get_the_ID() is ID 351 and I need this parent ID 35
The proper way
As LoicTheAztec suggested in comments, you should use this:
$parent_product = wc_get_product($product->get_parent_id());
The reason why you should retrieve parent product via get_parent_id() is that it will trigger hook woocommerce_product_variation_get_parent_id and it will be easily modifiable by other plugins/themes:
add_filter('woocommerce_product_variation_get_parent_id', function($value, $wc_data) {
// ...
return $value;
}, 10, 2);
This will also work, but it won't trigger WC-specific hooks:
$parent_product_id = wp_get_post_parent_id($product->get_id());
$parent_product = wc_get_product($parent_product_id);
Old answer
Note: That's not working outside the loop and will always return 0 if you attempt to substitute get_the_ID() with $product->id - in that case use $product->get_id() as in the example above.
Use wp_get_post_parent_id, as variations have their parent as the product itself.
Example:
$variation_id = get_the_ID();
$product_id = wp_get_post_parent_id($variation_id);
Never use WC_Product::get_parent():
$parent_product = $product->get_parent(); // will always return '0
I am trying to figure out how to control the discount behavior of my website so it collaborates with an external distribution channel. Right now i have a successful mail send with a XML-file attached when the customer has finished paying.
The only data field that is causing trouble is the discount. Because:
The disctribution channel only accepts discount for each item (product) as percentage that is a whole number
I've used simpleXML and added data for each item (sold product) like this - only a section of the full code
// Basic $order data and wp_mail configurations BEFORE
foreach ($order->get_items() as $item) {
$product = $item->get_product();
$isbn = get_field( 'isbn', $item->get_product_id() );
$price = $product->get_price();
$quantity = $item->get_quantity();
$order_line = $order_head->addChild( 'Ordrelinje' );
$order_line->addAttribute( 'besked', 'notat tilknyttet ordrelinjen' );
$order_line->addAttribute( 'rabatprocent', '' );
$order_line->addAttribute( 'stykpris', $price );
$order_line->addAttribute( 'antal', $quantity );
$order_line->addAttribute( 'isbn', $isbn );
}
// SimpleXML save and attachment created with the XML file AFTER
// Sent with wp_mail and the return tested AFTER
So the problem is basically:
I know how to calculate the percentage from full price and discount in the product[] data, but i have difficulties imagine a flexible system with different discount-possibilities (see bottom)
With both percentage- and amount discount you could imagine having a conflict with whole based number when calculating the percentage after purchase. Some decimals would be lost.
Example:
Let's say that i make a discount on one book at 22 KR (danish) and the price without discount were: 143.
Then the discount in percentage would be: 100/143*22: 15.38%.
I would then for example, to get a whole number as percentage, round with round(15.38, 0) to get 15.
And then the customer would lose: .38% that is 143*0.0038 = 0.54KR
So my question is:
Does anyone has an idea how to get solve this, or a workaround so the amount discount is a whole number of the regular price in percentage for example. Or have other ideas?
I'm trying to add to an existing fee, but when I use:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
$cartfee = $woocommerce->cart->get_fees();
$stripefee = 0.015;
$perorder = 0.20;
$surcharge1 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $stripefee;
$bookingfee = $surcharge1 + $perorder + $cartfee;
$woocommerce->cart->add_fee( 'Booking Fee', $bookingfee, true, '' );
}
There is already a Booking Fee of 0.30 that displays fine on the cart, when I add the above it does the $surcharge1 + $perorder but the $cartfee is empty. It doesn't seem to read the existing fee. The Booking fee is from a plugin that I can set a % per product.
Any help to point me in the right direction would be great.
What's kind of value $woocommerce->cart->get_fees() this one returns (your $cartfee)? Seems like it should be an array. Did you check for an empty array?
Have situation with custom price for item line in WooC.
Now find solution to change subtotal in Edit of order this way:
add_action( 'woocommerce_update_order_item', function ($_item_id, $_item, $_order_id) {
if( $arr_item_meta = wc_get_order_item_meta($_item_id) )
{
$prc = wooc_item1_price( $arr_item_meta["_a1"][0], $arr_item_meta["_a2"][0], $arr_item_meta["_a3"][0], $_item->get_product()->get_id() );
$_item -> set_total( $prc );
$_item -> set_subtotal( $prc );
$_item -> save();
}
},10,3);
This only works on edit. I.e. while updated - it set up price i make with function
wooc_item1_price with few argumens from cart (quantities of each type of product)
Problem is:
howto make same after product just added to cart.
So new subtotal+total will be available in order before any edit/updater.
Please help me!
add_action ('woocommerce_calculate_totals', function( $_cart )
{
foreach ($_cart->cart_contents as $cart_key => $cart_item)
{
$prc = function_to_make_item_custom_price(
$_cart->cart_contents[$cart_key]["_param1"],
$_cart->cart_contents[$cart_key]["_param2"],
$_cart->cart_contents[$cart_key]["_param3"],
$_cart->cart_contents[$cart_key]["product_id"]
);
$_cart->cart_contents[$cart_key]['line_subtotal'] = $prc;
$_cart->cart_contents[$cart_key]['line_total'] = $prc;
}
}, 10,1);
this is right way - make price on adding item to cart.
is need have price function function_to_make_item_custom_price to make custom price, based on parameters. In my case - this is quantites of product parts (tour adult&child&infants param1,param2,param3)
Its simplifed idea, is need also recal taxes, coupons ...
I have created a link in the header of my site to the cart with the current totals.
It is supposed to show the current total of the cart and the number of items contained in the cart, in this manner :
[icon] 1.20 € - 1 article
However, the value is only correctly calculated when in the cart page. On the homepage, for example, it will only display like this :
[icon] 0.00 € - 1 article
This is how I am accessing the values :
$count = WC()->cart->cart_contents_count;
WC()->cart->calculate_totals();
if($count > 0)
{
print "<a class='cart-contents' href='" . WC()->cart->get_cart_url();
print "' title='Voir votre panier'>";
print WC()->cart->get_total();
print " - " . sprintf(_n('%d article', '%d articles', $count, 'woothemes'), $count);
print "</a>";
}
If I call WC()->cart->get_cart_total();, this will show the price exclusive of tax, even if I force prices include tax.
How can I get the correct value consistently across the site ?
global $woocommerce;
$amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total;
You can also convert $amount in float value as per your requirement.
I had the same issue so I went through the WooCommerce code. In the function WC()->cart->calculate_totals() in the class WC_Cart since some time ago there is a following condition
// Only calculate the grand total + shipping if on the cart/checkout
if ( is_checkout() || is_cart() ||
defined('WOOCOMMERCE_CHECKOUT') || defined('WOOCOMMERCE_CART') )
This condition applies to calculating specifically the total, I'm not sure why. But this gives you the proper workaround:
On the page where you need to show the total include either one of the following lines of code:
define( 'WOOCOMMERCE_CART', true );
or
define( 'WOOCOMMERCE_CHECKOUT', true );
And then perform the function WC()->cart->calculate_totals();
Then to get the cart total use either WC()->cart->total or wc_cart_totals_order_total_html() (for currency symbol)