Make create_attribute helper use core functions
This commit is contained in:
parent
845300c975
commit
db6a73d474
|
@ -1,4 +1,9 @@
|
|||
<?php
|
||||
/**
|
||||
* Product helpers.
|
||||
*
|
||||
* @package woocommerce/tests
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Helper_Product.
|
||||
|
@ -10,7 +15,7 @@ class WC_Helper_Product {
|
|||
/**
|
||||
* Delete a product.
|
||||
*
|
||||
* @param $product_id
|
||||
* @param int $product_id ID to delete.
|
||||
*/
|
||||
public static function delete_product( $product_id ) {
|
||||
$product = wc_get_product( $product_id );
|
||||
|
@ -23,11 +28,13 @@ class WC_Helper_Product {
|
|||
* Create simple product.
|
||||
*
|
||||
* @since 2.3
|
||||
* @param bool $save Save or return object.
|
||||
* @return WC_Product_Simple
|
||||
*/
|
||||
public static function create_simple_product( $save = true ) {
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_props( array(
|
||||
$product->set_props(
|
||||
array(
|
||||
'name' => 'Dummy Product',
|
||||
'regular_price' => 10,
|
||||
'price' => 10,
|
||||
|
@ -38,7 +45,8 @@ class WC_Helper_Product {
|
|||
'virtual' => false,
|
||||
'stock_status' => 'instock',
|
||||
'weight' => '1.1',
|
||||
) );
|
||||
)
|
||||
);
|
||||
|
||||
if ( $save ) {
|
||||
$product->save();
|
||||
|
@ -56,13 +64,15 @@ class WC_Helper_Product {
|
|||
*/
|
||||
public static function create_external_product() {
|
||||
$product = new WC_Product_External();
|
||||
$product->set_props( array(
|
||||
$product->set_props(
|
||||
array(
|
||||
'name' => 'Dummy External Product',
|
||||
'regular_price' => 10,
|
||||
'sku' => 'DUMMY EXTERNAL SKU',
|
||||
'product_url' => 'http://woocommerce.com',
|
||||
'button_text' => 'Buy external product',
|
||||
) );
|
||||
)
|
||||
);
|
||||
$product->save();
|
||||
|
||||
return wc_get_product( $product->get_id() );
|
||||
|
@ -78,10 +88,12 @@ class WC_Helper_Product {
|
|||
$simple_product_1 = self::create_simple_product();
|
||||
$simple_product_2 = self::create_simple_product();
|
||||
$product = new WC_Product_Grouped();
|
||||
$product->set_props( array(
|
||||
$product->set_props(
|
||||
array(
|
||||
'name' => 'Dummy Grouped Product',
|
||||
'sku' => 'DUMMY GROUPED SKU',
|
||||
) );
|
||||
)
|
||||
);
|
||||
$product->set_children( array( $simple_product_1->get_id(), $simple_product_2->get_id() ) );
|
||||
$product->save();
|
||||
|
||||
|
@ -97,10 +109,12 @@ class WC_Helper_Product {
|
|||
*/
|
||||
public static function create_variation_product() {
|
||||
$product = new WC_Product_Variable();
|
||||
$product->set_props( array(
|
||||
$product->set_props(
|
||||
array(
|
||||
'name' => 'Dummy Variable Product',
|
||||
'sku' => 'DUMMY VARIABLE SKU',
|
||||
) );
|
||||
)
|
||||
);
|
||||
|
||||
$attribute_data = self::create_attribute( 'size', array( 'small', 'large' ) ); // Create all attribute related things.
|
||||
$attributes = array();
|
||||
|
@ -117,20 +131,24 @@ class WC_Helper_Product {
|
|||
$product->save();
|
||||
|
||||
$variation_1 = new WC_Product_Variation();
|
||||
$variation_1->set_props( array(
|
||||
$variation_1->set_props(
|
||||
array(
|
||||
'parent_id' => $product->get_id(),
|
||||
'sku' => 'DUMMY SKU VARIABLE SMALL',
|
||||
'regular_price' => 10,
|
||||
) );
|
||||
)
|
||||
);
|
||||
$variation_1->set_attributes( array( 'pa_size' => 'small' ) );
|
||||
$variation_1->save();
|
||||
|
||||
$variation_2 = new WC_Product_Variation();
|
||||
$variation_2->set_props( array(
|
||||
$variation_2->set_props(
|
||||
array(
|
||||
'parent_id' => $product->get_id(),
|
||||
'sku' => 'DUMMY SKU VARIABLE LARGE',
|
||||
'regular_price' => 15,
|
||||
) );
|
||||
)
|
||||
);
|
||||
$variation_2->set_attributes( array( 'pa_size' => 'large' ) );
|
||||
$variation_2->save();
|
||||
|
||||
|
@ -142,56 +160,81 @@ class WC_Helper_Product {
|
|||
*
|
||||
* @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.
|
||||
* @return array
|
||||
*/
|
||||
public static function create_attribute( $attribute_name = 'size', $terms = array( 'small' ) ) {
|
||||
global $wpdb;
|
||||
public static function create_attribute( $raw_name = 'size', $terms = array( 'small' ) ) {
|
||||
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,
|
||||
// Make sure caches are clean.
|
||||
delete_transient( 'wc_attribute_taxonomies' );
|
||||
|
||||
// These are exported as labels, so convert the label to a name if possible first.
|
||||
$attribute_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' );
|
||||
$attribute_name = array_search( $raw_name, $attribute_labels, true );
|
||||
|
||||
if ( ! $attribute_name ) {
|
||||
$attribute_name = wc_sanitize_taxonomy_name( $raw_name );
|
||||
}
|
||||
|
||||
$attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name );
|
||||
|
||||
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,
|
||||
)
|
||||
);
|
||||
$wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute );
|
||||
|
||||
// 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' => 'pa_' . $attribute_name,
|
||||
'attribute_id' => $wpdb->insert_id,
|
||||
'attribute_name' => $attribute->name,
|
||||
'attribute_taxonomy' => $attribute->slug,
|
||||
'attribute_id' => $attribute_id,
|
||||
'term_ids' => array(),
|
||||
);
|
||||
|
||||
// Register the taxonomy.
|
||||
$name = wc_attribute_taxonomy_name( $attribute_name );
|
||||
$label = $attribute_name;
|
||||
|
||||
delete_transient( 'wc_attribute_taxonomies' );
|
||||
|
||||
register_taxonomy( 'pa_' . $attribute_name, array( 'product' ), array(
|
||||
'labels' => array(
|
||||
'name' => $attribute_name,
|
||||
),
|
||||
) );
|
||||
|
||||
// Set product attributes global.
|
||||
global $wc_product_attributes;
|
||||
$wc_product_attributes = array();
|
||||
foreach ( wc_get_attribute_taxonomies() as $tax ) {
|
||||
$name = wc_attribute_taxonomy_name( $tax->attribute_name );
|
||||
if ( $name ) {
|
||||
$wc_product_attributes[ $name ] = $tax;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $terms as $term ) {
|
||||
$result = term_exists( $term, 'pa_' . $attribute_name );
|
||||
$result = term_exists( $term, $attribute->slug );
|
||||
|
||||
if ( ! $result ) {
|
||||
$result = wp_insert_term( $term, 'pa_' . $attribute_name );
|
||||
$result = wp_insert_term( $term, $attribute->slug );
|
||||
$return['term_ids'][] = $result['term_id'];
|
||||
} else {
|
||||
$return['term_ids'][] = $result['term_id'];
|
||||
|
@ -204,7 +247,7 @@ class WC_Helper_Product {
|
|||
/**
|
||||
* Delete an attribute.
|
||||
*
|
||||
* @param $attribute_id
|
||||
* @param int $attribute_id ID to delete.
|
||||
*
|
||||
* @since 2.3
|
||||
*/
|
||||
|
@ -222,9 +265,9 @@ class WC_Helper_Product {
|
|||
* Creates a new product review on a specific product.
|
||||
*
|
||||
* @since 3.0
|
||||
* @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
|
||||
* @param int $product_id integer Product ID that the review is for.
|
||||
* @param string $review_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(
|
||||
|
@ -243,6 +286,8 @@ class WC_Helper_Product {
|
|||
/**
|
||||
* A helper function for hooking into save_post during the test_product_meta_save_post test.
|
||||
* @since 3.0.1
|
||||
*
|
||||
* @param int $id ID to update.
|
||||
*/
|
||||
public static function save_post_test_update_meta_data_direct( $id ) {
|
||||
update_post_meta( $id, '_test2', 'world' );
|
||||
|
|
Loading…
Reference in New Issue