Fix assigment order issues
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Unit_Test_Order_Factory {
|
||||
|
||||
|
||||
/**
|
||||
* @param array $args {
|
||||
* @type int $customer_id
|
||||
* }
|
||||
*
|
||||
* @return WC_Order|WP_Error
|
||||
*/
|
||||
public function create_new_order($args = array()) {
|
||||
|
||||
$customer_id = empty($args['customer_id']) ? get_current_user_id() : $args['customer_id'];
|
||||
|
||||
$order = wc_create_order(array(
|
||||
'customer_id' => $customer_id
|
||||
));
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WC_Order|int $order
|
||||
* @param int $customer_organization_id
|
||||
*/
|
||||
public function add_customer_organization_info($order, $customer_organization_id) {
|
||||
|
||||
if (is_numeric($order)) {
|
||||
|
||||
$order = wc_get_order($order);
|
||||
}
|
||||
|
||||
$order->add_meta_data('_wiaas_customer', $customer_organization_id);
|
||||
|
||||
$order->add_meta_data('_wiaas_customer_info', wiaas_get_organization_info($customer_organization_id));
|
||||
|
||||
$order->save_meta_data();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $args
|
||||
* @param WC_Order_Item_Product|null $bundle_item
|
||||
* @param WC_Order|null $order
|
||||
*
|
||||
* @return WC_Order_Item_Product
|
||||
*/
|
||||
public function create_new_simple_product_order_item($args = array(), $bundle_item = null, $order = null) {
|
||||
|
||||
$defaults = array(
|
||||
'quantity' => 1,
|
||||
'total' => 0
|
||||
);
|
||||
|
||||
$args = wp_parse_args($args, $defaults);
|
||||
|
||||
$item = new WC_Order_Item_Product();
|
||||
|
||||
$item->set_props(
|
||||
array(
|
||||
'quantity' => $args['quantity'],
|
||||
'total' => $args['total'],
|
||||
)
|
||||
);
|
||||
|
||||
if (! empty($args['category']) ) {
|
||||
|
||||
$item->add_meta_data('_wiaas_category', $args['category']);
|
||||
}
|
||||
|
||||
if (! empty($args['supplier_organization_id']) ) {
|
||||
|
||||
$item->add_meta_data('_wiaas_supplier_organization_id', $args['supplier_organization_id']);
|
||||
}
|
||||
|
||||
$item->set_backorder_meta();
|
||||
|
||||
if (! empty($order) ) {
|
||||
|
||||
$order->add_item($item);
|
||||
|
||||
$order->save();
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $args
|
||||
* @param WC_Order|null $order
|
||||
*
|
||||
* @return WC_Order_Item_Product
|
||||
*/
|
||||
public function create_new_bundle_order_item($args = array(), $order = null) {
|
||||
|
||||
$defaults = array(
|
||||
'quantity' => 1,
|
||||
'total' => 0
|
||||
);
|
||||
|
||||
$args = wp_parse_args($args, $defaults);
|
||||
|
||||
$item = new WC_Order_Item_Product();
|
||||
|
||||
$item->set_props(
|
||||
array(
|
||||
'quantity' => $args['quantity'],
|
||||
'total' => $args['total'],
|
||||
)
|
||||
);
|
||||
|
||||
$item->set_backorder_meta();
|
||||
|
||||
if (! empty($order) ) {
|
||||
|
||||
$order->add_item($item);
|
||||
|
||||
$order->save();
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WC_Order $order
|
||||
* @param array $items
|
||||
*/
|
||||
public function add_order_items($order, $items) {
|
||||
|
||||
foreach ( $items as $item ) {
|
||||
|
||||
$order->add_item($item);
|
||||
}
|
||||
|
||||
$order->save();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Unit_Test_Organization_Factory {
|
||||
|
||||
|
||||
/**
|
||||
* @param array $args {
|
||||
* @type string $name
|
||||
* @type int $parent_id
|
||||
* }
|
||||
*
|
||||
* @return int Organization ID
|
||||
*/
|
||||
public function create_new_organization($args = array()) {
|
||||
|
||||
$defaults = array(
|
||||
'name' => 'organization'
|
||||
);
|
||||
|
||||
$args = wp_parse_args($args, $defaults);
|
||||
|
||||
$term_args = array();
|
||||
|
||||
if (! empty($args['parent_id'])) {
|
||||
|
||||
$term_args['parent'] = $args['parent_id'];
|
||||
}
|
||||
|
||||
$result = wp_insert_term(
|
||||
$args['name'],
|
||||
Wiaas_User_Organization::TAXONOMY_NAME,
|
||||
$term_args
|
||||
);
|
||||
|
||||
if (! is_wp_error($result) ) {
|
||||
|
||||
return $result['term_id'];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function assign_user_to_organization($user_id, $organization_id) {
|
||||
wp_set_object_terms(
|
||||
$user_id,
|
||||
$organization_id,
|
||||
Wiaas_User_Organization::TAXONOMY_NAME);
|
||||
|
||||
update_user_meta($user_id, '_wiaas_organization_id', $organization_id);
|
||||
}
|
||||
|
||||
public function delete_organization($organization_id) {
|
||||
wp_delete_term(
|
||||
$organization_id,
|
||||
Wiaas_User_Organization::TAXONOMY_NAME);
|
||||
}
|
||||
|
||||
public function delete_organizations() {
|
||||
|
||||
$terms = get_terms(array(
|
||||
'taxonomy' => Wiaas_User_Organization::TAXONOMY_NAME,
|
||||
'hide_empty' => false
|
||||
));
|
||||
|
||||
foreach ($terms as $term) {
|
||||
|
||||
wp_delete_term($term->term_id, Wiaas_User_Organization::TAXONOMY_NAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Unit_Test_Product_Factory {
|
||||
|
||||
function __construct() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new simple product
|
||||
*
|
||||
* @param array $args {
|
||||
* @type int $price Simple product price
|
||||
* @type bool $is_recurring Flag indicating is simple product price is recurring
|
||||
* @type int $pay_period Pay period for simple product price if price is recurring
|
||||
* @type string|int $category Simple product category
|
||||
* @type string|int $country Simple product category term slug or id
|
||||
* @type string|int $supplier Simple product supplier term slug or id
|
||||
*
|
||||
* }
|
||||
*
|
||||
* @return WC_Product_Simple
|
||||
*/
|
||||
public function create_simple_product($args = array()) {
|
||||
|
||||
$defaults = array(
|
||||
'price' => 10,
|
||||
'is_recurring' => false,
|
||||
'pay_period' => 0
|
||||
);
|
||||
|
||||
$args = wp_parse_args($args, $defaults);
|
||||
|
||||
$post_id = wp_insert_post(array(
|
||||
'post_type' => 'product',
|
||||
'post_status' => 'publish',
|
||||
'post_name' => 'product',
|
||||
'post_title' => 'Product',
|
||||
'post_content' => 'Product',
|
||||
'post_excerpt' => 'Product'
|
||||
), true);
|
||||
|
||||
$product = new WC_Product_Simple($post_id);
|
||||
|
||||
$this->set_product_price($product, $args['price'], $args['is_recurring'], $args['pay_period']);
|
||||
|
||||
$product->save();
|
||||
|
||||
if (! empty($args['category']) ) {
|
||||
|
||||
$this->assign_product_to_category($product->get_id(), $args['category']);
|
||||
}
|
||||
|
||||
if (! empty($args['country']) ) {
|
||||
|
||||
$this->assign_product_to_country($product->get_id(), $args['country']);
|
||||
}
|
||||
|
||||
if (! empty($args['supplier']) ) {
|
||||
|
||||
$this->assign_product_to_supplier($product->get_id(), $args['supplier']);
|
||||
}
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WC_Product_Simple|int $product Product or Product ID
|
||||
* @param int $price
|
||||
* @param bool $is_recurring
|
||||
* @param int $pay_period
|
||||
*/
|
||||
public function set_product_price($product, $price = 10, $is_recurring = false, $pay_period = 0) {
|
||||
|
||||
if (is_numeric($product)) {
|
||||
|
||||
$product = wc_get_product($product);
|
||||
}
|
||||
|
||||
Wiaas_Product_Pricing::set_product_price($product, $price, $is_recurring, $pay_period);
|
||||
|
||||
$product->save();
|
||||
|
||||
delete_transient('wc_bundled_product_data');
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WC_Product_Simple|int $product Product or Product ID
|
||||
* @param string|int $category
|
||||
*/
|
||||
public function assign_product_to_category($product, $category) {
|
||||
|
||||
if (is_numeric($product)) {
|
||||
$product_id = $product;
|
||||
} else {
|
||||
$product_id = $product->get_id();
|
||||
}
|
||||
|
||||
wp_add_object_terms($product_id, $category, 'product_cat');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WC_Product_Simple|int $product Product or Product ID
|
||||
* @param string|int $country
|
||||
*/
|
||||
public function assign_product_to_country($product, $country) {
|
||||
|
||||
if (is_numeric($product)) {
|
||||
$product_id = $product;
|
||||
} else {
|
||||
$product_id = $product->get_id();
|
||||
}
|
||||
|
||||
wp_add_object_terms($product_id, $country, 'product_country');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WC_Product_Simple|int $product Product or Product ID
|
||||
* @param string|int $supplier
|
||||
*/
|
||||
public function assign_product_to_supplier($product, $supplier) {
|
||||
|
||||
if (is_numeric($product)) {
|
||||
$product_id = $product;
|
||||
} else {
|
||||
$product_id = $product->get_id();
|
||||
}
|
||||
|
||||
wp_add_object_terms($product_id, $supplier, 'supplier');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $args {
|
||||
* @type array $products Simple products
|
||||
* @type array $product_quantities Quantities for corresponding products from $products ( ex: [10, 5, 7] )
|
||||
* }
|
||||
*
|
||||
* @return WC_Product_Bundle
|
||||
*/
|
||||
public function create_product_bundle($args = array()) {
|
||||
|
||||
$post_id = wp_insert_post(array(
|
||||
'post_type' => 'product',
|
||||
'post_status' => 'publish',
|
||||
'post_name' => 'Bundle',
|
||||
'post_title' => 'Product Bundle',
|
||||
'post_content' => 'Product Bundle',
|
||||
'post_excerpt' => 'Product Bundle'
|
||||
), true);
|
||||
|
||||
$bundle = new WC_Product_Bundle($post_id);
|
||||
|
||||
if (! empty($args['products']) ) {
|
||||
|
||||
if ( empty($args['product_quantities']) ) {
|
||||
|
||||
$args['product_quantities'] = array_fill(0, count($args['products']), 1);
|
||||
}
|
||||
|
||||
if ( count($args['product_quantities']) < count($args['products'])) {
|
||||
|
||||
$args['product_quantities'] = array_pad($args['product_quantities'], count($args['products']), 1);
|
||||
}
|
||||
|
||||
$args['product_quantities'] = array_map('absint', $args['product_quantities']);
|
||||
|
||||
$bundled_data = array();
|
||||
|
||||
foreach ($args['products'] as $index => $product) {
|
||||
|
||||
$bundled_data[] = array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity_min' => $args['product_quantities'][$index],
|
||||
'quantity_max' => $args['product_quantities'][$index],
|
||||
'priced_individually' => true
|
||||
);
|
||||
}
|
||||
|
||||
$bundle->set_bundled_data_items($bundled_data);
|
||||
|
||||
$bundle->sync(true);
|
||||
}
|
||||
|
||||
$bundle->save();
|
||||
|
||||
if (! empty($args['country']) ) {
|
||||
|
||||
$this->assign_product_to_country($bundle->get_id(), $args['country']);
|
||||
}
|
||||
|
||||
return $bundle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WC_Product_Bundle $bundle
|
||||
* @param array $pricing_rules
|
||||
* @param int $commission
|
||||
* @param int $cost_margin
|
||||
*/
|
||||
public function set_product_bundle_prices($bundle, $pricing_rules = null, $commission = 50, $cost_margin = 0) {
|
||||
|
||||
if ( empty($pricing_rules) ) {
|
||||
|
||||
$pricing_rules = array(
|
||||
'purchase' => array(
|
||||
'minimal_fixed_price' => 500,
|
||||
'principal_amount' => 0,
|
||||
'minimal_services_price' => 0
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Wiaas_Package_Pricing::set_package_prices($bundle, $pricing_rules, $commission, $cost_margin);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user