sali1101
4/24/2020 - 9:34 PM

Cancelled reduce total sold

http://hookr.io/actions/woocommerce_cancelled_order/%20%20%20

https://stackoverflow.com/questions/47803559/woocommerce-hook-woocommerce-cancelled-order


add_action( 'woocommerce_order_status_cancelled', 'change_status_to_refund', 10, 1 );

function change_status_to_refund( $order_id ){
	
	 if ( $order_items ) {
       foreach( $order_items as $item_id => $item ) {
        $refund_amount += $item->get_total();
       }
     }
	
    
}
Query woo orders on page 


// Loop through each WC_Order object - works with shortcode 
// from  https://stackoverflow.com/questions/51947198/how-to-query-woocommerce-orders-on-a-page

foreach( $orders as $order ){
    echo $order->get_id() . '<br>'; // The order ID
    echo $order->get_status() . '<br>'; // The order status
}


//https://pluginrepublic.com/querying-woocommerce-orders/
add_filter('manage_edit-shop_order_columns', 'misha_order_items_column' );
function misha_order_items_column( $order_columns ) {
    $order_columns['order_products'] = "Purchased products";
    return $order_columns;
}
 
add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
function misha_order_items_column_cnt( $colname ) {
	global $the_order; // the global order object
 
 	if( $colname == 'order_products' ) {
 
		// get items from the order global object
		$order_items = $the_order->get_items();
 
		if ( !is_wp_error( $order_items ) ) {
			foreach( $order_items as $order_item ) {
 
 				echo $order_item['quantity'] .'<img src="'.get_the_post_thumbnail_url($order_item['product_id'], 'thumbnail'). '"> <br/>';
				// you can also use $order_item->variation_id parameter
				// by the way, $order_item['name'] will display variation name too
  
			}
		}
 
	}
 
}
REDUCE TOTAL SOLD IF CALCELLED

	add_action( 'woocommerce_order_status_cancelled', 'update_total_sold', 10, 1 );

	
	
	function update_total_sold( $order_id ){
		
	$order = new WC_Order( $order_id );  

    $order_items   = $order->get_items();

     // Refund Amount
       $refund_amount = 0;
     // Prepare line items which we are refunding
     $line_items = array();

     if ( $order_items ) {
       foreach( $order_items as $item_id => $item ) {
		  $product_id = $item->get_product_id();
       // $refund_amount += $item->get_total();
		update_post_meta($product_id, $key = 'total_sales', $value = '3300' );
       }
     }
		
		
		
		
		
		
		
	
	
	}
/**
 * @snippet       Display Cart @ Checkout Page Only - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'woocommerce_before_checkout_form', 'bbloomer_cart_on_checkout_page_only', 5 );
 
function bbloomer_cart_on_checkout_page_only() {
 
if ( is_wc_endpoint_url( 'order-received' ) ) return;
 
echo do_shortcode('[woocommerce_cart]');
 
}
/*
 * Create order dynamically */

//add_action( 'woocommerce_before_checkout_form', 'create_order' );
add_shortcode('corder', 'create_order');
function create_order() {
	 $id = intval($_GET['id']);
	
	
	
	
	global $woocommerce;
	
	$address = array(
		'first_name' => $id ,
		'last_name'  => 'Corson',
		'company'    => 'Automattic',
		'email'      => 'no@spam.com',
		'phone'      => '123-123-123',
		'address_1'  => '123 Main Woo st.',
		'address_2'  => '100',
		'city'       => 'San Francisco',
		'state'      => 'Ca',
		'postcode'   => '92121',
		'country'    => 'US'
	);
	
	// Now we create the order
	$order = wc_create_order();
	
	// The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
	$order->add_product( get_product( 368 ), 5); // Use the product IDs to add
	
	// Set addresses
	$order->set_address( $address, 'billing' );
	$order->set_address( $address, 'shipping' );
	
	// Set payment gateway
	$payment_gateways = WC()->payment_gateways->payment_gateways();
	$order->set_payment_method( $payment_gateways['bacs'] );
	
	// Calculate totals
	$order->calculate_totals();
	$order->update_status( 'Completed', 'Order created dynamically - ', TRUE); 
}