woocommerce/classes/integrations/shareyourcart/class.shareyourcart-wp-wooc...

252 lines
7.0 KiB
PHP
Raw Normal View History

<?php
require_once("class.shareyourcart-wp.php");
if(!class_exists('ShareYourCartWooCommerce',false)){
class ShareYourCartWooCommerce extends ShareYourCartWordpressPlugin{
2012-11-27 16:22:47 +00:00
public $_plugin_name = "shareyourcart_woo_commerce";
public $_post_user_id = 1;
2012-11-27 16:22:47 +00:00
public function processInit(){
if(isset($_REQUEST['action'])){
switch($_REQUEST['action']){
case $this->_plugin_name:
$this->buttonCallback();
break;
case $this->_plugin_name.'_coupon':
$this->couponCallback();
break;
}
}
}
2012-11-27 16:22:47 +00:00
public function isCartActive() {
return in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
2012-11-27 16:22:47 +00:00
}
/*
*
* Extend the base class implementation
*
*/
public function pluginsLoadedHook() {
parent::pluginsLoadedHook();
2012-11-27 16:22:47 +00:00
if(!$this->isCartActive()) return;
2012-11-27 16:22:47 +00:00
add_action('init', array(&$this, 'processInit'));
2012-11-27 16:22:47 +00:00
add_action('woocommerce_before_single_product', array(&$this,'showProductButton'));
add_action('woocommerce_cart_contents', array(&$this,'showCartButton'));
}
2012-11-27 16:22:47 +00:00
/**
*
* Return the jQuery sibling selector for the product button
*
*/
protected function getProductButtonPosition(){
$selector = parent::getProductButtonPosition();
return (!empty($selector) ? $selector : ".summary .price .amount");
}
2012-11-27 16:22:47 +00:00
/**
*
* Return the jQuery sibling selector for the cart button
*
*/
protected function getCartButtonPosition(){
$selector = parent::getCartButtonPosition();
return (!empty($selector) ? $selector : ".cart-subtotal .amount");
}
2012-11-27 16:22:47 +00:00
public function getSecretKey() {
return 'd3ce6c18-7e45-495d-aa4c-8f63edee03a5';
}
2012-11-27 16:22:47 +00:00
public function isSingleProduct() {
return is_singular('product');
}
2012-11-27 16:22:47 +00:00
protected function saveCoupon($token, $coupon_code, $coupon_value, $coupon_type, $product_unique_ids = array()) {
2012-11-27 16:22:47 +00:00
// Create coupon
$post_id = $this->_saveCouponPost($coupon_code);
2012-11-27 16:22:47 +00:00
// Set coupon meta
switch ($coupon_type) {
case 'amount':
$discount_type = 'fixed_product';
$free_shipping = 'no';
break;
case 'percent':
$discount_type = 'percent_product';
$free_shipping = 'no';
break;
case 'free_shipping':
$discount_type = 'fixed_product';
$coupon_value = 0;
$free_shipping = 'yes';
break;
default :
$discount_type = 'fixed_cart';
$free_shipping = 'no';
}
update_post_meta( $post_id, 'customer_email', array() );
update_post_meta( $post_id, 'minimum_amount', '' );
update_post_meta( $post_id, 'exclude_product_categories', array() );
update_post_meta( $post_id, 'product_categories', array() );
update_post_meta( $post_id, 'free_shipping', $free_shipping );
update_post_meta( $post_id, 'apply_before_tax', 'yes' );
update_post_meta( $post_id, 'expiry_date', '' );
update_post_meta( $post_id, 'usage_limit', 1 );
update_post_meta( $post_id, 'exclude_product_ids', '' );
update_post_meta( $post_id, 'product_ids', implode(',', $product_unique_ids));
update_post_meta( $post_id, 'individual_use', 'yes' );
update_post_meta( $post_id, 'coupon_amount', $coupon_value );
update_post_meta( $post_id, 'discount_type', $discount_type );
// parent
parent::saveCoupon( $token, $coupon_code, $coupon_value, $coupon_type );
}
2012-11-27 16:22:47 +00:00
public function applyCoupon($coupon_code) {
//$this->_loadWooCommerce();
2012-11-27 16:22:47 +00:00
//global $woocommerce;
//$woocommerce->cart->add_discount($coupon_code);
2012-11-27 16:22:47 +00:00
return;
}
2012-11-27 16:22:47 +00:00
private function _saveCouponPost($coupon_code){
$new_post = array(
'post_title' => $coupon_code,
'post_name' => ereg_replace("[^A-Za-z0-9]", "", $coupon_code),
'post_content' => '',
'post_status' => 'publish',
'comment_status'=> 'closed',
'ping_status' => 'closed',
'post_author' => $this->_post_user_id,
'post_type' => 'shop_coupon'
);
$post_id = wp_insert_post($new_post);
return $post_id;
}
2012-11-27 16:22:47 +00:00
public function getButtonCallbackURL() {
global $wp_query;
$callback_url = add_query_arg( 'action', $this->_plugin_name, trailingslashit( home_url() ) );
if ($this->isSingleProduct()) {
$callback_url .= '&p='. $wp_query->post->ID;
}
return $callback_url;
}
2012-11-27 16:22:47 +00:00
public function buttonCallback(){
if(!$this->isCartActive()) return;
$this->_loadWooCommerce();
2012-12-23 15:16:04 +00:00
global $woocommerce;
//specify the parameters
$params = array(
'callback_url' => get_bloginfo('wpurl').'/?action='.$this->_plugin_name.'_coupon'.(isset($_REQUEST['p']) ? '&p='.$_REQUEST['p'] : '' ),
'success_url' => get_option('shopping_cart_url'),
'cancel_url' => get_option('shopping_cart_url'),
);
//there is no product set, thus send the products from the shopping cart
if(!isset($_REQUEST['p']))
2012-11-27 16:22:47 +00:00
{
2012-12-23 15:16:04 +00:00
if(sizeof( $woocommerce->cart->get_cart() ) == 0)
exit("Cart is empty");
2012-12-23 15:16:04 +00:00
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$params['cart'][] = $this->_getProductDetails($values['data']);
}
}
else
{
$params['cart'][] = $this->_getProductDetails($_REQUEST['p']);
}
2012-11-27 16:22:47 +00:00
try
{
$this->startSession($params);
2012-11-27 16:22:47 +00:00
}
catch(Exception $e)
{
//display the error to the user
echo $e->getMessage();
}
exit;
}
2012-11-27 16:22:47 +00:00
private function _getProductDetails($product_id){
2012-12-23 15:16:04 +00:00
if(is_object($product_id)) { //if we've already received the product, use this one
$product = $product_id;
}
else { //it's only a number, so get the product
$product = get_product($product_id);
}
//WooCommerce actually echoes the image
ob_start();
2013-03-03 17:07:31 +00:00
echo $product->get_image(); //older WooCommerce versions might already echo, but newer versions don't, so force it anyway
$image = ob_get_clean();
2012-11-27 16:22:47 +00:00
//check is image actually a HTML img entity
if(($doc = @DomDocument::loadHTML($image)) !== FALSE)
{
$imageTags = $doc->getElementsByTagName('img');
if($imageTags->length >0 )
$src = $imageTags->item(0)->getAttribute('src');
2012-11-27 16:22:47 +00:00
//replace image only if src has been set
if (!empty($src))
$image = $src;
}
return array(
"item_name" => $product->get_title(),
"item_description" => $product->post->post_excerpt,
2012-12-23 15:16:04 +00:00
"item_url" => get_permalink($product->id),
2012-11-27 16:22:47 +00:00
"item_price" => $product->price,
"item_picture_url" => $image,
2012-12-23 15:16:04 +00:00
"item_unique_id" => $product->id,
);
}
2012-11-27 16:22:47 +00:00
public function loadSessionData() {
return;
}
2012-11-27 16:22:47 +00:00
private function _loadWooCommerce(){
// Sometimes the WooCommerce Class is not loaded...
2012-11-27 16:22:47 +00:00
if(!class_exists('Woocommerce', false)){
require_once(ABSPATH . 'wp-content/plugins/woocommerce/woocommerce.php');
}
2012-11-27 16:22:47 +00:00
// Important Classes Not included
if(!function_exists('has_post_thumbnail')){
require_once(ABSPATH . 'wp-includes/post-thumbnail-template.php');
}
}
}
//TODO: see why this is not used
add_action(ShareYourCartWordpressPlugin::getPluginFile(), array('ShareYourCartWooCommerce','uninstallHook'));
} //END IF