ole1986 / wc-invoice-pdf

WC Recurring Invoice Pdf plugin for wordpress
https://wordpress.org/plugins/wc-invoice-pdf
6 stars 3 forks source link

Subscribe to orders #10

Closed ole1986 closed 4 years ago

ole1986 commented 4 years ago

This PR has its primary focus on moving the subscription concept from product to orders.

ole1986 commented 4 years ago

Some useful snipped

<?php

// show the custom meta data on the cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'tp_get_custom_item_data', 10, 2 );
function tp_get_custom_item_data( $item_data, $cart_item ) {
  if( isset( $cart_item['size'] ) ) {  
    $item_data[] = array( 'name' => 'Size', 'value' => $cart_item['size'] );
  }
  return $item_data;
}

// add custom meta to order
add_action( 'woocommerce_new_order_item', 'tp_add_custom_meta_to_order', 99, 3 );
function tp_add_custom_meta_to_order( $item_id, $item, $order_id ) {
  if( ! isset( $item->legacy_values ) ) { // applies to order_item_shipping
    return;
  }
  if( isset( $item->legacy_values['size'] ) ) {
    wc_add_order_item_meta( $item_id, 'Size', $item->legacy_values['size'] );
  }
}
ole1986 commented 4 years ago

A good place to add subscribtion section would be in action hook woocommerce_cart_contents

// add the action 
add_action( 'woocommerce_cart_actions', 'action_woocommerce_cart_actions, 10, 0 );

Followed by hook woocommerce_update_cart_action_cart_updated to save its post data

add_action( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );
function on_action_cart_updated( $cart_updated ){
    // save some data
    WC()->session->set( 'foo', 'some-data' );
    // after checkout proceeded, get the data to store into WC_Order meta data
    ///WC()->session->get( 'foo' );
}

See: https://stackoverflow.com/a/49496196/2106997