I am trying to read from thankyou.php orders and populate order_id, sku, price, quantity to a javascript snipper. The problem for me is how to "catch" the loop in php and pass those variables to JavaScript Snipper.
<?php
function CustomReadOrder ( $order_id ) {
// Lets grab the order
$order = wc_get_order( $order_id );
// This is the order total
$order->get_total();
// This is how to grab line items from the order
$line_items = $order->get_items();
// This loops over line items
foreach ( $line_items as $item ) {
// This will be a product
$product = $order->get_product_from_item( $item );
// This is the products SKU
$sku = $product->get_sku();
// This is the qty purchased
$qty = $item['qty'];
// Line item total cost including taxes and rounded
$total = $order->get_line_total( $item, true, true );
// Line item subtotal (before discounts)
$subtotal = $order->get_line_subtotal( $item, true, true );
echo $order_id."-";
echo $sku."-";
echo $qty;
}
}
?>
<?php add_action( 'woocommerce_thankyou', 'CustomReadOrder' ); ?>
<script type="text/javascript">
order.purchase = {
currency: 'EUR',
transactionId: '<?php echo $order->id ?>',
products: [{
id: '{{PRODUCT_SKU}}',
category: '{{PRODUCT_CATEGORY}}',
brand: '{{PRODUCT_BRAND}}',
price: '{{PRODUCT_PRICE}}',
quantity: '{{PRODUCT_QUANTITY}}'
}, {
id: '{{PRODUCT_SKU}}',
category: '{{PRODUCT_CATEGORY}}',
brand: '{{PRODUCT_BRAND}}',
price: '{{PRODUCT_PRICE}}',
quantity: '{{PRODUCT_QUANTITY}}'
}]
};
</script>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…