Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions class-wc-facebookcommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class WC_Facebookcommerce extends WooCommerce\Facebook\Framework\Plugin {
/** @var WooCommerce\Facebook\AJAX Ajax handler instance */
private $ajax;

/** @var WooCommerce\Facebook\Checkout */
private $checkout;

/** @var WooCommerce\Facebook\Products\Feed product feed handler */
private $product_feed;

Expand Down Expand Up @@ -184,6 +187,7 @@ public function init() {
$this->heartbeat = new Heartbeat( WC()->queue() );
$this->heartbeat->init();

$this->checkout = new WooCommerce\Facebook\Checkout();
$this->product_feed = new WooCommerce\Facebook\Products\Feed();
$this->products_stock_handler = new WooCommerce\Facebook\Products\Stock();
$this->products_sync_handler = new WooCommerce\Facebook\Products\Sync();
Expand Down
145 changes: 145 additions & 0 deletions includes/Checkout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* @package FacebookCommerce
*/

namespace WooCommerce\Facebook;

defined( 'ABSPATH' ) || exit;

/**
* The checkout permalink.
*
* @since 3.3.0
*/
class Checkout {

/**
* Checkout constructor.
*
* @since 3.3.0
*/
public function __construct() {
// add the necessary action and filter hooks
$this->add_hooks();
}

/**
* Adds the necessary action and filter hooks.
*
* @since 3.3.0
*/
public function add_hooks() {
// add the rewrite rule for the checkout permalink
add_action( 'init', array( $this, 'add_checkout_permalink_rewrite_rule' ) );

// add the query var for the checkout permalink
add_filter( 'query_vars', array( $this, 'add_checkout_permalink_query_var' ) );

// load the checkout permalink template
add_filter( 'template_include', array( $this, 'load_checkout_permalink_template' ) );

// flush rewrite rules when plugin is activated
register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules_on_activation' ) );

// flush rewrite rules when plugin is deactivated
register_deactivation_hook( __FILE__, array( $this, 'flush_rewrite_rules_on_deactivation' ) );
}

/**
* Adds a rewrite rule for the checkout permalink.
*
* @since 3.3.0
*/
public function add_checkout_permalink_rewrite_rule() {
add_rewrite_rule( '^fb-checkout/?$', 'index.php?fb_checkout=1', 'top' );
}

/**
* Adds query vars for the checkout permalink.
*
* @since 3.3.0
*
* @param array $vars
* @return array
*/
public function add_checkout_permalink_query_var( $vars ) {
// Add 'fb_checkout' as a query var
$vars[] = 'fb_checkout';

// Add 'products' as a query var
$vars[] = 'products';

// Add 'coupon' as a query var
$vars[] = 'coupon';

return $vars;
}

/**
* Loads the checkout permalink template.
*
* @since 3.3.0
*/
public function load_checkout_permalink_template() {
if ( get_query_var( 'fb_checkout' ) ) {
// Clear the WooCommerce cart
WC()->cart->empty_cart();

// Get the 'products' query parameter
$products_param = get_query_var( 'products' );

if ( $products_param ) {
// Split multiple products by comma
$products = explode( ',', $products_param );

foreach ( $products as $product ) {
// Parse each product ID and quantity
list($product_id, $quantity) = explode( ':', $product );

// Validate and add the product to the cart
if ( is_numeric( $product_id ) && is_numeric( $quantity ) && $quantity > 0 ) {
WC()->cart->add_to_cart( $product_id, $quantity );
}
}
}

// Get the 'coupon' query parameter
$coupon_code = get_query_var( 'coupon' );

if ( $coupon_code ) {
// Apply the coupon to the cart
WC()->cart->apply_coupon( sanitize_text_field( $coupon_code ) );
}

// Use a custom template file
include plugin_dir_path( __FILE__ ) . 'Templates/CheckoutTemplate.php';

exit;
}
}

/**
* Flushes rewrite rules when the plugin is activated.
*
* @since 3.3.0
*/
public function flush_rewrite_rules_on_activation() {
$this->add_checkout_permalink_rewrite_rule();
flush_rewrite_rules();
}

/**
* Flushes rewrite rules when the plugin is deactivated.
*
* @since 3.3.0
*/
public function flush_rewrite_rules_on_deactivation() {
flush_rewrite_rules();
}
}
28 changes: 28 additions & 0 deletions includes/Templates/CheckoutTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* Template Name: Checkout Template
*
* @package FacebookCommerce
*/

namespace WooCommerce\Facebook\Templates;

defined( 'ABSPATH' ) || exit;

get_header(); ?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Display the WooCommerce cart
echo do_shortcode( '[woocommerce_checkout]' );
?>
</main><!-- .site-main -->
</div><!-- .content-area -->

<?php get_footer(); ?>