2012-11-22 10:22:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Simple Product Class
|
|
|
|
*
|
2012-11-29 16:48:40 +00:00
|
|
|
* The default product type kinda product.
|
2012-11-22 10:22:18 +00:00
|
|
|
*
|
|
|
|
* @class WC_Product_Simple
|
2012-12-03 19:19:58 +00:00
|
|
|
* @version 2.0.0
|
2012-11-22 10:22:18 +00:00
|
|
|
* @package WooCommerce/Classes/Products
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
|
|
|
class WC_Product_Simple extends WC_Product {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __construct function.
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2012-11-22 10:22:18 +00:00
|
|
|
* @access public
|
|
|
|
* @param mixed $product
|
2012-11-22 11:48:16 +00:00
|
|
|
* @param array $args Contains arguments to set up this product
|
2012-11-22 10:22:18 +00:00
|
|
|
*/
|
2012-11-22 11:37:41 +00:00
|
|
|
function __construct( $product, $args ) {
|
2012-11-22 10:22:18 +00:00
|
|
|
|
|
|
|
parent::__construct( $product );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-22 10:22:18 +00:00
|
|
|
$this->product_type = 'simple';
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-22 10:22:18 +00:00
|
|
|
// Load data from custom fields
|
|
|
|
$this->load_product_data( array(
|
2012-11-29 16:48:40 +00:00
|
|
|
'sku' => '',
|
|
|
|
'downloadable' => 'no',
|
|
|
|
'virtual' => 'no',
|
|
|
|
'price' => '',
|
|
|
|
'visibility' => 'hidden',
|
|
|
|
'stock' => 0,
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'backorders' => 'no',
|
|
|
|
'manage_stock' => 'no',
|
|
|
|
'sale_price' => '',
|
|
|
|
'regular_price' => '',
|
|
|
|
'weight' => '',
|
|
|
|
'length' => '',
|
|
|
|
'width' => '',
|
|
|
|
'height' => '',
|
|
|
|
'tax_status' => 'taxable',
|
|
|
|
'tax_class' => '',
|
|
|
|
'upsell_ids' => array(),
|
|
|
|
'crosssell_ids' => array(),
|
2012-11-22 10:22:18 +00:00
|
|
|
'sale_price_dates_from' => '',
|
2012-11-29 16:48:40 +00:00
|
|
|
'sale_price_dates_to' => '',
|
|
|
|
'featured' => 'no',
|
|
|
|
'sold_individually' => 'no'
|
2012-11-22 10:22:18 +00:00
|
|
|
) );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-22 10:22:18 +00:00
|
|
|
$this->check_sale_price();
|
|
|
|
}
|
2012-11-29 16:48:40 +00:00
|
|
|
|
2012-12-11 17:28:47 +00:00
|
|
|
/**
|
|
|
|
* Get the title of the post.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_title() {
|
|
|
|
|
|
|
|
$title = $this->post->post_title;
|
|
|
|
|
|
|
|
if ( $this->get_parent() > 0 ) {
|
|
|
|
$title = get_the_title( $this->get_parent() ) . ' → ' . $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_product_title', apply_filters( 'the_title', $title, $this->id ), $this );
|
|
|
|
}
|
2012-11-29 16:48:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks sale data to see if the product is due to go on sale/sale has expired, and updates the main price.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function check_sale_price() {
|
|
|
|
|
|
|
|
if ( $this->sale_price_dates_from && $this->sale_price_dates_from < current_time('timestamp') ) {
|
|
|
|
|
|
|
|
if ( $this->sale_price && $this->price !== $this->sale_price ) {
|
|
|
|
|
|
|
|
// Update price
|
|
|
|
$this->price = $this->sale_price;
|
|
|
|
update_post_meta( $this->id, '_price', $this->price );
|
|
|
|
|
|
|
|
// Grouped product prices and sale status are affected by children
|
|
|
|
$this->grouped_product_sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->sale_price_dates_to && $this->sale_price_dates_to < current_time('timestamp') ) {
|
|
|
|
|
|
|
|
if ( $this->regular_price && $this->price !== $this->regular_price ) {
|
|
|
|
|
|
|
|
$this->price = $this->regular_price;
|
|
|
|
update_post_meta( $this->id, '_price', $this->price );
|
|
|
|
|
|
|
|
// Sale has expired - clear the schedule boxes
|
|
|
|
update_post_meta( $this->id, '_sale_price', '' );
|
|
|
|
update_post_meta( $this->id, '_sale_price_dates_from', '' );
|
|
|
|
update_post_meta( $this->id, '_sale_price_dates_to', '' );
|
|
|
|
|
|
|
|
// Grouped product prices and sale status are affected by children
|
|
|
|
$this->grouped_product_sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync grouped products with the childs lowest price (so they can be sorted by price accurately).
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function grouped_product_sync() {
|
|
|
|
global $wpdb, $woocommerce;
|
|
|
|
|
|
|
|
if ( ! $this->get_parent() ) return;
|
|
|
|
|
|
|
|
$children_by_price = get_posts( array(
|
|
|
|
'post_parent' => $this->get_parent(),
|
|
|
|
'orderby' => 'meta_value_num',
|
|
|
|
'order' => 'asc',
|
|
|
|
'meta_key' => '_price',
|
|
|
|
'posts_per_page' => 1,
|
|
|
|
'post_type' => 'product',
|
|
|
|
'fields' => 'ids'
|
|
|
|
));
|
|
|
|
if ( $children_by_price ) {
|
|
|
|
foreach ( $children_by_price as $child ) {
|
|
|
|
$child_price = get_post_meta( $child, '_price', true );
|
|
|
|
update_post_meta( $post_parent, '_price', $child_price );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$woocommerce->clear_product_transients( $this->id );
|
|
|
|
}
|
2012-11-22 10:22:18 +00:00
|
|
|
}
|