2014-10-21 22:26:04 +00:00
|
|
|
<?php
|
|
|
|
|
2014-10-28 10:53:36 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Class WC_Helper_Product.
|
2014-10-28 10:53:36 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* This helper class should ONLY be used for unit tests!.
|
2014-10-28 10:53:36 +00:00
|
|
|
*/
|
2014-10-21 22:26:04 +00:00
|
|
|
class WC_Helper_Product {
|
|
|
|
|
2014-10-28 10:53:36 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Delete a product.
|
2014-10-28 10:53:36 +00:00
|
|
|
*
|
|
|
|
* @param $product_id
|
|
|
|
*
|
|
|
|
* @todo check for variations, attributes, etc.
|
|
|
|
*/
|
2014-10-24 13:36:13 +00:00
|
|
|
public static function delete_product( $product_id ) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo check for variations, attributes, etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Delete the psot
|
2014-10-24 19:14:19 +00:00
|
|
|
wp_delete_post( $product_id, true );
|
2014-10-24 13:36:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-21 22:26:04 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Create simple product.
|
2014-10-21 22:26:04 +00:00
|
|
|
*
|
2014-10-24 13:36:13 +00:00
|
|
|
* @since 2.3
|
|
|
|
*
|
2014-10-21 22:26:04 +00:00
|
|
|
* @return WC_Product_Simple
|
|
|
|
*/
|
|
|
|
public static function create_simple_product() {
|
|
|
|
|
|
|
|
// Create the product
|
2014-10-24 13:36:13 +00:00
|
|
|
$product = wp_insert_post( array(
|
|
|
|
'post_title' => 'Dummy Product',
|
|
|
|
'post_type' => 'product',
|
2016-08-27 01:46:45 +00:00
|
|
|
'post_status' => 'publish',
|
2014-10-24 13:36:13 +00:00
|
|
|
) );
|
2014-10-21 22:26:04 +00:00
|
|
|
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 SKU' );
|
|
|
|
update_post_meta( $product, '_manage_stock', 'no' );
|
|
|
|
update_post_meta( $product, '_tax_status', 'taxable' );
|
|
|
|
update_post_meta( $product, '_downloadable', 'no' );
|
|
|
|
update_post_meta( $product, '_virtual', 'taxable' );
|
|
|
|
update_post_meta( $product, '_visibility', 'visible' );
|
|
|
|
update_post_meta( $product, '_stock_status', 'instock' );
|
|
|
|
|
|
|
|
return new WC_Product_Simple( $product );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-29 16:24:01 +00:00
|
|
|
* Create a dummy variation product.
|
2014-10-21 22:26:04 +00:00
|
|
|
*
|
2014-10-24 13:36:29 +00:00
|
|
|
* @since 2.3
|
|
|
|
*
|
2014-10-21 22:26:04 +00:00
|
|
|
* @return WC_Product_Variable
|
|
|
|
*/
|
|
|
|
public static function create_variation_product() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
// Create all attribute related things
|
|
|
|
$attribute_data = self::create_attribute();
|
|
|
|
|
|
|
|
// Create the product
|
2014-10-24 13:36:29 +00:00
|
|
|
$product_id = wp_insert_post( array(
|
|
|
|
'post_title' => 'Dummy Product',
|
|
|
|
'post_type' => 'product',
|
2016-08-27 01:46:45 +00:00
|
|
|
'post_status' => 'publish',
|
2014-10-24 13:36:29 +00:00
|
|
|
) );
|
2014-10-21 22:26:04 +00:00
|
|
|
|
2016-06-30 15:42:03 +00:00
|
|
|
// Set it as variable.
|
|
|
|
wp_set_object_terms( $product_id, 'variable', 'product_type' );
|
|
|
|
|
2014-10-21 22:26:04 +00:00
|
|
|
// Price related meta
|
|
|
|
update_post_meta( $product_id, '_price', '10' );
|
|
|
|
update_post_meta( $product_id, '_min_variation_price', '10' );
|
2015-06-09 13:02:02 +00:00
|
|
|
update_post_meta( $product_id, '_max_variation_price', '15' );
|
2014-10-21 22:26:04 +00:00
|
|
|
update_post_meta( $product_id, '_min_variation_regular_price', '10' );
|
2015-06-09 13:02:02 +00:00
|
|
|
update_post_meta( $product_id, '_max_variation_regular_price', '15' );
|
2014-10-21 22:26:04 +00:00
|
|
|
|
|
|
|
// General meta
|
|
|
|
update_post_meta( $product_id, '_sku', 'DUMMY SKU' );
|
|
|
|
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, '_visibility', 'visible' );
|
|
|
|
update_post_meta( $product_id, '_stock_status', 'instock' );
|
|
|
|
|
|
|
|
// Attributes
|
|
|
|
update_post_meta( $product_id, '_default_attributes', array() );
|
|
|
|
update_post_meta( $product_id, '_product_attributes', array(
|
2015-07-28 16:07:48 +00:00
|
|
|
'pa_size' => array(
|
|
|
|
'name' => 'pa_size',
|
2014-10-21 22:26:04 +00:00
|
|
|
'value' => '',
|
|
|
|
'position' => '1',
|
|
|
|
'is_visible' => 0,
|
|
|
|
'is_variation' => 1,
|
2016-08-27 01:46:45 +00:00
|
|
|
'is_taxonomy' => 1,
|
|
|
|
),
|
2014-10-21 22:26:04 +00:00
|
|
|
) );
|
|
|
|
|
|
|
|
// Link the product to the attribute
|
|
|
|
$wpdb->insert( $wpdb->prefix . 'term_relationships', array(
|
|
|
|
'object_id' => $product_id,
|
|
|
|
'term_taxonomy_id' => $attribute_data['term_taxonomy_id'],
|
2016-08-27 01:46:45 +00:00
|
|
|
'term_order' => 0,
|
2014-10-21 22:26:04 +00:00
|
|
|
) );
|
|
|
|
$return['term_taxonomy_id'] = $wpdb->insert_id;
|
|
|
|
|
|
|
|
// Create the variation
|
|
|
|
$variation_id = wp_insert_post( array(
|
|
|
|
'post_title' => 'Variation #' . ( $product_id + 1 ) . ' of Dummy Product',
|
|
|
|
'post_type' => 'product_variation',
|
|
|
|
'post_parent' => $product_id,
|
2016-08-27 01:46:45 +00:00
|
|
|
'post_status' => 'publish',
|
2014-10-21 22:26:04 +00:00
|
|
|
) );
|
|
|
|
|
|
|
|
// Price related meta
|
|
|
|
update_post_meta( $variation_id, '_price', '10' );
|
|
|
|
update_post_meta( $variation_id, '_regular_price', '10' );
|
|
|
|
|
|
|
|
// General meta
|
|
|
|
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, '_stock_status', 'instock' );
|
|
|
|
|
|
|
|
// Attribute meta
|
|
|
|
update_post_meta( $variation_id, 'attribute_pa_size', 'small' );
|
|
|
|
|
2015-06-09 13:02:02 +00:00
|
|
|
// Create the variation
|
|
|
|
$variation_id = wp_insert_post( array(
|
|
|
|
'post_title' => 'Variation #' . ( $product_id + 2 ) . ' of Dummy Product',
|
|
|
|
'post_type' => 'product_variation',
|
|
|
|
'post_parent' => $product_id,
|
2016-08-27 01:46:45 +00:00
|
|
|
'post_status' => 'publish',
|
2015-06-09 13:02:02 +00:00
|
|
|
) );
|
|
|
|
|
|
|
|
// Price related meta
|
|
|
|
update_post_meta( $variation_id, '_price', '15' );
|
|
|
|
update_post_meta( $variation_id, '_regular_price', '15' );
|
|
|
|
|
|
|
|
// General meta
|
|
|
|
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, '_stock_status', 'instock' );
|
|
|
|
|
|
|
|
// Attribute meta
|
|
|
|
update_post_meta( $variation_id, 'attribute_pa_size', 'large' );
|
|
|
|
|
|
|
|
// Add the variation meta to the main product
|
2014-10-21 22:26:04 +00:00
|
|
|
update_post_meta( $product_id, '_max_price_variation_id', $variation_id );
|
|
|
|
|
|
|
|
return new WC_Product_Variable( $product_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Create a dummy attribute.
|
2014-10-21 22:26:04 +00:00
|
|
|
*
|
2014-10-24 13:36:29 +00:00
|
|
|
* @since 2.3
|
|
|
|
*
|
2014-10-21 22:26:04 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function create_attribute() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$return = array();
|
|
|
|
|
2016-09-29 16:24:01 +00:00
|
|
|
$attribute_name = 'size';
|
2014-10-21 22:26:04 +00:00
|
|
|
|
|
|
|
// Create attribute
|
|
|
|
$attribute = array(
|
|
|
|
'attribute_label' => $attribute_name,
|
|
|
|
'attribute_name' => $attribute_name,
|
|
|
|
'attribute_type' => 'select',
|
|
|
|
'attribute_orderby' => 'menu_order',
|
2014-12-19 17:58:49 +00:00
|
|
|
'attribute_public' => 0,
|
2014-10-21 22:26:04 +00:00
|
|
|
);
|
|
|
|
$wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute );
|
|
|
|
$return['attribute_id'] = $wpdb->insert_id;
|
|
|
|
|
|
|
|
// Register the taxonomy
|
2014-10-24 13:36:29 +00:00
|
|
|
$name = wc_attribute_taxonomy_name( $attribute_name );
|
2014-10-21 22:26:04 +00:00
|
|
|
$label = $attribute_name;
|
|
|
|
|
|
|
|
// Add the term
|
|
|
|
$wpdb->insert( $wpdb->prefix . 'terms', array(
|
|
|
|
'name' => 'small',
|
|
|
|
'slug' => 'small',
|
2016-08-27 01:46:45 +00:00
|
|
|
'term_group' => 0,
|
2014-10-21 22:26:04 +00:00
|
|
|
), array(
|
|
|
|
'%s',
|
|
|
|
'%s',
|
2016-08-27 02:08:49 +00:00
|
|
|
'%d',
|
2014-10-21 22:26:04 +00:00
|
|
|
) );
|
|
|
|
$return['term_id'] = $wpdb->insert_id;
|
|
|
|
|
|
|
|
// Add the term_taxonomy
|
|
|
|
$wpdb->insert( $wpdb->prefix . 'term_taxonomy', array(
|
|
|
|
'term_id' => $return['term_id'],
|
2016-09-29 16:24:01 +00:00
|
|
|
'taxonomy' => 'pa_size',
|
2014-10-21 22:26:04 +00:00
|
|
|
'description' => '',
|
|
|
|
'parent' => 0,
|
2016-08-27 01:46:45 +00:00
|
|
|
'count' => 1,
|
2014-10-21 22:26:04 +00:00
|
|
|
) );
|
|
|
|
$return['term_taxonomy_id'] = $wpdb->insert_id;
|
|
|
|
|
|
|
|
// Delete transient
|
|
|
|
delete_transient( 'wc_attribute_taxonomies' );
|
|
|
|
|
2016-09-29 16:24:01 +00:00
|
|
|
$taxonomy_data = array(
|
|
|
|
'labels' => array(
|
|
|
|
'name' => __( 'size', 'woocommerce' ),
|
2016-10-04 21:57:01 +00:00
|
|
|
),
|
2016-09-29 16:24:01 +00:00
|
|
|
);
|
|
|
|
register_taxonomy( 'pa_size', array( 'product' ), $taxonomy_data );
|
|
|
|
|
2014-10-21 22:26:04 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Delete an attribute.
|
2014-10-21 22:26:04 +00:00
|
|
|
*
|
|
|
|
* @param $attribute_id
|
|
|
|
*
|
2014-10-24 13:36:29 +00:00
|
|
|
* @since 2.3
|
|
|
|
*
|
2014-10-21 22:26:04 +00:00
|
|
|
* @todo clean up all term/taxonomy/etc data
|
|
|
|
*/
|
|
|
|
public static function delete_attribute( $attribute_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$attribute_id = absint( $attribute_id );
|
|
|
|
|
|
|
|
$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = $attribute_id" );
|
|
|
|
}
|
|
|
|
|
2016-07-28 22:12:49 +00:00
|
|
|
/**
|
|
|
|
* Creates a new product review on a specific product.
|
|
|
|
*
|
|
|
|
* @since 2.7
|
|
|
|
* @param $product_id integer Product ID that the review is for
|
|
|
|
* @param $revieww_content string Content to use for the product review
|
|
|
|
* @return integer Product Review ID
|
|
|
|
*/
|
|
|
|
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_date' => '2016-01-01T11:11:11',
|
|
|
|
'comment_content' => $review_content,
|
|
|
|
'comment_approved' => 1,
|
|
|
|
'comment_type' => 'review',
|
|
|
|
);
|
|
|
|
|
|
|
|
return wp_insert_comment( $data );
|
|
|
|
}
|
2015-07-28 16:07:48 +00:00
|
|
|
}
|