Plugin do WooCommerce para criação de combos de produtos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

254 lines
7.0 KiB

<?php
/**
* The public-facing functionality of the plugin.
*
* @link https://horizontes.info
* @since 1.0.0
*
* @package Wc_Cdp
* @subpackage Wc_Cdp/public
*/
/**
* The public-facing functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the public-facing stylesheet and JavaScript.
*
* @package Wc_Cdp
* @subpackage Wc_Cdp/public
* @author Horizontes Coop. <contato@horizontes.info>
*/
class Wc_Cdp_Public {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of the plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
}
/**
* Register the stylesheets for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_styles() {
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Wc_Cdp_Loader as all of the hooks are defined
* in that particular class.
*
* The Wc_Cdp_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wc-cdp-public.css', array(), $this->version, 'all' );
}
/**
* Register the JavaScript for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_scripts() {
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Wc_Cdp_Loader as all of the hooks are defined
* in that particular class.
*
* The Wc_Cdp_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wc-cdp-public.js', array( 'jquery' ), $this->version, false );
}
private function get_bundle_products($product_id)
{
$product = wc_get_product( $product_id );
if ( !$product->get_meta( '_wc_cdp_bundle_products' ) ) return null;
$bundle_products = wc_get_products([
'limit' => 2,
'include' => $product->get_meta( '_wc_cdp_bundle_products' ),
]);
return $bundle_products;
}
private function get_bundle_coupon($product_id)
{
$product = wc_get_product( $product_id );
$bundle_coupon = new WC_Coupon( $product->get_meta( '_wc_cdp_bundle_coupon' ) );
$bundle_coupon_type = $bundle_coupon->get_discount_type();
if ( $bundle_coupon_type !== 'percent' && $bundle_coupon_type !== 'fixed_cart') {
return null;
}
return $bundle_coupon;
}
private function should_display_bundle_modal($product_id)
{
$bundle_coupon = $this->get_bundle_coupon($product_id);
if (!$bundle_coupon) return false;
$bundle_products = $this->get_bundle_products($product_id);
if (!$bundle_products) return false;
$all_products_in_cart = true;
foreach ($bundle_products as $bundle_product) {
if ($bundle_product->get_stock_status() === 'outofstock' && $bundle_product->get_backorders() !== 'yes') return false;
$product_cart_id = WC()->cart->generate_cart_id( $bundle_product->get_id() );
if ( !WC()->cart->find_product_in_cart( $product_cart_id ) ) {
$all_products_in_cart = false;
}
}
if ($all_products_in_cart) return false;
return true;
}
public function maybe_display_bundle_modal($cart_item_key, $product_id)
{
if ( $this->should_display_bundle_modal( $product_id ) ) { // TODO: Check if all bundle products are available
add_action( 'woocommerce_after_single_product', array( $this, 'display_bundle_modal' ) );
}
}
public function display_bundle_modal()
{
global $product;
$bundle_products = $this->get_bundle_products( $product->get_id() );
$bundle_ids = array_map( function($product) {
return $product->get_id();
}, $bundle_products );
array_unshift($bundle_products, $product);
$bundle_regular_price = array_sum( array_map( function ($product) {
return $product->get_price();
}, $bundle_products ) );
$bundle_coupon = $this->get_bundle_coupon( $product->get_id() );
if ($bundle_coupon->get_discount_type() === 'percent') {
$bundle_discount_price = $bundle_regular_price * ( 1 - $bundle_coupon->get_amount()/100 );
} else {
$bundle_discount_price = $bundle_regular_price - $bundle_coupon->get_amount();
}
require('partials/product-bundle-modal.php');
}
public function add_bundle_to_cart()
{
$this->check_prerequisites();
$bundle_product_ids = $_POST['wc_cdp_bundle_products'];
foreach ( $bundle_product_ids as $bundle_product_id ) {
$bundle_product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $bundle_product_id ) );
$bundle_product = wc_get_product( $bundle_product_id );
if ( ! $bundle_product ) {
continue;
}
if ( $bundle_product->is_type( 'simple' ) ) {
$quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( $_POST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $bundle_product_id, $quantity );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $bundle_product_id, $quantity ) ) {
wc_add_to_cart_message( array( $bundle_product_id => $quantity ), true );
}
}
}
WC()->cart->apply_coupon( $_POST['wc_cdp_bundle_coupon'] );
wc_clear_notices();
wc_add_notice( __('O combo foi adicionado no seu carrinho.', 'wc-cdp') );
header ('Location: ' . WC()->cart->get_cart_url());
}
/**
* Check any prerequisites required for our add to cart request.
*/
private function check_prerequisites() {
if ( defined( 'WC_ABSPATH' ) ) {
// WC 3.6+ - Cart and notice functions are not included during a REST request.
include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
}
if ( null === WC()->session ) {
$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
WC()->session = new $session_class();
WC()->session->init();
}
if ( null === WC()->customer ) {
WC()->customer = new WC_Customer( get_current_user_id(), true );
}
if ( null === WC()->cart ) {
WC()->cart = new WC_Cart();
// We need to force a refresh of the cart contents from session here (cart contents are normally refreshed on wp_loaded, which has already happened by this point).
WC()->cart->get_cart();
}
}
}