繁体   English   中英

获取 WooCommerce 订单项中的订单项 ID

[英]Get the order item Id in WooCommerce order items

使用 WooCommerce,我尝试更改订单项的下载 URL。 最初我使用的是订单 ID,但每个订单只允许一个订阅。 所以我需要客户能够购买多个订阅。

这是我目前拥有的代码:

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();

}

一切正常,除了我无法获得订单项目 ID。

任何帮助,将不胜感激。

要从 WC_Order_Item 对象中检索订单商品 ID,请使用 get_id() 方法。

所以在你的脚本中,你应该替换这个部分:

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

有了这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM