• Resolved loopforever

    (@loopforever)


    Hello,
    I want to get the discounted price and the non-discounted price after ordering. However, I couldn’t find a solution.

    For example, for product A;

    Regular Price: 100 (Tax Included)
    Sales Price: 80 (Tax Included)

    After Order:

    $order = wc_get_order( $order_id );
    foreach ($order->get_items() as $item_id => $item) {
      $regular_price = $order->get_item_subtotal( $item, false, true );
      $sale_price = $order->get_item_total( $item, false, true );
    }

    However, in such a case, both variables are equal. So $regular_price and $sale_price variables. There is a discount here, why are these variables equal?
    I’ve looked through many resources but couldn’t find what I was looking for. Could you help ?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello,

    I understand the variables are equal.

    I’ve found this page that might help you out:
    get_item_subtotal Examples

    Let us know if this worked for you.

    Thread Starter loopforever

    (@loopforever)

    Thank you for your answer. I looked at the samples in the link you sent. However, I couldn’t find what I was looking for.
    I hope you understand what I mean. I’m wondering if there is a function that does this. Otherwise unfortunately I’ll have to deal with it myself, work on it and write a new function.

    There is a product named A.
    Regular Price: $100 dollars
    Sale Price: $50

    The customer adds the product to the cart and successfully purchases the product.
    This is where I start everything. I want to get regular price and sale price “on order”. So, I’m looking for questions like how much did the customer buy this product and bought it at a discount.
    Yes, you can have this product-based information:

    foreach ($order->get_items() as $item_id => $item ) {
        $regular_price = $product->get_sale_price(); // The product raw sale price
    
        $sale_price = $product->get_regular_price(); // The product raw regular price
    }

    But of course this is not true. Because, this price may change later. For example, the discounted price can be removed. That is, only regular price ($100) can be sold. Or a price of more: $110.

    Mirko P.

    (@rainfallnixfig)

    Hi there,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook Community group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers.

    Thread Starter loopforever

    (@loopforever)

    This is not a complex development issue. Unfortunately, you are copy-pasting canned answers. Previously, Woocommerce support was not like this.
    I’m not asking you to write code. Such a function, usage etc. I’m asking whether there is or not.

    I also wrote what you call complex. Maybe it helps someone else. I am sharing it below.
    Have a nice day.

    /**
    * If there is a discounted item, save it in the woocommerce_order_itemmeta table for further use.
    * 
    * @return void
    */
    add_action( 'woocommerce_payment_complete', 'order_check_discounted_price_control' );
    function order_check_discounted_price_control( $order_id ){ 
    	//create order object
    	$order = wc_get_order($order_id); 
    	foreach ($order->get_items() as $item_id => $item ) { 
    		$product = $item->get_product();
    	//Check if the item is discounted
    	if($product->get_sale_price() != $product->get_regular_price()){
    		
    		wc_add_order_item_meta($item_id,'_discounted_item', $product->get_regular_price());
    	}
    	
    }
    Mirko P.

    (@rainfallnixfig)

    Hi @loopforever,

    Just so you know, the support staff here are not Woo developers and we help understand and troubleshoot WooCommerce’s built-in core features.

    That said, here’s the list of functions in WooCommerce. You may take a look and find the one that helps achieve your goal.

    https://woocommerce.github.io/code-reference/packages/WooCommerce-Functions.html

    You might also be interested in reading the support policy of this forum:

    https://woocommerce.com/support-policy/#customization

    As mentioned above, I’ll leave this thread open in case anyone else wants to chime in.

    Best regards.

    Mirko P.

    (@rainfallnixfig)

    Hi there,

    We’ve not seen any activity on this thread for a while, so I’m marking it as resolved.

    Hopefully, you were able to find a solution and the above resources for developers were helpful. If you have further questions, please feel free to open a new topic.

    Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get discount information after order’ is closed to new replies.