Merge branch 'product-crud' into product-crud-grouped

# Conflicts:
#	tests/framework/helpers/class-wc-helper-product.php
This commit is contained in:
Mike Jolley 2016-10-18 18:58:08 +01:00
commit 78931220b4
5 changed files with 223 additions and 40 deletions

View File

@ -974,8 +974,6 @@ class WC_Product extends WC_Abstract_Legacy_Product {
'menu_order' => $post_object->menu_order,
) );
$this->read_meta_data();
do_action( 'woocommerce_product_loaded', $this );
}
/**
@ -1058,9 +1056,9 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* Helper method that updates all the post meta for a product based on it's settings in the WC_Product class.
*
* @since 2.7.0
* @param int $id Object ID.
*/
private function update_post_meta( $id ) {
protected function update_post_meta() {
$id = $this->get_id();
update_post_meta( $id, '_featured', $this->get_featured() );
update_post_meta( $id, '_visibility', $this->get_catalog_visibility() );
update_post_meta( $id, '_sku', $this->get_sku() );

View File

@ -10,13 +10,40 @@ if ( ! defined( 'ABSPATH' ) ) {
* External products cannot be bought; they link offsite. Extends simple products.
*
* @class WC_Product_External
* @version 2.0.0
* @version 2.7.0
* @package WooCommerce/Classes/Products
* @category Class
* @author WooThemes
*/
class WC_Product_External extends WC_Product {
/**
* Stores product data.
*
* @var array
*/
protected $extra_data = array(
'product_url' => '',
'button_text' => '',
);
/**
* Merges external product data into the parent object.
* @param int|WC_Product|object $product Product to init.
*/
public function __construct( $product = 0 ) {
$this->data = array_merge( $this->data, $this->extra_data );
parent::__construct( $product );
}
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
|
| Methods for getting data from the product object.
*/
/**
* Get internal type.
* @return string
@ -25,6 +52,60 @@ class WC_Product_External extends WC_Product {
return 'external';
}
/**
* Get product url.
*
* @return string
*/
public function get_product_url() {
return esc_url( $this->data['product_url'] );
}
/**
* Get button text.
*
* @return string
*/
public function get_button_text() {
return $this->data['button_text'] ? $this->data['button_text'] : __( 'Buy product', 'woocommerce' );
}
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
|
| Functions for setting product data. These should not update anything in the
| database itself and should only change what is stored in the class
| object.
*/
/**
* Set product URL.
*
* @since 2.7.0
* @param string $product_url Product URL.
*/
public function set_product_url( $product_url ) {
$this->data['product_url'] = $product_url;
}
/**
* Set button text.
*
* @since 2.7.0
* @param string $button_text Button text.
*/
public function set_button_text( $button_text ) {
$this->data['button_text'] = $button_text;
}
/*
|--------------------------------------------------------------------------
| Other Actions
|--------------------------------------------------------------------------
*/
/**
* Returns false if the product cannot be bought.
*
@ -65,23 +146,36 @@ class WC_Product_External extends WC_Product {
return apply_filters( 'woocommerce_product_single_add_to_cart_text', $this->get_button_text(), $this );
}
/*
|--------------------------------------------------------------------------
| CRUD methods
|--------------------------------------------------------------------------
*/
/**
* Get product url.
* Reads a product from the database and sets its data to the class.
*
* @access public
* @return string
* @since 2.7.0
* @param int $id Product ID.
*/
public function get_product_url() {
return esc_url( $this->product_url );
public function read( $id ) {
parent::read( $id );
$this->set_props( array(
'product_url' => get_post_meta( $id, '_product_url', true ),
'button_text' => get_post_meta( $id, '_button_text', true ),
) );
do_action( 'woocommerce_product_loaded', $this );
do_action( 'woocommerce_product_' . $this->get_type() . '_loaded', $this );
}
/**
* Get button text.
* Helper method that updates all the post meta for an external product.
*
* @access public
* @return string
* @since 2.7.0
*/
public function get_button_text() {
return $this->button_text ? $this->button_text : __( 'Buy product', 'woocommerce' );
protected function update_post_meta() {
parent::update_post_meta();
update_post_meta( $this->get_id(), '_product_url', $this->get_product_url() );
update_post_meta( $this->get_id(), '_button_text', $this->get_button_text() );
}
}

View File

@ -163,26 +163,18 @@ class WC_Product_Grouped extends WC_Product {
public function read( $id ) {
parent::read( $id );
$transient_name = 'wc_product_children_' . $this->get_id();
$grouped_products = array_filter( wp_parse_id_list( (array) get_transient( $transient_name ) ) );
if ( empty( $grouped_products ) ) {
$grouped_products = get_posts( apply_filters( 'woocommerce_grouped_children_args', array(
'post_parent' => $this->get_id(),
'post_type' => 'product',
'orderby' => 'menu_order',
'order' => 'ASC',
'fields' => 'ids',
'post_status' => 'publish',
'numberposts' => -1,
) ) );
set_transient( $transient_name, $grouped_products, DAY_IN_SECONDS * 30 );
}
$this->set_props( array(
'children' => $grouped_products,
'children' => wp_parse_id_list( get_post_meta( $id, '_children', true ) ),
) );
do_action( 'woocommerce_product_loaded', $this );
do_action( 'woocommerce_product_' . $this->get_type() . '_loaded', $this );
}
/**
* Helper method that updates all the post meta for a grouped product.
*/
protected function update_post_meta() {
parent::update_post_meta();
update_post_meta( $this->get_id(), '_children', $this->get_children() );
}
}

View File

@ -81,6 +81,29 @@ class WC_Helper_Product {
return new WC_Product_Grouped( $product );
}
/**
* Create external product.
*
* @since 2.7.0
*
* @return WC_Product_External
*/
public static function create_external_product() {
// Create the product
$product = wp_insert_post( array(
'post_title' => 'Dummy External Product',
'post_type' => 'product',
'post_status' => 'publish',
) );
update_post_meta( $product, '_price', '10' );
update_post_meta( $product, '_regular_price', '10' );
update_post_meta( $product, '_sale_price', '' );
update_post_meta( $product, '_sku', 'DUMMY EXTERNAL SKU' );
update_post_meta( $product, '_product_url', 'http://woocommerce.com' );
update_post_meta( $product, '_button_text', 'Buy external product' );
return new WC_Product_External( $product );
}
/**
* Create a dummy variation product.
*
@ -116,7 +139,7 @@ class WC_Helper_Product {
update_post_meta( $product_id, '_manage_stock', 'no' );
update_post_meta( $product_id, '_tax_status', 'taxable' );
update_post_meta( $product_id, '_downloadable', 'no' );
update_post_meta( $product_id, '_virtual', 'taxable' );
update_post_meta( $product_id, '_virtual', 'no' );
update_post_meta( $product_id, '_visibility', 'visible' );
update_post_meta( $product_id, '_stock_status', 'instock' );
@ -157,7 +180,7 @@ class WC_Helper_Product {
update_post_meta( $variation_id, '_sku', 'DUMMY SKU VARIABLE SMALL' );
update_post_meta( $variation_id, '_manage_stock', 'no' );
update_post_meta( $variation_id, '_downloadable', 'no' );
update_post_meta( $variation_id, '_virtual', 'taxable' );
update_post_meta( $variation_id, '_virtual', 'no' );
update_post_meta( $variation_id, '_stock_status', 'instock' );
// Attribute meta
@ -179,7 +202,7 @@ class WC_Helper_Product {
update_post_meta( $variation_id, '_sku', 'DUMMY SKU VARIABLE SMALL' );
update_post_meta( $variation_id, '_manage_stock', 'no' );
update_post_meta( $variation_id, '_downloadable', 'no' );
update_post_meta( $variation_id, '_virtual', 'taxable' );
update_post_meta( $variation_id, '_virtual', 'no' );
update_post_meta( $variation_id, '_stock_status', 'instock' );
// Attribute meta
@ -283,12 +306,12 @@ class WC_Helper_Product {
public static function create_product_review( $product_id, $review_content = 'Review content here' ) {
$data = array(
'comment_post_ID' => $product_id,
'comment_author' => 'admin',
'comment_author_email' => 'woo@woo.local',
'comment_author_url' => '',
'comment_author' => 'admin',
'comment_author_email' => 'woo@woo.local',
'comment_author_url' => '',
'comment_date' => '2016-01-01T11:11:11',
'comment_content' => $review_content,
'comment_approved' => 1,
'comment_content' => $review_content,
'comment_approved' => 1,
'comment_type' => 'review',
);

View File

@ -177,4 +177,80 @@ class WC_Tests_Product_CRUD extends WC_Unit_Test_Case {
$this->assertEquals( $value, $product->{"get_{$function}"}(), $function );
}
}
/**
* Test creating a new external product.
*
* @since 2.7.0
*/
function test_external_product_create() {
$product = new WC_Product_External;
$product->set_regular_price( 42 );
$product->set_button_text( 'Test CRUD' );
$product->set_product_url( 'http://automattic.com' );
$product->set_name( 'My External Product' );
$product->create();
$read_product = new WC_Product_External( $product->get_id() );
$this->assertEquals( '42', $read_product->get_regular_price() );
$this->assertEquals( 'Test CRUD', $read_product->get_button_text() );
$this->assertEquals( 'http://automattic.com', $read_product->get_product_url() );
$this->assertEquals( 'My External Product', $read_product->get_name() );
}
/**
* Test getting / reading an external product. Make sure both our external
* product data and the main product data are present.
*
* @since 2.7.0
*/
function test_external_product_read() {
$product = WC_Helper_Product::create_external_product();
$product = new WC_Product_External( $product->get_id() );
$this->assertEquals( 'Buy external product', $product->get_button_text() );
$this->assertEquals( '10', $product->get_regular_price() );
}
/**
* Test updating an external product. Make sure both our external
* product data and the main product data are written to and present.
*
* @since 2.7.0
*/
function test_external_product_update() {
$product = WC_Helper_Product::create_external_product();
$this->assertEquals( 'Buy external product', $product->get_button_text() );
$this->assertEquals( '10', $product->get_regular_price() );
$product->set_button_text( 'Buy my external product' );
$product->set_regular_price( 15 );
$product->save();
// Reread from database
$product = new WC_Product_External( $product->get_id() );
$this->assertEquals( 'Buy my external product', $product->get_button_text() );
$this->assertEquals( '15', $product->get_regular_price() );
}
/**
* Test external product setters and getters
*
* @since 2.7.0
*/
public function test_external_product_getters_and_setters() {
$time = time();
$getters_and_setters = array(
'button_text' => 'Test Button Text',
'product_url' => 'http://wordpress.org',
);
$product = new WC_Product_External;
foreach ( $getters_and_setters as $function => $value ) {
$product->{"set_{$function}"}( $value );
$this->assertEquals( $value, $product->{"get_{$function}"}(), $function );
}
}
}