plugin_name = plugin_basename(__FILE__);
$this->shop_ajax_listing = get_option('wphopt_pixel_ajax_listing', '0');
$this->shop_ajax_product = get_option('wphopt_pixel_ajax_product', '0');
// all pages
add_action( 'wp_head', [$this, 'init_pixel']); // inicjalizacja wph pixel
// dodanie do koszyka
// 3. ajax add to cart on category page and hotspots
if($this->shop_ajax_product) {
// 2. ajax add to cart woocommerce_after_add_to_cart_button
add_action('wp_footer', [$this, 'pixel_add_to_cart']); // dodanie zdarzenia po dodaniu do koszyka
} else {
// 1. refresh page (not ajax)
add_action( 'wp_footer', [$this, 'pixel_add_to_cart_refresh']);
}
if($this->shop_ajax_listing) {
add_action('wp_enqueue_scripts', [$this, 'pixel_add_to_cart_box']);
add_action( 'wp_ajax_wph_ajax_get_product', [$this, 'wph_ajax_get_product'] );
add_action( 'wp_ajax_nopriv_wph_ajax_get_product', [$this, 'wph_ajax_get_product'] );
}
if('yes' === get_option( 'woocommerce_cart_redirect_after_add' )) {
add_action( 'init', [$this, 'init_session'] );
add_action('woocommerce_add_to_cart', [$this, 'pixel_add_to_cart_redirect'], 10, 4);
}
// home page
add_action( 'wp_footer', [$this, 'pixel_home']); // kod na stronie głównej
// product page
add_action( 'wp_footer', [$this, 'pixel_product']); // strona produktu
// category page
add_action( 'wp_footer', [$this, 'pixel_category']); // strona kategorii
// received order page
add_action( 'wp_footer', [$this, 'pixel_purchase']); // strona podsumowania zamowienia
// landing pages, other pages
add_action( 'wp_footer', [$this, 'pixel_otherpages']); // strony stworzone przez użytkownika
}
public function init_session()
{
if ( ! session_id() ) {
session_start();
}
}
public function init_pixel()
{
$pixel_id = get_option($this->pixel_option, '');
?>
wph_viewproduct_sizes($product);
$price = $this->wph_price(wc_get_price_including_tax($product));
?>
admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce('wph-ajax-get-product')
]
);
}
public function wph_ajax_get_product()
{
if ( isset($_REQUEST) && is_numeric($_REQUEST['product_id']) ) {
$product_id = sanitize_text_field($_REQUEST['product_id']);
$product = wc_get_product($product_id);
$sizes = $this->wph_addtocart_sizes();
$sizes = str_replace("['", '', $sizes);
$sizes = str_replace("']", '', $sizes);
$data = [
'name'=>$product->get_name(),
'id'=>(string)$product->get_id(),
'sizes'=>[$sizes],
'category'=>$this->wph_getcategory($product),
'in_stock'=>$this->wph_instock($product->get_stock_status())
];
$price = $this->wph_price(wc_get_price_including_tax($product));
if($price) {
$data['price'] = floatval($price);
}
if($this->size_color)
$data['colour'] = $this->size_color;
echo json_encode($data);
}
die();
}
public function pixel_add_to_cart_redirect($cart_id, $id_product, $quantity, $id_variation)
{
$_SESSION['wph_add_to_cart_product_id'] = [
'id_product' => $id_product,
'quantity' => $quantity,
'id_variation' => $id_variation
];
}
public function pixel_add_to_cart_refresh()
{
$variation_id = NULL;
if(isset($_REQUEST['add-to-cart'])) {
$addToCart = $_REQUEST['add-to-cart'];
$quantity = $_REQUEST['quantity'];
if(isset($_REQUEST['variation_id']) && $_REQUEST['variation_id']) {
$variation_id = $_REQUEST['variation_id'];
}
} else if(isset($_SESSION['wph_add_to_cart_product_id']) && is_array($_SESSION['wph_add_to_cart_product_id'])) {
$addToCart = $_SESSION['wph_add_to_cart_product_id']['id_product'];
$quantity = $_SESSION['wph_add_to_cart_product_id']['quantity'];
if($_SESSION['wph_add_to_cart_product_id']['id_variation']) {
$variation_id = $_SESSION['wph_add_to_cart_product_id']['id_variation'];
}
unset($_SESSION['wph_add_to_cart_product_id']);
} else {
return;
}
if(!isset($addToCart) || !is_numeric($addToCart))
return;
global $product;
if($variation_id) {
$product = new WC_Product_Variation($variation_id);
if(!is_a( $product, 'WC_Product_Variation' )) {
return;
}
} else {
$product = wc_get_product($addToCart);
if(is_a( $product, 'WC_Product' )) {
if($product->get_type() == 'variable') {
return;
}
} else {
return;
}
}
$product_id = $product->get_parent_id();
if(!$product_id)
$product_id = $product->get_id();
$sizes = $this->wph_addtocart_sizes();
$price = $this->wph_price(wc_get_price_including_tax($product));
?>
get_type() == 'variable') {
$current_products = $product->get_children();
if(is_array($current_products)) {
$jsVariant = 'var pv = [];';
foreach($current_products as $variant) {
$ProductVariant = wc_get_product($variant);
$price = $this->wph_price($ProductVariant->get_price());
$price = ($price) ? $price : '0';
$jsVariant .= 'pv['.esc_html( $variant ).'] = '.$price.';';
}
}
$attributes = $product->get_attributes();
if(is_array($attributes)) {
foreach($attributes as $attribute => $obj) {
$data = $obj->get_data();
if($data['variation'] && $data['visible']) {
$label = wc_attribute_label($attribute);
if($this->wph_check_is_size($label)) {
$rozmiar = [
'attr'=>'attribute_'.esc_html( $attribute ),
'label'=>esc_html( $label ),
];
}
if($this->wph_check_is_color($label))
$color = 'attribute_'.esc_html( $attribute );
}
}
}
}
/*
Potrzebna obsluga .add_to_cart_button czyli produktow na kategorii
dodanie do koszyka na karcie produktu
*/
?>
have_posts()):
while ( $wp_query->have_posts() ) : $wp_query->the_post();
global $post;
$id_post = $post->ID;
$productOne = wc_get_product( $id_post );
if(!$productOne) {
continue;
}
$pid = esc_html( $productOne->get_id() );
$pname = esc_html( $productOne->get_name() );
$sizes = $this->wph_viewproduct_sizes($productOne);
$stock = $this->wph_instock($productOne->get_stock_status(), true);
$price = $this->wph_price(wc_get_price_including_tax($productOne));
$cat = $this->wph_getcategory($productOne);
$oneObj = [
'id'=>(string)$pid,
'name'=>$pname,
'category'=>$cat,
'sizes'=>$sizes,
'in_stock'=>$stock
];
if($this->size_color)
$oneObj['colour'] = $this->size_color;
if($price)
$oneObj['price'] = floatval($price);
$contents[] = $oneObj;
endwhile;
endif;
?>
query_vars['order-received'] );
if( $order = wc_get_order( $order_id ) ) {
$data = $order->get_data();
$items = $order->get_items();
}
$coupons = $order->get_coupon_codes();
$shipping_total = $data['shipping_total'] + $data['shipping_tax'];
if(!$shipping_total) $shipping_total = 0.00;
$value_gross = $data['total'] - $shipping_total;
$value = $value_gross - $data['cart_tax'];
if(is_array($coupons)) {
$coupons_show = implode(',', $coupons);
$coupons_show = esc_html($coupons_show);
}
// end prepare
$value = $this->wph_price($value);
$value_gross = $this->wph_price($value_gross);
$shipping_total = $this->wph_price($shipping_total);
?>
get_stock_status()
// ($status == 'onbackorder') = false
if($raw)
return ($status == 'instock') ? true : false;
else
return ($status == 'instock') ? 'true' : 'false';
}
public function wph_quantity($quantity)
{
// from $product->get_stock_quantity()
return (is_numeric($quantity)) ? esc_html($quantity) : "1";
}
public function wph_purchase_sizes($meta)//$attributes
{
$output = [];
$this->size_color = [];
if(is_array($meta)) {
foreach($meta as $single_meta) {
$single_data = $single_meta->get_data();
$label = wc_attribute_label($single_data['key']);
if($this->wph_check_is_color($label))
$this->size_color[] = $single_data['value'];
if($this->wph_check_is_size($label))
$output[] = $single_data['value'];
}
}
if(is_array($this->size_color) && !count($this->size_color)) {
$this->size_color[] = 'onecolour';
}
if(!$output) $output[] = 'onesize';
return $output;
}
public function wph_addtocart_sizes() // data from $_REQUEST
{
$output = '';
$this->size_color = [];
if(is_array($_REQUEST)) {
foreach($_REQUEST as $attr => $val) {
if(strpos($attr, 'attribute_') !== false) {
$attr_prod = str_replace('attribute_', '', $attr);
$label = wc_attribute_label($attr_prod);
if($this->wph_check_is_color($label))
$this->size_color[] = $val;
if($this->wph_check_is_size($label)) {
$output = $val;
}
}
}
}
if(is_array($this->size_color) && !count($this->size_color)) {
$this->size_color[] = 'onecolour';
}
if(!$output) $output = 'onesize';
return $output;
}
private function wph_viewproduct_sizes($product)
{
$attributes = $product->get_attributes();
$output = [];
$this->size_color = [];
if(is_array($attributes)) {
foreach($attributes as $key => $value)
{
$label = wc_attribute_label($key);
if($value->get_variation() && $this->wph_check_is_color($label)) {
$attribute_slug = wc_get_product_terms( get_the_ID(), $key, array( 'fields' => 'slugs' ) );
if(is_array($attribute_slug)) {
foreach($attribute_slug as $one) {
$one_esc = esc_html($one);
$this->size_color[] = $one_esc;
}
}
}
if($value->get_variation() && $this->wph_check_is_size($label)) {
$attribute_slug = wc_get_product_terms( get_the_ID(), $key, array( 'fields' => 'slugs' ) );
if(is_array($attribute_slug)) {
foreach($attribute_slug as $one) {
$one_esc = esc_html($one);
$output[] = $one_esc;
}
}
}
}
}
if(is_array($this->size_color) && !count($this->size_color)) {
$this->size_color[] = 'onecolour';
}
if(!$output) $output[] = 'onesize';
return $output;
}
private function wph_getcategory($product)
{
$id = $product->get_type() == 'variation' ? wp_get_post_parent_id($product->get_id()) : $product->get_id();
$terms = wc_get_product_terms(
$id,
'product_cat',
[
'orderby' => 'parent',
'order' => 'DESC',
]
);
return $terms[0]->name;
}
private function wph_check_is_size($label)
{
return strpos(strtolower($label), 'rozmiar') !== false || strpos(strtolower($label), 'size') !== false;
}
private function wph_check_is_color($label)
{
return strpos(strtolower($label), 'color') !== false || strpos(strtolower($label), 'kolor') !== false || strpos(strtolower($label), 'colour') !== false;
}
}
$WPH_Core = new WPHPixelCore();
require_once('admin/admin.php');
$Admin = new WPHPixelAdmin($WPH_Core->plugin_name);
$Admin->init();