Created
January 26, 2018 17:31
-
-
Save cmunns/87563cdb800dfd2bfe919ad14947cdd5 to your computer and use it in GitHub Desktop.
WooCommerce Bronto Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('wp_footer', 'add_bronto_json'); | |
function add_bronto_json() { | |
$brontoItems = []; | |
if( is_wc_endpoint_url( 'order-received' ) ): | |
if(isset($_GET['view-order'])) { | |
$order_id = $_GET['view-order']; | |
} | |
//check if on view order-received page and get parameter is available | |
else if(isset($_GET['order-received'])) { | |
$order_id = $_GET['order-received']; | |
} | |
//no more get parameters in the url | |
else { | |
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; | |
$template_name = strpos($url,'/order-received/') === false ? '/view-order/' : '/order-received/'; | |
if (strpos($url,$template_name) !== false) { | |
$start = strpos($url,$template_name); | |
$first_part = substr($url, $start+strlen($template_name)); | |
$order_id = substr($first_part, 0, strpos($first_part, '/')); | |
} | |
} | |
//yes, I can retrieve the order via the order id | |
$order = new WC_Order($order_id); | |
$order_item = $order->get_items(); | |
foreach( $order_item as $item ) { | |
$product_name = $item['name']; | |
$product_id = ($item['variation_id']) ? $item['variation_id'] : $item['product_id']; | |
$product_qty = $item['qty']; | |
$product = wc_get_product($product_id); | |
$brontoItems[] = array( | |
'sku' => $product->get_sku(), | |
'name' => $product->get_name(), | |
'description' => $product->get_description(), | |
'category' => $product->get_category_ids(), | |
'unitPrice' => $product->get_regular_price(), | |
'salePrice' => $product->get_sale_price(), | |
'quantity' => $item['quantity'], | |
'totalPrice' => $product->get_price()*$item['quantity'], | |
'imageUrl' => wp_get_attachment_image_src($product->get_image_id()), | |
'productUrl' => $product->get_permalink() | |
); | |
} | |
?> | |
<script type="text/javascript"> | |
brontoCart = { | |
"currency": "USD", | |
"subtotal": <?php echo wc_format_decimal( $_cart->subtotal , 2 )?>, | |
"discountAmount": <?php echo $_cart->get_cart_discount_total()?>, | |
"taxAmount": <?php echo wc_format_decimal( $_cart->get_taxes_total(), 2 )?>, | |
"cartUrl": "<?php echo wc_get_cart_url() ?>", | |
"lineItems": <?php echo json_encode($brontoItems) ?> | |
} | |
</script> | |
<?php | |
else: | |
$_cart = WC()->cart; | |
foreach( $_cart->get_cart() as $item ) { | |
$product_name = $item['name']; | |
$product_id = ($item['variation_id']) ? $item['variation_id'] : $item['product_id']; | |
$product_qty = $item['qty']; | |
$product = wc_get_product($product_id); | |
$brontoItems[] = array( | |
'sku' => $product->get_sku(), | |
'name' => $product->get_name(), | |
'description' => $product->get_description(), | |
'category' => $product->get_category_ids(), | |
'unitPrice' => $product->get_regular_price(), | |
'salePrice' => $product->get_sale_price(), | |
'quantity' => $item['quantity'], | |
'totalPrice' => $product->get_price()*$item['quantity'], | |
'imageUrl' => wp_get_attachment_image_src($product->get_image_id()), | |
'productUrl' => $product->get_permalink() | |
); | |
} | |
?> | |
<script type="text/javascript"> | |
brontoCart = { | |
"currency": "USD", | |
"subtotal": <?php echo wc_format_decimal( $_cart->subtotal , 2 )?>, | |
"discountAmount": <?php echo $_cart->get_cart_discount_total()?>, | |
"taxAmount": <?php echo wc_format_decimal( $_cart->get_taxes_total(), 2 )?>, | |
"cartUrl": "<?php echo wc_get_cart_url() ?>", | |
"lineItems": <?php echo json_encode($brontoItems) ?> | |
} | |
</script> | |
<?php | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment