I'm trying to return product weights so they display on the cart for my customers.
In the cart template, I'm calling this external JS file from the cart template in BigCommerce
storefront: https://store-xxx.mybigcommerce.com/cart.php
or backend template: cart.html
$(document).ready(function () {
var url = 'http://xxxx.com/cartProduct.php';
var prodWeights;
$(".prod_SKU").each(function () {
$.ajax({
url: url,
method: 'POST',
dataType: "json",
data: {sku: $(this).text()},
contentType: "application/json; charset=utf-8",
async: false,
crossDomain: true,
success: function (result) {
prodWeights = result;
},
error: function (request, textStatus, errorThrown) {
console.log(request.responseText);
console.log(textStatus);
console.log(errorThrown);
}
});
});
console.log(prodWeights);
});
So it passes the SKU properly to the PHP file, "cartProduct.php":
<?php
$sku = $_POST['sku'];
$api_url = 'https://store-xxxxxx.mybigcommerce.com/api/v2/products?sku=' . sku;
$ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array ('Accept: application/json', 'Content-Length: 0') );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_USERPWD, "xxxxx:xxxxxxxxxxxx" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$response = curl_exec( $ch );
$result = json_decode($response);
print_r($result);
?>
I believe there might be an authorization issue, when I try to hit my PHP as just "http://", I get this error: