0

With WooCommerce, I am trying to change the download URLs for order items. Originally I was using the order ID but that only allows one subscription per order. So I need the customer to be able to purchase more than one subscription.

Here is the code I currently have:

if ( !class_exists( 'pl_wcdf_extention' ) ) {

    class pl_wcdf_extention {

        public function __construct() {

            add_filter( 'woocommerce_order_get_downloadable_items', array( $this, 'download_link' ), 10, 2 );

            add_filter( 'woocommerce_customer_get_downloadable_products', array( $this, 'download_link' ), 10, 1 );
        }

        public function download_link( $downloads, $order = null ) {

            $download_url_base = 'https://xxxx.com/';

            // retrieve Order ID from Download array

            $order_id = $downloads [0]['order_id'];

            // retrieve Order data

            $order = wc_get_order( $order_id );

            // retrieve Order Items 

            $order_item = $order->get_items();

            // retrieve first Order Item ID

            $order_item_id = $order_item [0][order_item_id];

            foreach ( $downloads as $download ) {

                $download['download_url'] = $download_url_base . "xxxx-" . $order_item_id . ".svg";

                $tag_item = array(
                    '--native-libs-dir' => '/home/Lib/',
                    '--type' => 'template0001style1',
                    '--data'=> $order_item_id,
                    '--join' => 'horizontal',
                    '--output-file' => '/home/public_html/xxxx-' . $order_item_id . '.svg'
                );

                $args = "";

                foreach ($tag_item as $k=>$v) {
                    $args .= " $k " . " $v ";
                }

                shell_exec("/home/Python-2.7.14/Python-3.6.3/python /home/Lib/generate-code.py $args");
            }

            return $downloads;
        }
    }
}

if ( class_exists( 'pl_wcdf_extention' ) ) {

    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }

    global $wpdb;

    define( 'PL_WCDFE_DIR_PATH', plugin_dir_path( __FILE__ ) );
    define( 'PL_WCDFE_PLUGIN_FILE', __FILE__ );

    new pl_wcdf_extention();

}

Everything works except I am not able to get the order item ID.

Any help would be appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
ggntc
  • 57
  • 1
  • 9

1 Answers1

1

To retrieve order item id from the WC_Order_Item Object, please use the get_id() method.

So in your script, you should replace this section:

// retrieve first Order Item ID
$order_item_id = $order_item [0][order_item_id];

with this:

// retrieve first Order Item ID $order_item_id = $order_item[0]->get_id();

Emamuzo
  • 11
  • 2