Make create_attribute helper use core functions

This commit is contained in:
Mike Jolley 2019-03-11 14:59:10 +00:00
parent 845300c975
commit db6a73d474
1 changed files with 124 additions and 79 deletions

View File

@ -1,4 +1,9 @@
<?php <?php
/**
* Product helpers.
*
* @package woocommerce/tests
*/
/** /**
* Class WC_Helper_Product. * Class WC_Helper_Product.
@ -10,7 +15,7 @@ class WC_Helper_Product {
/** /**
* Delete a product. * Delete a product.
* *
* @param $product_id * @param int $product_id ID to delete.
*/ */
public static function delete_product( $product_id ) { public static function delete_product( $product_id ) {
$product = wc_get_product( $product_id ); $product = wc_get_product( $product_id );
@ -23,22 +28,25 @@ class WC_Helper_Product {
* Create simple product. * Create simple product.
* *
* @since 2.3 * @since 2.3
* @param bool $save Save or return object.
* @return WC_Product_Simple * @return WC_Product_Simple
*/ */
public static function create_simple_product( $save = true ) { public static function create_simple_product( $save = true ) {
$product = new WC_Product_Simple(); $product = new WC_Product_Simple();
$product->set_props( array( $product->set_props(
'name' => 'Dummy Product', array(
'regular_price' => 10, 'name' => 'Dummy Product',
'price' => 10, 'regular_price' => 10,
'sku' => 'DUMMY SKU', 'price' => 10,
'manage_stock' => false, 'sku' => 'DUMMY SKU',
'tax_status' => 'taxable', 'manage_stock' => false,
'downloadable' => false, 'tax_status' => 'taxable',
'virtual' => false, 'downloadable' => false,
'stock_status' => 'instock', 'virtual' => false,
'weight' => '1.1', 'stock_status' => 'instock',
) ); 'weight' => '1.1',
)
);
if ( $save ) { if ( $save ) {
$product->save(); $product->save();
@ -56,13 +64,15 @@ class WC_Helper_Product {
*/ */
public static function create_external_product() { public static function create_external_product() {
$product = new WC_Product_External(); $product = new WC_Product_External();
$product->set_props( array( $product->set_props(
'name' => 'Dummy External Product', array(
'regular_price' => 10, 'name' => 'Dummy External Product',
'sku' => 'DUMMY EXTERNAL SKU', 'regular_price' => 10,
'product_url' => 'http://woocommerce.com', 'sku' => 'DUMMY EXTERNAL SKU',
'button_text' => 'Buy external product', 'product_url' => 'http://woocommerce.com',
) ); 'button_text' => 'Buy external product',
)
);
$product->save(); $product->save();
return wc_get_product( $product->get_id() ); return wc_get_product( $product->get_id() );
@ -78,10 +88,12 @@ class WC_Helper_Product {
$simple_product_1 = self::create_simple_product(); $simple_product_1 = self::create_simple_product();
$simple_product_2 = self::create_simple_product(); $simple_product_2 = self::create_simple_product();
$product = new WC_Product_Grouped(); $product = new WC_Product_Grouped();
$product->set_props( array( $product->set_props(
'name' => 'Dummy Grouped Product', array(
'sku' => 'DUMMY GROUPED SKU', 'name' => 'Dummy Grouped Product',
) ); 'sku' => 'DUMMY GROUPED SKU',
)
);
$product->set_children( array( $simple_product_1->get_id(), $simple_product_2->get_id() ) ); $product->set_children( array( $simple_product_1->get_id(), $simple_product_2->get_id() ) );
$product->save(); $product->save();
@ -97,10 +109,12 @@ class WC_Helper_Product {
*/ */
public static function create_variation_product() { public static function create_variation_product() {
$product = new WC_Product_Variable(); $product = new WC_Product_Variable();
$product->set_props( array( $product->set_props(
'name' => 'Dummy Variable Product', array(
'sku' => 'DUMMY VARIABLE SKU', 'name' => 'Dummy Variable Product',
) ); 'sku' => 'DUMMY VARIABLE SKU',
)
);
$attribute_data = self::create_attribute( 'size', array( 'small', 'large' ) ); // Create all attribute related things. $attribute_data = self::create_attribute( 'size', array( 'small', 'large' ) ); // Create all attribute related things.
$attributes = array(); $attributes = array();
@ -117,20 +131,24 @@ class WC_Helper_Product {
$product->save(); $product->save();
$variation_1 = new WC_Product_Variation(); $variation_1 = new WC_Product_Variation();
$variation_1->set_props( array( $variation_1->set_props(
'parent_id' => $product->get_id(), array(
'sku' => 'DUMMY SKU VARIABLE SMALL', 'parent_id' => $product->get_id(),
'regular_price' => 10, 'sku' => 'DUMMY SKU VARIABLE SMALL',
) ); 'regular_price' => 10,
)
);
$variation_1->set_attributes( array( 'pa_size' => 'small' ) ); $variation_1->set_attributes( array( 'pa_size' => 'small' ) );
$variation_1->save(); $variation_1->save();
$variation_2 = new WC_Product_Variation(); $variation_2 = new WC_Product_Variation();
$variation_2->set_props( array( $variation_2->set_props(
'parent_id' => $product->get_id(), array(
'sku' => 'DUMMY SKU VARIABLE LARGE', 'parent_id' => $product->get_id(),
'regular_price' => 15, 'sku' => 'DUMMY SKU VARIABLE LARGE',
) ); 'regular_price' => 15,
)
);
$variation_2->set_attributes( array( 'pa_size' => 'large' ) ); $variation_2->set_attributes( array( 'pa_size' => 'large' ) );
$variation_2->save(); $variation_2->save();
@ -142,56 +160,81 @@ class WC_Helper_Product {
* *
* @since 2.3 * @since 2.3
* *
* @param string $attribute_name Name of attribute to create. * @param string $raw_name Name of attribute to create.
* @param array(string) $terms Terms to create for the attribute. * @param array(string) $terms Terms to create for the attribute.
* @return array * @return array
*/ */
public static function create_attribute( $attribute_name = 'size', $terms = array( 'small' ) ) { public static function create_attribute( $raw_name = 'size', $terms = array( 'small' ) ) {
global $wpdb; global $wpdb, $wc_product_attributes;
$attribute = array(
'attribute_label' => $attribute_name,
'attribute_name' => $attribute_name,
'attribute_type' => 'select',
'attribute_orderby' => 'menu_order',
'attribute_public' => 0,
);
$wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute );
$return = array(
'attribute_name' => $attribute_name,
'attribute_taxonomy' => 'pa_' . $attribute_name,
'attribute_id' => $wpdb->insert_id,
'term_ids' => array(),
);
// Register the taxonomy.
$name = wc_attribute_taxonomy_name( $attribute_name );
$label = $attribute_name;
// Make sure caches are clean.
delete_transient( 'wc_attribute_taxonomies' ); delete_transient( 'wc_attribute_taxonomies' );
register_taxonomy( 'pa_' . $attribute_name, array( 'product' ), array( // These are exported as labels, so convert the label to a name if possible first.
'labels' => array( $attribute_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' );
'name' => $attribute_name, $attribute_name = array_search( $raw_name, $attribute_labels, true );
),
) );
// Set product attributes global. if ( ! $attribute_name ) {
global $wc_product_attributes; $attribute_name = wc_sanitize_taxonomy_name( $raw_name );
$wc_product_attributes = array(); }
foreach ( wc_get_attribute_taxonomies() as $tax ) {
$name = wc_attribute_taxonomy_name( $tax->attribute_name ); $attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name );
if ( $name ) {
$wc_product_attributes[ $name ] = $tax; if ( ! $attribute_id ) {
$taxonomy_name = wc_attribute_taxonomy_name( $attribute_name );
// Degister taxonomy which other tests may have created...
unregister_taxonomy( $taxonomy_name );
$attribute_id = wc_create_attribute(
array(
'name' => $raw_name,
'slug' => $attribute_name,
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => 0,
)
);
// Register as taxonomy.
register_taxonomy(
$taxonomy_name,
apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, array( 'product' ) ),
apply_filters(
'woocommerce_taxonomy_args_' . $taxonomy_name,
array(
'labels' => array(
'name' => $raw_name,
),
'hierarchical' => false,
'show_ui' => false,
'query_var' => true,
'rewrite' => false,
)
)
);
// Set product attributes global.
$wc_product_attributes = array();
foreach ( wc_get_attribute_taxonomies() as $taxonomy ) {
$wc_product_attributes[ wc_attribute_taxonomy_name( $taxonomy->attribute_name ) ] = $taxonomy;
} }
} }
$attribute = wc_get_attribute( $attribute_id );
$return = array(
'attribute_name' => $attribute->name,
'attribute_taxonomy' => $attribute->slug,
'attribute_id' => $attribute_id,
'term_ids' => array(),
);
foreach ( $terms as $term ) { foreach ( $terms as $term ) {
$result = term_exists( $term, 'pa_' . $attribute_name ); $result = term_exists( $term, $attribute->slug );
if ( ! $result ) { if ( ! $result ) {
$result = wp_insert_term( $term, 'pa_' . $attribute_name ); $result = wp_insert_term( $term, $attribute->slug );
$return['term_ids'][] = $result['term_id']; $return['term_ids'][] = $result['term_id'];
} else { } else {
$return['term_ids'][] = $result['term_id']; $return['term_ids'][] = $result['term_id'];
@ -204,7 +247,7 @@ class WC_Helper_Product {
/** /**
* Delete an attribute. * Delete an attribute.
* *
* @param $attribute_id * @param int $attribute_id ID to delete.
* *
* @since 2.3 * @since 2.3
*/ */
@ -222,9 +265,9 @@ class WC_Helper_Product {
* Creates a new product review on a specific product. * Creates a new product review on a specific product.
* *
* @since 3.0 * @since 3.0
* @param $product_id integer Product ID that the review is for * @param int $product_id integer Product ID that the review is for.
* @param $revieww_content string Content to use for the product review * @param string $review_content string Content to use for the product review.
* @return integer Product Review ID * @return integer Product Review ID.
*/ */
public static function create_product_review( $product_id, $review_content = 'Review content here' ) { public static function create_product_review( $product_id, $review_content = 'Review content here' ) {
$data = array( $data = array(
@ -243,6 +286,8 @@ class WC_Helper_Product {
/** /**
* A helper function for hooking into save_post during the test_product_meta_save_post test. * A helper function for hooking into save_post during the test_product_meta_save_post test.
* @since 3.0.1 * @since 3.0.1
*
* @param int $id ID to update.
*/ */
public static function save_post_test_update_meta_data_direct( $id ) { public static function save_post_test_update_meta_data_direct( $id ) {
update_post_meta( $id, '_test2', 'world' ); update_post_meta( $id, '_test2', 'world' );