Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 63 additions & 43 deletions includes/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function load_checkout_permalink_template( $template ) {
$products = explode( ',', $products_param );

foreach ( $products as $product ) {
list($product_id, $quantity) = explode( ':', $product );
list( $product_id, $quantity ) = explode( ':', $product );

// Parse the product ID. The input is sent in the Retailer ID format (see get_fb_retailer_id())
// The Retailer ID format is: {product_sku}_{product_id}, so we need to extract the product_id
Expand All @@ -93,32 +93,52 @@ public function load_checkout_permalink_template( $template ) {
$product_id = end( $parts );
}

// Validate and add the product to the cart
if ( is_numeric( $product_id ) && is_numeric( $quantity ) && $quantity > 0 ) {
try {
WC()->cart->add_to_cart( $product_id, $quantity );
} catch ( \Exception $e ) {
\WC_Facebookcommerce_Utils::log_exception_immediately_to_meta(
$e,
$product_obj = wc_get_product( $product_id );

if (
$product_obj &&
$product_obj->is_purchasable() &&
is_numeric( $quantity ) &&
$quantity > 0
) {
$added = WC()->cart->add_to_cart( $product_id, $quantity );
if ( ! $added ) {
$error_message = sprintf(
'WC add_to_cart() failed: product_id=%s, quantity=%s',
$product_id,
$quantity
);

\WC_Facebookcommerce_Utils::log_to_meta(
$error_message,
array(
'event' => 'checkout',
'event_type' => 'checkout_permalink_template_exception',
'incoming_params' => array(
'flow_name' => 'checkout',
'flow_step' => 'add_to_cart',
'extra_data' => [
'products_param' => $products_param,
'product_id' => $product_id,
),
'quantity' => $quantity,
],
)
);
}
} else {
$error_message = sprintf(
'Invalid product or quantity: product_id=%s, quantity=%s',
$product_id,
$quantity
);

\WC_Facebookcommerce_Utils::log_to_meta(
'Failed to add product to cart',
$error_message,
array(
'flow_name' => 'checkout',
'incoming_params' => array(
'flow_name' => 'checkout',
'flow_step' => 'product_quantity_validation',
'extra_data' => [
'products_param' => $products_param,
'product_id' => $product_id,
),
'quantity' => $quantity,
],
)
);
}
Expand All @@ -132,33 +152,33 @@ public function load_checkout_permalink_template( $template ) {

$checkout_url = wc_get_checkout_url();
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Checkout</title>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
iframe {
width: 100%;
height: 100vh;
border: none;
display: block;
max-width: 100%;
max-height: 100%;
box-sizing: border-box;
}
</style>
</head>
<body>
<iframe src="' . esc_url( $checkout_url ) . '"></iframe>
</body>
</html>';
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Checkout</title>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
iframe {
width: 100%;
height: 100vh;
border: none;
display: block;
max-width: 100%;
max-height: 100%;
box-sizing: border-box;
}
</style>
</head>
<body>
<iframe src="' . esc_url( $checkout_url ) . '"></iframe>
</body>
</html>';

exit;
}
Expand Down