woocommerce/includes/class-wc-ajax.php

2979 lines
94 KiB
PHP
Raw Normal View History

2013-11-14 12:13:34 +00:00
<?php
/**
2018-02-22 14:30:13 +00:00
* WooCommerce WC_AJAX. AJAX Event Handlers.
2013-11-14 12:13:34 +00:00
*
* @class WC_AJAX
* @package WooCommerce/Classes
2018-02-22 14:30:13 +00:00
*/
defined( 'ABSPATH' ) || exit;
/**
* WC_Ajax class.
2013-11-14 12:13:34 +00:00
*/
class WC_AJAX {
/**
2015-11-03 13:31:20 +00:00
* Hook in ajax handlers.
2013-11-14 12:13:34 +00:00
*/
public static function init() {
add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 );
add_action( 'template_redirect', array( __CLASS__, 'do_wc_ajax' ), 0 );
self::add_ajax_events();
}
/**
2015-11-03 13:31:20 +00:00
* Get WC Ajax Endpoint.
2016-11-24 11:50:58 +00:00
*
* @param string $request Optional.
*
* @return string
*/
public static function get_endpoint( $request = '' ) {
2018-06-22 12:32:21 +00:00
return esc_url_raw( apply_filters( 'woocommerce_ajax_get_endpoint', add_query_arg( 'wc-ajax', $request, remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart', 'order_again', '_wpnonce' ), home_url( '/', 'relative' ) ) ), $request ) );
}
2015-07-31 18:15:17 +00:00
/**
* Set WC AJAX constant and headers.
2015-07-31 18:15:17 +00:00
*/
public static function define_ajax() {
2019-02-15 17:09:45 +00:00
// phpcs:disable
2015-07-31 18:15:17 +00:00
if ( ! empty( $_GET['wc-ajax'] ) ) {
2016-11-24 14:20:58 +00:00
wc_maybe_define_constant( 'DOING_AJAX', true );
wc_maybe_define_constant( 'WC_DOING_AJAX', true );
if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) {
@ini_set( 'display_errors', 0 ); // Turn off display_errors during AJAX events to prevent malformed JSON.
}
$GLOBALS['wpdb']->hide_errors();
2015-07-31 18:15:17 +00:00
}
2019-02-15 17:09:45 +00:00
// phpcs:enable
2015-07-31 18:15:17 +00:00
}
/**
2016-11-24 11:50:58 +00:00
* Send headers for WC Ajax Requests.
*
* @since 2.5.0
*/
private static function wc_ajax_headers() {
2019-02-15 17:23:34 +00:00
if ( ! headers_sent() ) {
send_origin_headers();
send_nosniff_header();
wc_nocache_headers();
header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
header( 'X-Robots-Tag: noindex' );
status_header( 200 );
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
headers_sent( $file, $line );
trigger_error( "wc_ajax_headers cannot set headers - headers already sent by {$file} on line {$line}", E_USER_NOTICE ); // @codingStandardsIgnoreLine
}
}
/**
2015-11-03 13:31:20 +00:00
* Check for WC Ajax request and fire action.
*/
public static function do_wc_ajax() {
global $wp_query;
2019-02-15 17:09:45 +00:00
// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
2015-03-20 13:10:47 +00:00
if ( ! empty( $_GET['wc-ajax'] ) ) {
2018-02-22 15:37:01 +00:00
$wp_query->set( 'wc-ajax', sanitize_text_field( wp_unslash( $_GET['wc-ajax'] ) ) );
2015-03-20 13:10:47 +00:00
}
2018-02-22 15:37:01 +00:00
$action = $wp_query->get( 'wc-ajax' );
if ( $action ) {
self::wc_ajax_headers();
2018-05-02 13:28:53 +00:00
$action = sanitize_text_field( $action );
do_action( 'wc_ajax_' . $action );
wp_die();
2015-07-07 01:50:35 +00:00
}
2019-02-15 17:09:45 +00:00
// phpcs:enable
}
/**
2015-11-03 13:31:20 +00:00
* Hook in methods - uses WordPress ajax handlers (admin-ajax).
*/
public static function add_ajax_events() {
2019-02-15 17:09:45 +00:00
$ajax_events_nopriv = array(
'get_refreshed_fragments',
'apply_coupon',
'remove_coupon',
'update_shipping_method',
'get_cart_totals',
'update_order_review',
'add_to_cart',
'remove_from_cart',
'checkout',
'get_variation',
'get_customer_location',
2013-11-14 12:13:34 +00:00
);
2019-02-15 17:09:45 +00:00
foreach ( $ajax_events_nopriv as $ajax_event ) {
add_action( 'wp_ajax_woocommerce_' . $ajax_event, array( __CLASS__, $ajax_event ) );
2019-02-15 17:09:45 +00:00
add_action( 'wp_ajax_nopriv_woocommerce_' . $ajax_event, array( __CLASS__, $ajax_event ) );
2013-11-14 12:13:34 +00:00
2019-02-15 17:09:45 +00:00
// WC AJAX can be used for frontend ajax requests.
add_action( 'wc_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) );
}
2019-02-15 17:09:45 +00:00
$ajax_events = array(
'feature_product',
'mark_order_status',
'get_order_details',
'add_attribute',
'add_new_attribute',
'remove_variation',
'remove_variations',
'save_attributes',
'add_variation',
'link_all_variations',
'revoke_access_to_download',
'grant_access_to_download',
'get_customer_details',
'add_order_item',
'add_order_fee',
'add_order_shipping',
'add_order_tax',
'add_coupon_discount',
'remove_order_coupon',
'remove_order_item',
'remove_order_tax',
'reduce_order_item_stock',
'increase_order_item_stock',
'add_order_item_meta',
'remove_order_item_meta',
'calc_line_taxes',
'save_order_items',
'load_order_items',
'add_order_note',
'delete_order_note',
'json_search_products',
'json_search_products_and_variations',
'json_search_downloadable_products_and_variations',
'json_search_customers',
'json_search_categories',
'term_ordering',
'product_ordering',
'refund_line_items',
'delete_refund',
'rated',
'update_api_key',
'load_variations',
'save_variations',
'bulk_edit_variations',
'tax_rates_save_changes',
'shipping_zones_save_changes',
'shipping_zone_add_method',
'shipping_zone_methods_save_changes',
'shipping_zone_methods_save_settings',
'shipping_classes_save_changes',
'toggle_gateway_enabled',
);
foreach ( $ajax_events as $ajax_event ) {
add_action( 'wp_ajax_woocommerce_' . $ajax_event, array( __CLASS__, $ajax_event ) );
2013-11-14 12:13:34 +00:00
}
}
/**
2016-11-24 11:50:58 +00:00
* Get a refreshed cart fragment, including the mini cart HTML.
2013-11-14 12:13:34 +00:00
*/
public static function get_refreshed_fragments() {
2013-11-14 12:13:34 +00:00
ob_start();
woocommerce_mini_cart();
2013-11-14 12:13:34 +00:00
$mini_cart = ob_get_clean();
$data = array(
2018-02-22 15:37:01 +00:00
'fragments' => apply_filters(
2019-02-15 17:09:45 +00:00
'woocommerce_add_to_cart_fragments',
array(
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>',
2013-11-14 12:13:34 +00:00
)
),
'cart_hash' => WC()->cart->get_cart_hash(),
2013-11-14 12:13:34 +00:00
);
wp_send_json( $data );
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* AJAX apply coupon on checkout page.
2013-11-14 12:13:34 +00:00
*/
public static function apply_coupon() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'apply-coupon', 'security' );
if ( ! empty( $_POST['coupon_code'] ) ) {
WC()->cart->add_discount( wc_format_coupon_code( wp_unslash( $_POST['coupon_code'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2013-11-14 12:13:34 +00:00
} else {
wc_add_notice( WC_Coupon::get_generic_coupon_error( WC_Coupon::E_WC_COUPON_PLEASE_ENTER ), 'error' );
2013-11-14 12:13:34 +00:00
}
wc_print_notices();
wp_die();
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* AJAX remove coupon on cart and checkout page.
*/
public static function remove_coupon() {
check_ajax_referer( 'remove-coupon', 'security' );
$coupon = isset( $_POST['coupon'] ) ? wc_format_coupon_code( wp_unslash( $_POST['coupon'] ) ) : false; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( empty( $coupon ) ) {
wc_add_notice( __( 'Sorry there was a problem removing this coupon.', 'woocommerce' ), 'error' );
} else {
WC()->cart->remove_coupon( $coupon );
wc_add_notice( __( 'Coupon has been removed.', 'woocommerce' ) );
}
wc_print_notices();
wp_die();
}
2013-11-14 12:13:34 +00:00
/**
2015-11-03 13:31:20 +00:00
* AJAX update shipping method on cart page.
2013-11-14 12:13:34 +00:00
*/
public static function update_shipping_method() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'update-shipping-method', 'security' );
2016-11-24 14:20:58 +00:00
wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );
2013-11-14 12:13:34 +00:00
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
2019-02-15 17:09:45 +00:00
$posted_shipping_methods = isset( $_POST['shipping_method'] ) ? wc_clean( wp_unslash( $_POST['shipping_method'] ) ) : array();
2013-11-14 12:13:34 +00:00
2019-02-15 17:09:45 +00:00
if ( is_array( $posted_shipping_methods ) ) {
foreach ( $posted_shipping_methods as $i => $value ) {
$chosen_shipping_methods[ $i ] = $value;
2014-04-25 11:30:39 +00:00
}
}
2013-11-14 12:13:34 +00:00
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
2016-11-24 14:20:58 +00:00
self::get_cart_totals();
2013-11-14 12:13:34 +00:00
}
/**
* AJAX receive updated cart_totals div.
*/
public static function get_cart_totals() {
2016-11-24 14:20:58 +00:00
wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );
WC()->cart->calculate_totals();
woocommerce_cart_totals();
wp_die();
}
2013-11-14 12:13:34 +00:00
/**
2016-11-24 14:20:58 +00:00
* Session has expired.
2013-11-14 12:13:34 +00:00
*/
2016-11-24 14:20:58 +00:00
private static function update_order_review_expired() {
2018-02-22 15:37:01 +00:00
wp_send_json(
array(
'fragments' => apply_filters(
2019-02-15 17:09:45 +00:00
'woocommerce_update_order_review_fragments',
array(
2018-02-22 15:37:01 +00:00
'form.woocommerce-checkout' => '<div class="woocommerce-error">' . __( 'Sorry, your session has expired.', 'woocommerce' ) . ' <a href="' . esc_url( wc_get_page_permalink( 'shop' ) ) . '" class="wc-backward">' . __( 'Return to shop', 'woocommerce' ) . '</a></div>',
)
),
)
);
2016-11-24 14:20:58 +00:00
}
2013-11-14 12:13:34 +00:00
2016-11-24 14:20:58 +00:00
/**
2016-11-24 17:50:24 +00:00
* AJAX update order review on checkout.
2016-11-24 14:20:58 +00:00
*/
public static function update_order_review() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'update-order-review', 'security' );
2016-11-24 14:20:58 +00:00
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
2013-11-14 12:13:34 +00:00
if ( WC()->cart->is_empty() && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_update_order_review_expired', true ) ) {
2016-11-24 14:20:58 +00:00
self::update_order_review_expired();
2013-11-14 12:13:34 +00:00
}
2019-02-15 17:09:45 +00:00
do_action( 'woocommerce_checkout_update_order_review', isset( $_POST['post_data'] ) ? wp_unslash( $_POST['post_data'] ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2013-11-14 12:13:34 +00:00
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
2019-02-15 17:09:45 +00:00
$posted_shipping_methods = isset( $_POST['shipping_method'] ) ? wc_clean( wp_unslash( $_POST['shipping_method'] ) ) : array();
2013-11-14 12:13:34 +00:00
2019-02-15 17:09:45 +00:00
if ( is_array( $posted_shipping_methods ) ) {
foreach ( $posted_shipping_methods as $i => $value ) {
$chosen_shipping_methods[ $i ] = $value;
}
}
2013-11-14 12:13:34 +00:00
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
2019-02-15 17:09:45 +00:00
WC()->session->set( 'chosen_payment_method', empty( $_POST['payment_method'] ) ? '' : wc_clean( wp_unslash( $_POST['payment_method'] ) ) );
2018-02-22 15:37:01 +00:00
WC()->customer->set_props(
array(
2019-02-15 17:09:45 +00:00
'billing_country' => isset( $_POST['country'] ) ? wc_clean( wp_unslash( $_POST['country'] ) ) : null,
'billing_state' => isset( $_POST['state'] ) ? wc_clean( wp_unslash( $_POST['state'] ) ) : null,
'billing_postcode' => isset( $_POST['postcode'] ) ? wc_clean( wp_unslash( $_POST['postcode'] ) ) : null,
'billing_city' => isset( $_POST['city'] ) ? wc_clean( wp_unslash( $_POST['city'] ) ) : null,
'billing_address_1' => isset( $_POST['address'] ) ? wc_clean( wp_unslash( $_POST['address'] ) ) : null,
'billing_address_2' => isset( $_POST['address_2'] ) ? wc_clean( wp_unslash( $_POST['address_2'] ) ) : null,
2018-02-22 15:37:01 +00:00
)
);
2013-11-14 12:13:34 +00:00
if ( wc_ship_to_billing_address_only() ) {
2018-02-22 15:37:01 +00:00
WC()->customer->set_props(
array(
2019-02-15 17:09:45 +00:00
'shipping_country' => isset( $_POST['country'] ) ? wc_clean( wp_unslash( $_POST['country'] ) ) : null,
'shipping_state' => isset( $_POST['state'] ) ? wc_clean( wp_unslash( $_POST['state'] ) ) : null,
'shipping_postcode' => isset( $_POST['postcode'] ) ? wc_clean( wp_unslash( $_POST['postcode'] ) ) : null,
'shipping_city' => isset( $_POST['city'] ) ? wc_clean( wp_unslash( $_POST['city'] ) ) : null,
'shipping_address_1' => isset( $_POST['address'] ) ? wc_clean( wp_unslash( $_POST['address'] ) ) : null,
'shipping_address_2' => isset( $_POST['address_2'] ) ? wc_clean( wp_unslash( $_POST['address_2'] ) ) : null,
2018-02-22 15:37:01 +00:00
)
);
} else {
2018-02-22 15:37:01 +00:00
WC()->customer->set_props(
array(
2019-02-15 17:09:45 +00:00
'shipping_country' => isset( $_POST['s_country'] ) ? wc_clean( wp_unslash( $_POST['s_country'] ) ) : null,
'shipping_state' => isset( $_POST['s_state'] ) ? wc_clean( wp_unslash( $_POST['s_state'] ) ) : null,
'shipping_postcode' => isset( $_POST['s_postcode'] ) ? wc_clean( wp_unslash( $_POST['s_postcode'] ) ) : null,
'shipping_city' => isset( $_POST['s_city'] ) ? wc_clean( wp_unslash( $_POST['s_city'] ) ) : null,
'shipping_address_1' => isset( $_POST['s_address'] ) ? wc_clean( wp_unslash( $_POST['s_address'] ) ) : null,
'shipping_address_2' => isset( $_POST['s_address_2'] ) ? wc_clean( wp_unslash( $_POST['s_address_2'] ) ) : null,
2018-02-22 15:37:01 +00:00
)
);
}
2019-02-15 17:09:45 +00:00
if ( isset( $_POST['has_full_address'] ) && wc_string_to_bool( wc_clean( wp_unslash( $_POST['has_full_address'] ) ) ) ) {
WC()->customer->set_calculated_shipping( true );
} else {
WC()->customer->set_calculated_shipping( false );
}
2013-11-14 12:13:34 +00:00
WC()->customer->save();
2019-02-22 16:43:42 +00:00
2019-03-05 13:31:14 +00:00
// Calculate shipping before totals. This will ensure any shipping methods that affect things like taxes are chosen prior to final totals being calculated. Ref: #22708.
2019-02-22 16:43:42 +00:00
WC()->cart->calculate_shipping();
2013-11-14 12:13:34 +00:00
WC()->cart->calculate_totals();
// Get order review fragment.
ob_start();
woocommerce_order_review();
$woocommerce_order_review = ob_get_clean();
// Get checkout payment fragment.
ob_start();
woocommerce_checkout_payment();
$woocommerce_checkout_payment = ob_get_clean();
// Get messages if reload checkout is not true.
$reload_checkout = isset( WC()->session->reload_checkout ) ? true : false;
if ( ! $reload_checkout ) {
2018-04-19 17:27:29 +00:00
$messages = wc_print_notices( true );
} else {
$messages = '';
}
2016-11-24 14:20:58 +00:00
unset( WC()->session->refresh_totals, WC()->session->reload_checkout );
2018-02-22 15:37:01 +00:00
wp_send_json(
array(
'result' => empty( $messages ) ? 'success' : 'failure',
'messages' => $messages,
'reload' => $reload_checkout ? 'true' : 'false',
2018-02-22 15:37:01 +00:00
'fragments' => apply_filters(
'woocommerce_update_order_review_fragments',
array(
2018-02-22 15:37:01 +00:00
'.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
'.woocommerce-checkout-payment' => $woocommerce_checkout_payment,
)
),
)
);
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* AJAX add to cart.
2013-11-14 12:13:34 +00:00
*/
public static function add_to_cart() {
ob_start();
2019-02-15 17:09:45 +00:00
// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
if ( ! isset( $_POST['product_id'] ) ) {
return;
}
2013-11-14 12:13:34 +00:00
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
$product = wc_get_product( $product_id );
2019-02-15 17:09:45 +00:00
$quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_POST['quantity'] ) );
2013-11-14 12:13:34 +00:00
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
$product_status = get_post_status( $product_id );
$variation_id = 0;
$variation = array();
2013-11-14 12:13:34 +00:00
2018-01-03 21:40:51 +00:00
if ( $product && 'variation' === $product->get_type() ) {
$variation_id = $product_id;
$product_id = $product->get_parent_id();
$variation = $product->get_variation_attributes();
}
if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation ) && 'publish' === $product_status ) {
2013-11-14 12:13:34 +00:00
do_action( 'woocommerce_ajax_added_to_cart', $product_id );
2018-02-22 15:37:01 +00:00
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
2013-11-14 12:13:34 +00:00
}
self::get_refreshed_fragments();
2013-11-14 12:13:34 +00:00
} else {
2019-02-15 17:09:45 +00:00
// If there was an error adding to the cart, redirect to the product page to show any errors.
2013-11-14 12:13:34 +00:00
$data = array(
'error' => true,
'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id ),
2013-11-14 12:13:34 +00:00
);
wp_send_json( $data );
2013-11-14 12:13:34 +00:00
}
2019-02-15 17:09:45 +00:00
// phpcs:enable
2013-11-14 12:13:34 +00:00
}
/**
* AJAX remove from cart.
*/
public static function remove_from_cart() {
ob_start();
2019-02-15 17:09:45 +00:00
// phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
$cart_item_key = wc_clean( isset( $_POST['cart_item_key'] ) ? wp_unslash( $_POST['cart_item_key'] ) : '' );
if ( $cart_item_key && false !== WC()->cart->remove_cart_item( $cart_item_key ) ) {
self::get_refreshed_fragments();
} else {
wp_send_json_error();
}
}
2013-11-14 12:13:34 +00:00
/**
2015-11-03 13:31:20 +00:00
* Process ajax checkout form.
2013-11-14 12:13:34 +00:00
*/
public static function checkout() {
2016-11-24 14:20:58 +00:00
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
WC()->checkout()->process_checkout();
wp_die( 0 );
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Get a matching variation based on posted attributes.
*/
public static function get_variation() {
ob_start();
2019-02-15 17:09:45 +00:00
// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
if ( empty( $_POST['product_id'] ) ) {
wp_die();
}
$variable_product = wc_get_product( absint( $_POST['product_id'] ) );
if ( ! $variable_product ) {
wp_die();
}
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$data_store = WC_Data_Store::load( 'product' );
$variation_id = $data_store->find_matching_product_variation( $variable_product, wp_unslash( $_POST ) );
2016-11-24 15:31:05 +00:00
$variation = $variation_id ? $variable_product->get_available_variation( $variation_id ) : false;
wp_send_json( $variation );
2019-02-15 17:09:45 +00:00
// phpcs:enable
}
2016-11-24 11:50:58 +00:00
/**
* Locate user via AJAX.
*/
public static function get_customer_location() {
$location_hash = WC_Cache_Helper::geolocation_ajax_get_location_hash();
wp_send_json_success( array( 'hash' => $location_hash ) );
}
2013-11-14 12:13:34 +00:00
/**
* Toggle Featured status of a product from admin.
2013-11-14 12:13:34 +00:00
*/
public static function feature_product() {
2019-02-15 17:09:45 +00:00
if ( current_user_can( 'edit_products' ) && check_admin_referer( 'woocommerce-feature-product' ) && isset( $_GET['product_id'] ) ) {
2016-11-24 11:50:58 +00:00
$product = wc_get_product( absint( $_GET['product_id'] ) );
2013-11-14 12:13:34 +00:00
2016-11-24 11:50:58 +00:00
if ( $product ) {
$product->set_featured( ! $product->get_featured() );
$product->save();
2014-11-27 15:40:39 +00:00
}
}
2013-11-14 12:13:34 +00:00
wp_safe_redirect( wp_get_referer() ? remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'ids' ), wp_get_referer() ) : admin_url( 'edit.php?post_type=product' ) );
exit;
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Mark an order with a status.
2013-11-14 12:13:34 +00:00
*/
2014-11-19 12:13:24 +00:00
public static function mark_order_status() {
2019-02-15 17:09:45 +00:00
if ( current_user_can( 'edit_shop_orders' ) && check_admin_referer( 'woocommerce-mark-order-status' ) && isset( $_GET['status'], $_GET['order_id'] ) ) {
$status = sanitize_text_field( wp_unslash( $_GET['status'] ) );
$order = wc_get_order( absint( wp_unslash( $_GET['order_id'] ) ) );
2016-11-24 11:50:58 +00:00
if ( wc_is_order_status( 'wc-' . $status ) && $order ) {
// Initialize payment gateways in case order has hooked status transition actions.
2018-11-08 10:59:07 +00:00
WC()->payment_gateways();
$order->update_status( $status, '', true );
2017-01-03 23:08:37 +00:00
do_action( 'woocommerce_order_edit_status', $order->get_id(), $status );
2014-11-27 15:37:42 +00:00
}
}
2013-11-14 12:13:34 +00:00
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'edit.php?post_type=shop_order' ) );
exit;
2013-11-14 12:13:34 +00:00
}
/**
* Get order details.
*/
public static function get_order_details() {
check_admin_referer( 'woocommerce-preview-order', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_GET['order_id'] ) ) {
wp_die( -1 );
}
2017-11-20 22:43:37 +00:00
$order = wc_get_order( absint( $_GET['order_id'] ) ); // WPCS: sanitization ok.
2017-11-20 22:43:37 +00:00
if ( $order ) {
2018-02-22 15:37:01 +00:00
include_once 'admin/list-tables/class-wc-admin-list-table-orders.php';
wp_send_json_success( WC_Admin_List_Table_Orders::order_preview_get_order_details( $order ) );
}
wp_die();
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Add an attribute row.
*/
public static function add_attribute() {
ob_start();
check_ajax_referer( 'add-attribute', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_products' ) || ! isset( $_POST['taxonomy'], $_POST['i'] ) ) {
wp_die( -1 );
}
$i = absint( $_POST['i'] );
$metabox_class = array();
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$attribute = new WC_Product_Attribute();
2019-02-15 17:09:45 +00:00
$attribute->set_id( wc_attribute_taxonomy_id_by_name( sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) ) );
$attribute->set_name( sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) );
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$attribute->set_visible( apply_filters( 'woocommerce_attribute_default_visibility', 1 ) );
$attribute->set_variation( apply_filters( 'woocommerce_attribute_default_is_variation', 0 ) );
if ( $attribute->is_taxonomy() ) {
$metabox_class[] = 'taxonomy';
$metabox_class[] = $attribute->get_name();
}
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-product-attribute.php';
wp_die();
}
2013-11-14 12:13:34 +00:00
/**
2015-11-03 13:31:20 +00:00
* Add a new attribute via ajax function.
2013-11-14 12:13:34 +00:00
*/
public static function add_new_attribute() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'add-attribute', 'security' );
2019-02-15 17:09:45 +00:00
if ( current_user_can( 'manage_product_terms' ) && isset( $_POST['taxonomy'], $_POST['term'] ) ) {
$taxonomy = esc_attr( wp_unslash( $_POST['taxonomy'] ) ); // phpcs:ignore
$term = wc_clean( wp_unslash( $_POST['term'] ) );
2016-11-24 11:50:58 +00:00
if ( taxonomy_exists( $taxonomy ) ) {
$result = wp_insert_term( $term, $taxonomy );
if ( is_wp_error( $result ) ) {
2018-02-22 15:37:01 +00:00
wp_send_json(
array(
'error' => $result->get_error_message(),
)
);
2016-11-24 11:50:58 +00:00
} else {
$term = get_term_by( 'id', $result['term_id'], $taxonomy );
2018-02-22 15:37:01 +00:00
wp_send_json(
array(
'term_id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
)
);
2016-11-24 11:50:58 +00:00
}
2013-11-14 12:13:34 +00:00
}
}
wp_die( -1 );
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Delete variations via ajax function.
2013-11-14 12:13:34 +00:00
*/
public static function remove_variations() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'delete-variations', 'security' );
2019-02-15 17:09:45 +00:00
if ( current_user_can( 'edit_products' ) && isset( $_POST['variation_ids'] ) ) {
$variation_ids = array_map( 'absint', (array) wp_unslash( $_POST['variation_ids'] ) );
2016-11-24 11:50:58 +00:00
foreach ( $variation_ids as $variation_id ) {
if ( 'product_variation' === get_post_type( $variation_id ) ) {
$variation = wc_get_product( $variation_id );
2017-01-04 14:05:13 +00:00
$variation->delete( true );
2016-11-24 11:50:58 +00:00
}
}
2013-11-14 12:13:34 +00:00
}
wp_die( -1 );
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Save attributes via ajax.
2013-11-14 12:13:34 +00:00
*/
public static function save_attributes() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'save-attributes', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_products' ) || ! isset( $_POST['data'], $_POST['post_id'] ) ) {
wp_die( -1 );
}
2019-01-07 13:29:09 +00:00
$response = array();
2017-11-17 15:43:14 +00:00
try {
2019-02-15 17:09:45 +00:00
parse_str( wp_unslash( $_POST['data'] ), $data ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2017-11-17 15:43:14 +00:00
$attributes = WC_Meta_Box_Product_Data::prepare_attributes( $data );
2019-02-15 17:09:45 +00:00
$product_id = absint( wp_unslash( $_POST['post_id'] ) );
$product_type = ! empty( $_POST['product_type'] ) ? wc_clean( wp_unslash( $_POST['product_type'] ) ) : 'simple';
2017-11-17 15:43:14 +00:00
$classname = WC_Product_Factory::get_product_classname( $product_id, $product_type );
$product = new $classname( $product_id );
2013-11-14 12:13:34 +00:00
2017-11-17 15:43:14 +00:00
$product->set_attributes( $attributes );
$product->save();
ob_start();
$attributes = $product->get_attributes( 'edit' );
$i = -1;
2019-02-27 11:56:00 +00:00
if ( ! empty( $data['attribute_names'] ) ) {
foreach ( $data['attribute_names'] as $attribute_name ) {
$attribute = isset( $attributes[ sanitize_title( $attribute_name ) ] ) ? $attributes[ sanitize_title( $attribute_name ) ] : false;
if ( ! $attribute ) {
continue;
}
$i++;
$metabox_class = array();
2017-11-17 15:43:14 +00:00
if ( $attribute->is_taxonomy() ) {
$metabox_class[] = 'taxonomy';
$metabox_class[] = $attribute->get_name();
}
2017-11-17 15:43:14 +00:00
2019-03-05 13:31:14 +00:00
include 'admin/meta-boxes/views/html-product-attribute.php';
2017-11-17 15:43:14 +00:00
}
}
$response['html'] = ob_get_clean();
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
wp_send_json_success( $response );
2013-11-14 12:13:34 +00:00
}
/**
* Add variation via ajax function.
2013-11-14 12:13:34 +00:00
*/
public static function add_variation() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'add-variation', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_products' ) || ! isset( $_POST['post_id'], $_POST['loop'] ) ) {
wp_die( -1 );
}
global $post; // Set $post global so its available, like within the admin screens.
2013-11-14 12:13:34 +00:00
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$product_id = intval( $_POST['post_id'] );
2019-02-15 17:09:45 +00:00
$post = get_post( $product_id ); // phpcs:ignore
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$loop = intval( $_POST['loop'] );
$product_object = wc_get_product( $product_id );
$variation_object = new WC_Product_Variation();
$variation_object->set_parent_id( $product_id );
$variation_object->set_attributes( array_fill_keys( array_map( 'sanitize_title', array_keys( $product_object->get_variation_attributes() ) ), '' ) );
2018-02-22 15:37:01 +00:00
$variation_id = $variation_object->save();
$variation = get_post( $variation_id );
2019-01-21 16:02:28 +00:00
$variation_data = array_merge( get_post_custom( $variation_id ), wc_get_product_variation_attributes( $variation_id ) ); // kept for BW compatibility.
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-variation-admin.php';
wp_die();
2013-11-14 12:13:34 +00:00
}
/**
2016-11-24 15:31:05 +00:00
* Link all variations via ajax function.
2013-11-14 12:13:34 +00:00
*/
public static function link_all_variations() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'link-variations', 'security' );
if ( ! current_user_can( 'edit_products' ) ) {
wp_die( -1 );
}
2016-11-24 14:20:58 +00:00
wc_maybe_define_constant( 'WC_MAX_LINKED_VARIATIONS', 49 );
2016-06-06 15:55:27 +00:00
wc_set_time_limit( 0 );
2013-11-14 12:13:34 +00:00
2019-02-15 17:09:45 +00:00
$post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
2013-11-14 12:13:34 +00:00
if ( ! $post_id ) {
wp_die();
}
2013-11-14 12:13:34 +00:00
2016-11-24 14:20:58 +00:00
$product = wc_get_product( $post_id );
$attributes = wc_list_pluck( array_filter( $product->get_attributes(), 'wc_attributes_array_filter_variation' ), 'get_slugs' );
2013-11-14 12:13:34 +00:00
if ( ! empty( $attributes ) ) {
// Get existing variations so we don't create duplicates.
$existing_variations = array_map( 'wc_get_product', $product->get_children() );
$existing_attributes = array();
2013-11-14 12:13:34 +00:00
foreach ( $existing_variations as $existing_variation ) {
$existing_attributes[] = $existing_variation->get_attributes();
}
2013-11-14 12:13:34 +00:00
$added = 0;
$possible_attributes = array_reverse( wc_array_cartesian( $attributes ) );
2013-11-14 12:13:34 +00:00
foreach ( $possible_attributes as $possible_attribute ) {
2019-02-15 17:09:45 +00:00
if ( in_array( $possible_attribute, $existing_attributes, true ) ) {
continue;
}
$variation = new WC_Product_Variation();
$variation->set_parent_id( $post_id );
$variation->set_attributes( $possible_attribute );
2013-11-14 12:13:34 +00:00
do_action( 'product_variation_linked', $variation->save() );
2013-11-14 12:13:34 +00:00
if ( ( $added ++ ) > WC_MAX_LINKED_VARIATIONS ) {
break;
2016-11-24 14:20:58 +00:00
}
}
2016-12-08 14:28:51 +00:00
2019-02-15 17:09:45 +00:00
echo esc_html( $added );
2013-11-14 12:13:34 +00:00
}
$data_store = $product->get_data_store();
$data_store->sort_all_product_variations( $product->get_id() );
wp_die();
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Delete download permissions via ajax function.
2013-11-14 12:13:34 +00:00
*/
public static function revoke_access_to_download() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'revoke-access', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['download_id'], $_POST['product_id'], $_POST['order_id'], $_POST['permission_id'] ) ) {
wp_die( -1 );
}
2019-02-15 17:09:45 +00:00
$download_id = wc_clean( wp_unslash( $_POST['download_id'] ) );
$product_id = intval( $_POST['product_id'] );
$order_id = intval( $_POST['order_id'] );
$permission_id = absint( $_POST['permission_id'] );
2016-11-18 17:13:02 +00:00
$data_store = WC_Data_Store::load( 'customer-download' );
$data_store->delete_by_id( $permission_id );
2013-11-14 12:13:34 +00:00
do_action( 'woocommerce_ajax_revoke_access_to_product_download', $download_id, $product_id, $order_id, $permission_id );
2013-11-14 12:13:34 +00:00
wp_die();
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Grant download permissions via ajax function.
2013-11-14 12:13:34 +00:00
*/
public static function grant_access_to_download() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'grant-access', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['loop'], $_POST['order_id'], $_POST['product_ids'] ) ) {
wp_die( -1 );
}
2013-11-14 12:13:34 +00:00
global $wpdb;
$wpdb->hide_errors();
$order_id = intval( $_POST['order_id'] );
2019-02-15 17:09:45 +00:00
$product_ids = array_filter( array_map( 'absint', (array) wp_unslash( $_POST['product_ids'] ) ) );
$loop = intval( $_POST['loop'] );
$file_counter = 0;
$order = wc_get_order( $order_id );
2013-11-14 12:13:34 +00:00
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$files = $product->get_downloads();
2013-11-14 12:13:34 +00:00
2016-08-05 14:57:40 +00:00
if ( ! $order->get_billing_email() ) {
wp_die();
}
2013-11-14 12:13:34 +00:00
2016-06-06 18:39:23 +00:00
if ( ! empty( $files ) ) {
foreach ( $files as $download_id => $file ) {
2019-02-15 17:09:45 +00:00
$inserted_id = wc_downloadable_file_permission( $download_id, $product_id, $order );
if ( $inserted_id ) {
2016-11-18 17:13:02 +00:00
$download = new WC_Customer_Download( $inserted_id );
$loop ++;
$file_counter ++;
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
if ( $file->get_name() ) {
$file_count = $file->get_name();
} else {
/* translators: %d file count */
$file_count = sprintf( __( 'File %d', 'woocommerce' ), $file_counter );
}
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-download-permission.php';
}
2013-11-14 12:13:34 +00:00
}
}
}
wp_die();
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Get customer details via ajax.
2013-11-14 12:13:34 +00:00
*/
public static function get_customer_details() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'get-customer-details', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['user_id'] ) ) {
wp_die( -1 );
}
$user_id = absint( $_POST['user_id'] );
$customer = new WC_Customer( $user_id );
if ( has_filter( 'woocommerce_found_customer_details' ) ) {
wc_deprecated_function( 'The woocommerce_found_customer_details filter', '3.0', 'woocommerce_ajax_get_customer_details' );
}
2018-02-22 15:37:01 +00:00
$data = $customer->get_data();
$data['date_created'] = $data['date_created'] ? $data['date_created']->getTimestamp() : null;
$data['date_modified'] = $data['date_modified'] ? $data['date_modified']->getTimestamp() : null;
$customer_data = apply_filters( 'woocommerce_ajax_get_customer_details', $data, $customer, $user_id );
wp_send_json( $customer_data );
2013-11-14 12:13:34 +00:00
}
/**
2019-03-05 13:31:14 +00:00
* Add order item via ajax. Used on the edit order screen in WP Admin.
2019-02-15 17:09:45 +00:00
*
* @throws Exception If order is invalid.
2013-11-14 12:13:34 +00:00
*/
public static function add_order_item() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'order-item', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) ) {
wp_die( -1 );
}
2019-01-07 13:29:09 +00:00
$response = array();
2016-08-24 15:02:19 +00:00
try {
if ( ! isset( $_POST['order_id'] ) ) {
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
}
2019-01-03 12:23:49 +00:00
$order_id = absint( wp_unslash( $_POST['order_id'] ) ); // WPCS: input var ok.
$order = wc_get_order( $order_id );
2013-11-14 12:13:34 +00:00
if ( ! $order ) {
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
2016-08-24 15:02:19 +00:00
}
2013-11-14 12:13:34 +00:00
// If we passed through items it means we need to save first before adding a new one.
2019-02-15 17:09:45 +00:00
$items = ( ! empty( $_POST['items'] ) ) ? wp_unslash( $_POST['items'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! empty( $items ) ) {
$save_items = array();
parse_str( $items, $save_items );
wc_save_order_items( $order->get_id(), $save_items );
}
2019-02-15 17:09:45 +00:00
$items_to_add = isset( $_POST['data'] ) ? array_filter( wp_unslash( (array) $_POST['data'] ) ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
// Add items to order.
2019-01-03 12:23:49 +00:00
$order_notes = array();
foreach ( $items_to_add as $item ) {
if ( ! isset( $item['id'], $item['qty'] ) || empty( $item['id'] ) ) {
continue;
}
$product_id = absint( $item['id'] );
$qty = wc_stock_amount( $item['qty'] );
$product = wc_get_product( $product_id );
if ( ! $product ) {
throw new Exception( __( 'Invalid product ID', 'woocommerce' ) . ' ' . $product_id );
}
2019-01-03 12:23:49 +00:00
$item_id = $order->add_product( $product, $qty );
$item = apply_filters( 'woocommerce_ajax_order_item', $order->get_item( $item_id ), $item_id );
$added_items[ $item_id ] = $item;
2019-01-03 12:23:49 +00:00
$order_notes[ $item_id ] = $product->get_formatted_name();
if ( $product->managing_stock() ) {
$new_stock = wc_update_product_stock( $product, $qty, 'decrease' );
$order_notes[ $item_id ] = $product->get_formatted_name() . ' &ndash; ' . ( $new_stock + $qty ) . '&rarr;' . $new_stock;
$item->add_meta_data( '_reduced_stock', $qty, true );
$item->save();
}
2017-08-22 12:19:20 +00:00
do_action( 'woocommerce_ajax_add_order_item_meta', $item_id, $item, $order );
}
2016-08-25 13:22:27 +00:00
2019-01-03 12:23:49 +00:00
/* translators: %s item name. */
$order->add_order_note( sprintf( __( 'Added line items: %s', 'woocommerce' ), implode( ', ', $order_notes ) ), false, true );
do_action( 'woocommerce_ajax_order_items_added', $added_items, $order );
2017-08-22 12:19:20 +00:00
$data = get_post_meta( $order_id );
2019-01-03 12:23:49 +00:00
// Get HTML to return.
2017-08-22 12:19:20 +00:00
ob_start();
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
2019-01-03 12:23:49 +00:00
$items_html = ob_get_clean();
ob_start();
$notes = wc_get_order_notes( array( 'order_id' => $order_id ) );
include 'admin/meta-boxes/views/html-order-notes.php';
$notes_html = ob_get_clean();
2018-02-22 15:37:01 +00:00
wp_send_json_success(
array(
2019-01-03 12:23:49 +00:00
'html' => $items_html,
'notes_html' => $notes_html,
2018-02-22 15:37:01 +00:00
)
);
2016-08-25 13:22:27 +00:00
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
wp_send_json_success( $response );
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Add order fee via ajax.
2019-02-15 17:09:45 +00:00
*
* @throws Exception If order is invalid.
2013-11-14 12:13:34 +00:00
*/
public static function add_order_fee() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'order-item', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) ) {
wp_die( -1 );
}
2019-01-07 13:29:09 +00:00
$response = array();
2016-08-24 15:02:19 +00:00
try {
2019-02-15 17:09:45 +00:00
$order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0;
$order = wc_get_order( $order_id );
2017-08-23 10:22:18 +00:00
if ( ! $order ) {
2019-02-15 17:09:45 +00:00
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
2017-08-23 10:22:18 +00:00
}
2019-02-15 17:09:45 +00:00
$amount = isset( $_POST['amount'] ) ? wc_clean( wp_unslash( $_POST['amount'] ) ) : 0;
$calculate_tax_args = array(
2019-03-06 12:08:07 +00:00
'country' => isset( $_POST['country'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['country'] ) ) ) : '',
'state' => isset( $_POST['state'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['state'] ) ) ) : '',
'postcode' => isset( $_POST['postcode'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['postcode'] ) ) ) : '',
'city' => isset( $_POST['city'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['city'] ) ) ) : '',
2019-02-15 17:09:45 +00:00
);
2017-08-23 10:22:18 +00:00
if ( strstr( $amount, '%' ) ) {
$formatted_amount = $amount;
$percent = floatval( trim( $amount, '%' ) );
$amount = $order->get_total() * ( $percent / 100 );
} else {
$amount = floatval( $amount );
$formatted_amount = wc_price( $amount, array( 'currency' => $order->get_currency() ) );
}
$fee = new WC_Order_Item_Fee();
$fee->set_amount( $amount );
2017-08-23 10:22:18 +00:00
$fee->set_total( $amount );
/* translators: %s fee amount */
$fee->set_name( sprintf( __( '%s fee', 'woocommerce' ), wc_clean( $formatted_amount ) ) );
2017-08-23 10:22:18 +00:00
$order->add_item( $fee );
$order->calculate_taxes( $calculate_tax_args );
$order->calculate_totals( false );
2017-08-23 10:22:18 +00:00
$order->save();
2013-11-14 12:13:34 +00:00
2016-08-25 13:22:27 +00:00
ob_start();
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
$response['html'] = ob_get_clean();
2016-08-25 13:22:27 +00:00
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
wp_send_json_success( $response );
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Add order shipping cost via ajax.
2019-02-15 17:09:45 +00:00
*
* @throws Exception If order is invalid.
*/
public static function add_order_shipping() {
check_ajax_referer( 'order-item', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) ) {
wp_die( -1 );
}
2019-01-07 13:29:09 +00:00
$response = array();
2016-08-24 15:02:19 +00:00
try {
2019-02-15 17:09:45 +00:00
$order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0;
$order = wc_get_order( $order_id );
if ( ! $order ) {
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
}
2016-08-24 15:02:19 +00:00
$order_taxes = $order->get_taxes();
$shipping_methods = WC()->shipping() ? WC()->shipping()->load_shipping_methods() : array();
2019-02-15 17:09:45 +00:00
// Add new shipping.
2016-08-24 15:02:19 +00:00
$item = new WC_Order_Item_Shipping();
$item->set_shipping_rate( new WC_Shipping_Rate() );
$item->set_order_id( $order_id );
$item_id = $item->save();
2016-08-25 13:22:27 +00:00
ob_start();
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-shipping.php';
$response['html'] = ob_get_clean();
2016-08-25 13:22:27 +00:00
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
wp_send_json_success( $response );
}
2014-07-21 01:36:12 +00:00
/**
2015-11-03 13:31:20 +00:00
* Add order tax column via ajax.
2019-02-15 17:09:45 +00:00
*
2019-03-05 13:05:55 +00:00
* @throws Exception If order or tax rate is invalid.
2014-07-21 01:36:12 +00:00
*/
public static function add_order_tax() {
check_ajax_referer( 'order-item', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) ) {
wp_die( -1 );
}
2019-01-07 13:29:09 +00:00
$response = array();
2016-08-24 15:02:19 +00:00
try {
2019-02-15 17:09:45 +00:00
$order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0;
2016-08-24 15:02:19 +00:00
$order = wc_get_order( $order_id );
2019-02-15 17:09:45 +00:00
if ( ! $order ) {
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
}
$rate_id = isset( $_POST['rate_id'] ) ? absint( $_POST['rate_id'] ) : '';
if ( ! $rate_id ) {
throw new Exception( __( 'Invalid rate', 'woocommerce' ) );
}
$data = get_post_meta( $order_id );
// Add new tax.
2016-08-24 15:02:19 +00:00
$item = new WC_Order_Item_Tax();
$item->set_rate( $rate_id );
$item->set_order_id( $order_id );
$item->save();
2014-07-21 01:36:12 +00:00
2016-08-25 13:22:27 +00:00
ob_start();
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
$response['html'] = ob_get_clean();
2016-08-25 13:22:27 +00:00
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
wp_send_json_success( $response );
2014-07-21 01:36:12 +00:00
}
2017-08-09 13:54:30 +00:00
/**
* Add order discount via ajax.
2019-02-15 17:09:45 +00:00
*
2019-03-05 13:05:55 +00:00
* @throws Exception If order or coupon is invalid.
2017-08-09 13:54:30 +00:00
*/
2017-08-18 09:59:55 +00:00
public static function add_coupon_discount() {
2017-08-09 13:54:30 +00:00
check_ajax_referer( 'order-item', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) ) {
wp_die( -1 );
}
2019-01-07 13:29:09 +00:00
$response = array();
2017-08-09 13:54:30 +00:00
try {
2019-03-06 12:08:07 +00:00
$order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0;
$order = wc_get_order( $order_id );
$calculate_tax_args = array(
2019-03-06 12:08:07 +00:00
'country' => isset( $_POST['country'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['country'] ) ) ) : '',
'state' => isset( $_POST['state'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['state'] ) ) ) : '',
'postcode' => isset( $_POST['postcode'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['postcode'] ) ) ) : '',
'city' => isset( $_POST['city'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['city'] ) ) ) : '',
);
2019-02-15 17:09:45 +00:00
if ( ! $order ) {
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
}
if ( empty( $_POST['coupon'] ) ) {
throw new Exception( __( 'Invalid coupon', 'woocommerce' ) );
}
// Add user ID so validation for coupon limits works.
$user_id_arg = isset( $_POST['user_id'] ) ? absint( $_POST['user_id'] ) : 0;
if ( $user_id_arg ) {
$order->set_customer_id( $user_id_arg );
}
2019-03-06 11:46:37 +00:00
$result = $order->apply_coupon( wc_format_coupon_code( wp_unslash( $_POST['coupon'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2017-08-09 13:54:30 +00:00
if ( is_wp_error( $result ) ) {
throw new Exception( html_entity_decode( wp_strip_all_tags( $result->get_error_message() ) ) );
}
2017-08-09 13:54:30 +00:00
$order->calculate_taxes( $calculate_tax_args );
$order->calculate_totals( false );
2017-08-09 13:54:30 +00:00
ob_start();
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
$response['html'] = ob_get_clean();
2017-08-09 13:54:30 +00:00
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
wp_send_json_success( $response );
2017-08-09 13:54:30 +00:00
}
2017-08-09 21:55:35 +00:00
/**
* Remove coupon from an order via ajax.
2019-02-15 17:09:45 +00:00
*
2019-03-05 13:05:55 +00:00
* @throws Exception If order or coupon is invalid.
2017-08-09 21:55:35 +00:00
*/
public static function remove_order_coupon() {
check_ajax_referer( 'order-item', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) ) {
wp_die( -1 );
}
2019-01-07 13:29:09 +00:00
$response = array();
2017-08-09 21:55:35 +00:00
try {
2019-03-06 12:08:07 +00:00
$order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0;
$order = wc_get_order( $order_id );
$calculate_tax_args = array(
2019-03-06 12:08:07 +00:00
'country' => isset( $_POST['country'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['country'] ) ) ) : '',
'state' => isset( $_POST['state'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['state'] ) ) ) : '',
'postcode' => isset( $_POST['postcode'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['postcode'] ) ) ) : '',
'city' => isset( $_POST['city'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['city'] ) ) ) : '',
);
2017-08-09 21:55:35 +00:00
2019-02-15 17:09:45 +00:00
if ( ! $order ) {
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
}
if ( empty( $_POST['coupon'] ) ) {
throw new Exception( __( 'Invalid coupon', 'woocommerce' ) );
}
2019-03-06 11:46:37 +00:00
$order->remove_coupon( wc_format_coupon_code( wp_unslash( $_POST['coupon'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$order->calculate_taxes( $calculate_tax_args );
$order->calculate_totals( false );
2017-08-09 21:55:35 +00:00
ob_start();
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
$response['html'] = ob_get_clean();
2017-08-09 21:55:35 +00:00
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
wp_send_json_success( $response );
2017-08-09 21:55:35 +00:00
}
2013-11-14 12:13:34 +00:00
/**
2015-11-03 13:31:20 +00:00
* Remove an order item.
2019-02-15 17:09:45 +00:00
*
* @throws Exception If order is invalid.
2013-11-14 12:13:34 +00:00
*/
public static function remove_order_item() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'order-item', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['order_id'], $_POST['order_item_ids'] ) ) {
wp_die( -1 );
}
2019-01-07 13:29:09 +00:00
$response = array();
2017-08-10 15:32:24 +00:00
try {
2019-01-03 12:23:49 +00:00
$order_id = absint( $_POST['order_id'] );
$order = wc_get_order( $order_id );
if ( ! $order ) {
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
}
if ( ! isset( $_POST['order_item_ids'] ) ) {
throw new Exception( __( 'Invalid items', 'woocommerce' ) );
}
2019-02-15 17:09:45 +00:00
$order_item_ids = wp_unslash( $_POST['order_item_ids'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$items = ( ! empty( $_POST['items'] ) ) ? wp_unslash( $_POST['items'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$calculate_tax_args = array(
2019-03-06 12:08:07 +00:00
'country' => isset( $_POST['country'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['country'] ) ) ) : '',
'state' => isset( $_POST['state'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['state'] ) ) ) : '',
'postcode' => isset( $_POST['postcode'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['postcode'] ) ) ) : '',
'city' => isset( $_POST['city'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['city'] ) ) ) : '',
);
2013-11-14 12:13:34 +00:00
2017-08-10 15:32:24 +00:00
if ( ! is_array( $order_item_ids ) && is_numeric( $order_item_ids ) ) {
$order_item_ids = array( $order_item_ids );
}
// If we passed through items it means we need to save first before deleting.
if ( ! empty( $items ) ) {
$save_items = array();
parse_str( $items, $save_items );
2019-01-03 12:23:49 +00:00
wc_save_order_items( $order->get_id(), $save_items );
}
2019-01-03 12:23:49 +00:00
if ( ! empty( $order_item_ids ) ) {
$order_notes = array();
foreach ( $order_item_ids as $item_id ) {
2019-03-05 13:31:14 +00:00
$item_id = absint( $item_id );
$item = $order->get_item( $item_id );
2019-01-03 12:23:49 +00:00
// Before deleting the item, adjust any stock values already reduced.
if ( $item->is_type( 'line_item' ) ) {
2019-01-03 13:40:48 +00:00
$changed_stock = wc_maybe_adjust_line_item_product_stock( $item, 0 );
2019-01-03 12:23:49 +00:00
2019-01-03 13:40:48 +00:00
if ( $changed_stock && ! is_wp_error( $changed_stock ) ) {
2019-01-03 12:23:49 +00:00
/* translators: %1$s: item name %2$s: stock change */
2019-01-03 13:40:48 +00:00
$order->add_order_note( sprintf( __( 'Deleted %1$s and adjusted stock (%2$s)', 'woocommerce' ), $item->get_name(), $changed_stock['from'] . '&rarr;' . $changed_stock['to'] ), false, true );
2019-01-03 12:23:49 +00:00
} else {
/* translators: %s item name. */
$order->add_order_note( sprintf( __( 'Deleted %s', 'woocommerce' ), $item->get_name() ), false, true );
}
}
wc_delete_order_item( $item_id );
2017-08-10 15:32:24 +00:00
}
2013-11-14 12:13:34 +00:00
}
2017-08-10 15:32:24 +00:00
$order = wc_get_order( $order_id );
$order->calculate_taxes( $calculate_tax_args );
$order->calculate_totals( false );
2017-08-10 15:32:24 +00:00
2019-01-03 12:23:49 +00:00
// Get HTML to return.
2017-08-10 15:32:24 +00:00
ob_start();
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
2019-01-03 12:23:49 +00:00
$items_html = ob_get_clean();
ob_start();
$notes = wc_get_order_notes( array( 'order_id' => $order_id ) );
include 'admin/meta-boxes/views/html-order-notes.php';
$notes_html = ob_get_clean();
2017-08-10 15:32:24 +00:00
2018-02-22 15:37:01 +00:00
wp_send_json_success(
array(
2019-01-03 12:23:49 +00:00
'html' => $items_html,
'notes_html' => $notes_html,
2018-02-22 15:37:01 +00:00
)
);
2017-08-10 15:32:24 +00:00
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
2013-11-14 12:13:34 +00:00
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
wp_send_json_success( $response );
2013-11-14 12:13:34 +00:00
}
2014-07-21 01:57:23 +00:00
/**
2015-11-03 13:31:20 +00:00
* Remove an order tax.
2019-02-15 17:09:45 +00:00
*
2019-03-05 13:05:55 +00:00
* @throws Exception If there is an error whilst deleting the rate.
2014-07-21 01:57:23 +00:00
*/
2014-08-27 20:03:49 +00:00
public static function remove_order_tax() {
2014-07-21 01:57:23 +00:00
check_ajax_referer( 'order-item', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['order_id'], $_POST['rate_id'] ) ) {
wp_die( -1 );
}
2019-01-07 13:29:09 +00:00
$response = array();
2017-08-10 15:32:24 +00:00
try {
$order_id = absint( $_POST['order_id'] );
$rate_id = absint( $_POST['rate_id'] );
2014-07-21 01:57:23 +00:00
2017-08-10 15:32:24 +00:00
wc_delete_order_item( $rate_id );
2014-07-21 01:57:23 +00:00
2017-08-10 15:32:24 +00:00
$order = wc_get_order( $order_id );
2017-08-15 13:51:12 +00:00
$order->calculate_totals( false );
2017-08-10 15:32:24 +00:00
ob_start();
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
$response['html'] = ob_get_clean();
2017-08-10 15:32:24 +00:00
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
wp_send_json_success( $response );
2014-07-21 01:57:23 +00:00
}
2013-11-14 12:13:34 +00:00
/**
2015-11-03 13:31:20 +00:00
* Calc line tax.
2013-11-14 12:13:34 +00:00
*/
public static function calc_line_taxes() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'calc-totals', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['order_id'], $_POST['items'] ) ) {
wp_die( -1 );
}
2016-08-22 15:48:19 +00:00
$order_id = absint( $_POST['order_id'] );
$calculate_tax_args = array(
2019-03-06 12:08:07 +00:00
'country' => isset( $_POST['country'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['country'] ) ) ) : '',
'state' => isset( $_POST['state'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['state'] ) ) ) : '',
'postcode' => isset( $_POST['postcode'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['postcode'] ) ) ) : '',
'city' => isset( $_POST['city'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['city'] ) ) ) : '',
2016-08-22 15:48:19 +00:00
);
2019-02-15 17:09:45 +00:00
// Parse the jQuery serialized items.
2016-08-22 15:48:19 +00:00
$items = array();
2019-02-15 17:09:45 +00:00
parse_str( wp_unslash( $_POST['items'] ), $items ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2014-07-24 21:09:13 +00:00
2019-02-15 17:09:45 +00:00
// Save order items first.
2014-07-24 20:33:26 +00:00
wc_save_order_items( $order_id, $items );
2013-11-14 12:13:34 +00:00
2019-02-15 17:09:45 +00:00
// Grab the order and recalculate taxes.
2016-08-22 15:48:19 +00:00
$order = wc_get_order( $order_id );
$order->calculate_taxes( $calculate_tax_args );
$order->calculate_totals( false );
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
wp_die();
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Save order items via ajax.
*/
public static function save_order_items() {
check_ajax_referer( 'order-item', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['order_id'], $_POST['items'] ) ) {
wp_die( -1 );
}
2016-11-24 15:31:05 +00:00
if ( isset( $_POST['order_id'], $_POST['items'] ) ) {
$order_id = absint( $_POST['order_id'] );
2019-02-15 17:09:45 +00:00
// Parse the jQuery serialized items.
$items = array();
2019-02-15 17:09:45 +00:00
parse_str( wp_unslash( $_POST['items'] ), $items ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2019-02-15 17:09:45 +00:00
// Save order items.
wc_save_order_items( $order_id, $items );
2019-02-15 17:09:45 +00:00
// Return HTML items.
$order = wc_get_order( $order_id );
2019-01-03 13:40:48 +00:00
// Get HTML to return.
ob_start();
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
2019-01-03 13:40:48 +00:00
$items_html = ob_get_clean();
ob_start();
$notes = wc_get_order_notes( array( 'order_id' => $order_id ) );
include 'admin/meta-boxes/views/html-order-notes.php';
$notes_html = ob_get_clean();
wp_send_json_success(
array(
'html' => $items_html,
'notes_html' => $notes_html,
)
);
}
wp_die();
}
/**
2015-11-03 13:31:20 +00:00
* Load order items via ajax.
*/
public static function load_order_items() {
check_ajax_referer( 'order-item', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['order_id'] ) ) {
wp_die( -1 );
}
2019-02-15 17:09:45 +00:00
// Return HTML items.
$order_id = absint( $_POST['order_id'] );
$order = wc_get_order( $order_id );
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-order-items.php';
wp_die();
}
2013-11-14 12:13:34 +00:00
/**
2015-11-03 13:31:20 +00:00
* Add order note via ajax.
2013-11-14 12:13:34 +00:00
*/
public static function add_order_note() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'add-order-note', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['post_id'], $_POST['note'], $_POST['note_type'] ) ) {
wp_die( -1 );
}
2015-03-03 16:36:47 +00:00
$post_id = absint( $_POST['post_id'] );
2019-02-15 17:09:45 +00:00
$note = wp_kses_post( trim( wp_unslash( $_POST['note'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$note_type = wc_clean( wp_unslash( $_POST['note_type'] ) );
2013-11-14 12:13:34 +00:00
2016-09-07 22:32:24 +00:00
$is_customer_note = ( 'customer' === $note_type ) ? 1 : 0;
2013-11-14 12:13:34 +00:00
if ( $post_id > 0 ) {
$order = wc_get_order( $post_id );
$comment_id = $order->add_order_note( $note, $is_customer_note, true );
$note = wc_get_order_note( $comment_id );
2013-11-14 12:13:34 +00:00
$note_classes = array( 'note' );
$note_classes[] = $is_customer_note ? 'customer-note' : '';
$note_classes = apply_filters( 'woocommerce_order_note_class', array_filter( $note_classes ), $note );
?>
<li rel="<?php echo absint( $note->id ); ?>" class="<?php echo esc_attr( implode( ' ', $note_classes ) ); ?>">
<div class="note_content">
2019-02-15 17:09:45 +00:00
<?php echo wp_kses_post( wpautop( wptexturize( make_clickable( $note->content ) ) ) ); ?>
</div>
<p class="meta">
2019-02-15 17:09:45 +00:00
<abbr class="exact-date" title="<?php echo esc_attr( $note->date_created->date( 'y-m-d h:i:s' ) ); ?>">
<?php
/* translators: $1: Date created, $2 Time created */
2019-02-15 17:09:45 +00:00
printf( esc_html__( 'added on %1$s at %2$s', 'woocommerce' ), esc_html( $note->date_created->date_i18n( wc_date_format() ) ), esc_html( $note->date_created->date_i18n( wc_time_format() ) ) );
?>
</abbr>
<?php
if ( 'system' !== $note->added_by ) :
/* translators: %s: note author */
2019-02-15 17:09:45 +00:00
printf( ' ' . esc_html__( 'by %s', 'woocommerce' ), esc_html( $note->added_by ) );
endif;
?>
2019-02-15 17:09:45 +00:00
<a href="#" class="delete_note" role="button"><?php esc_html_e( 'Delete note', 'woocommerce' ); ?></a>
</p>
</li>
<?php
2013-11-14 12:13:34 +00:00
}
wp_die();
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Delete order note via ajax.
2013-11-14 12:13:34 +00:00
*/
public static function delete_order_note() {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'delete-order-note', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['note_id'] ) ) {
wp_die( -1 );
}
$note_id = (int) $_POST['note_id'];
2013-11-14 12:13:34 +00:00
if ( $note_id > 0 ) {
wc_delete_order_note( $note_id );
}
wp_die();
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Search for products and echo json.
2013-11-14 12:13:34 +00:00
*
2019-02-15 17:09:45 +00:00
* @param string $term (default: '') Term to search for.
* @param bool $include_variations in search or not.
2013-11-14 12:13:34 +00:00
*/
2016-11-24 11:50:58 +00:00
public static function json_search_products( $term = '', $include_variations = false ) {
2013-11-14 12:13:34 +00:00
check_ajax_referer( 'search-products', 'security' );
if ( empty( $term ) && isset( $_GET['term'] ) ) {
$term = (string) wc_clean( wp_unslash( $_GET['term'] ) );
}
2013-11-14 12:13:34 +00:00
if ( empty( $term ) ) {
wp_die();
}
2013-11-14 12:13:34 +00:00
2018-06-09 16:22:26 +00:00
if ( ! empty( $_GET['limit'] ) ) {
$limit = absint( $_GET['limit'] );
} else {
$limit = absint( apply_filters( 'woocommerce_json_search_limit', 30 ) );
}
2016-11-24 11:50:58 +00:00
$data_store = WC_Data_Store::load( 'product' );
2018-06-09 16:22:26 +00:00
$ids = $data_store->search_products( $term, '', (bool) $include_variations, false, $limit );
2013-11-14 12:13:34 +00:00
if ( ! empty( $_GET['exclude'] ) ) {
2019-02-15 17:09:45 +00:00
$ids = array_diff( $ids, array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) );
2013-11-14 12:13:34 +00:00
}
if ( ! empty( $_GET['include'] ) ) {
2019-02-15 17:09:45 +00:00
$ids = array_intersect( $ids, array_map( 'absint', (array) wp_unslash( $_GET['include'] ) ) );
}
$product_objects = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_readable' );
2016-11-24 11:50:58 +00:00
$products = array();
2016-11-24 11:50:58 +00:00
foreach ( $product_objects as $product_object ) {
$formatted_name = $product_object->get_formatted_name();
$managing_stock = $product_object->managing_stock();
2018-06-15 14:11:07 +00:00
if ( $managing_stock && ! empty( $_GET['display_stock'] ) ) {
2019-03-12 14:48:05 +00:00
$stock_amount = $product_object->get_stock_quantity();
/* Translators: %d stock amount */
$formatted_name .= ' &ndash; ' . sprintf( __( 'Stock: %d', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product_object ) );
}
$products[ $product_object->get_id() ] = rawurldecode( $formatted_name );
2013-11-14 12:13:34 +00:00
}
2016-11-24 11:50:58 +00:00
wp_send_json( apply_filters( 'woocommerce_json_search_found_products', $products ) );
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Search for product variations and return json.
2013-11-14 12:13:34 +00:00
*
* @see WC_AJAX::json_search_products()
*/
public static function json_search_products_and_variations() {
2016-11-24 11:50:58 +00:00
self::json_search_products( '', true );
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Search for downloadable product variations and return json.
*
* @see WC_AJAX::json_search_products()
*/
public static function json_search_downloadable_products_and_variations() {
check_ajax_referer( 'search-products', 'security' );
2019-02-15 17:09:45 +00:00
$term = isset( $_GET['term'] ) ? (string) wc_clean( wp_unslash( $_GET['term'] ) ) : '';
2016-11-24 11:50:58 +00:00
$data_store = WC_Data_Store::load( 'product' );
$ids = $data_store->search_products( $term, 'downloadable', true );
if ( ! empty( $_GET['exclude'] ) ) {
2019-02-15 17:09:45 +00:00
$ids = array_diff( $ids, array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) );
}
2016-11-24 11:50:58 +00:00
if ( ! empty( $_GET['include'] ) ) {
2019-02-15 17:09:45 +00:00
$ids = array_intersect( $ids, array_map( 'absint', (array) wp_unslash( $_GET['include'] ) ) );
2016-11-24 11:50:58 +00:00
}
2016-11-24 11:50:58 +00:00
if ( ! empty( $_GET['limit'] ) ) {
$ids = array_slice( $ids, 0, absint( $_GET['limit'] ) );
}
$product_objects = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_readable' );
2016-11-24 11:50:58 +00:00
$products = array();
2016-11-24 11:50:58 +00:00
foreach ( $product_objects as $product_object ) {
$products[ $product_object->get_id() ] = rawurldecode( $product_object->get_formatted_name() );
}
2016-11-24 11:50:58 +00:00
wp_send_json( $products );
}
/**
2015-11-03 13:31:20 +00:00
* Search for customers and return json.
*/
public static function json_search_customers() {
ob_start();
check_ajax_referer( 'search-customers', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) ) {
wp_die( -1 );
}
2019-02-15 17:09:45 +00:00
$term = isset( $_GET['term'] ) ? (string) wc_clean( wp_unslash( $_GET['term'] ) ) : '';
$limit = 0;
if ( empty( $term ) ) {
wp_die();
}
$ids = array();
// Search by ID.
if ( is_numeric( $term ) ) {
$customer = new WC_Customer( intval( $term ) );
// Customer does not exists.
if ( 0 !== $customer->get_id() ) {
$ids = array( $customer->get_id() );
}
}
// Usernames can be numeric so we first check that no users was found by ID before searching for numeric username, this prevents performance issues with ID lookups.
if ( empty( $ids ) ) {
$data_store = WC_Data_Store::load( 'customer' );
// If search is smaller than 3 characters, limit result set to avoid
// too many rows being returned.
if ( 3 > strlen( $term ) ) {
$limit = 20;
}
$ids = $data_store->search_customers( $term, $limit );
}
2018-01-19 07:57:27 +00:00
$found_customers = array();
2016-11-24 15:31:05 +00:00
if ( ! empty( $_GET['exclude'] ) ) {
2019-02-15 17:09:45 +00:00
$ids = array_diff( $ids, array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) );
}
2016-11-24 15:31:05 +00:00
foreach ( $ids as $id ) {
$customer = new WC_Customer( $id );
/* translators: 1: user display name 2: user ID 3: user email */
$found_customers[ $id ] = sprintf(
/* translators: $1: customer name, $2 customer id, $3: customer email */
2016-11-24 15:31:05 +00:00
esc_html__( '%1$s (#%2$s &ndash; %3$s)', 'woocommerce' ),
$customer->get_first_name() . ' ' . $customer->get_last_name(),
$customer->get_id(),
$customer->get_email()
);
}
2013-11-14 12:13:34 +00:00
2016-11-24 15:31:05 +00:00
wp_send_json( apply_filters( 'woocommerce_json_search_found_customers', $found_customers ) );
2013-11-14 12:13:34 +00:00
}
/**
* Search for categories and return json.
*/
public static function json_search_categories() {
ob_start();
check_ajax_referer( 'search-categories', 'security' );
if ( ! current_user_can( 'edit_products' ) ) {
wp_die( -1 );
}
2019-02-15 17:09:45 +00:00
$search_text = isset( $_GET['term'] ) ? wc_clean( wp_unslash( $_GET['term'] ) ) : '';
if ( ! $search_text ) {
wp_die();
}
$found_categories = array();
$args = array(
'taxonomy' => array( 'product_cat' ),
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => true,
'fields' => 'all',
'name__like' => $search_text,
);
2019-02-15 17:09:45 +00:00
$terms = get_terms( $args );
if ( $terms ) {
foreach ( $terms as $term ) {
$term->formatted_name = '';
if ( $term->parent ) {
$ancestors = array_reverse( get_ancestors( $term->term_id, 'product_cat' ) );
foreach ( $ancestors as $ancestor ) {
2019-02-15 17:09:45 +00:00
$ancestor_term = get_term( $ancestor, 'product_cat' );
if ( $ancestor_term ) {
$term->formatted_name .= $ancestor_term->name . ' > ';
}
}
}
2018-02-22 15:37:01 +00:00
$term->formatted_name .= $term->name . ' (' . $term->count . ')';
$found_categories[ $term->term_id ] = $term;
}
}
wp_send_json( apply_filters( 'woocommerce_json_search_found_categories', $found_categories ) );
}
2013-11-14 12:13:34 +00:00
/**
2015-11-03 13:31:20 +00:00
* Ajax request handling for categories ordering.
2013-11-14 12:13:34 +00:00
*/
public static function term_ordering() {
2019-02-15 17:09:45 +00:00
// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
if ( ! current_user_can( 'edit_products' ) || empty( $_POST['id'] ) ) {
wp_die( -1 );
}
$id = (int) $_POST['id'];
$next_id = isset( $_POST['nextid'] ) && (int) $_POST['nextid'] ? (int) $_POST['nextid'] : null;
2019-02-15 17:09:45 +00:00
$taxonomy = isset( $_POST['thetaxonomy'] ) ? esc_attr( wp_unslash( $_POST['thetaxonomy'] ) ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$term = get_term_by( 'id', $id, $taxonomy );
2013-11-14 12:13:34 +00:00
if ( ! $id || ! $term || ! $taxonomy ) {
wp_die( 0 );
}
2013-11-14 12:13:34 +00:00
2013-12-05 16:07:44 +00:00
wc_reorder_terms( $term, $next_id, $taxonomy );
2013-11-14 12:13:34 +00:00
$children = get_terms( $taxonomy, "child_of=$id&menu_order=ASC&hide_empty=0" );
2013-11-14 12:13:34 +00:00
2019-02-15 17:09:45 +00:00
if ( $term && count( $children ) ) {
2013-11-14 12:13:34 +00:00
echo 'children';
wp_die();
2013-11-14 12:13:34 +00:00
}
2019-02-15 17:09:45 +00:00
// phpcs:enable
2013-11-14 12:13:34 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Ajax request handling for product ordering.
2013-11-14 12:13:34 +00:00
*
2016-10-11 23:37:15 +00:00
* Based on Simple Page Ordering by 10up (https://wordpress.org/plugins/simple-page-ordering/).
2013-11-14 12:13:34 +00:00
*/
public static function product_ordering() {
2013-11-14 12:13:34 +00:00
global $wpdb;
2019-02-15 17:09:45 +00:00
// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
2016-11-24 17:50:24 +00:00
if ( ! current_user_can( 'edit_products' ) || empty( $_POST['id'] ) ) {
wp_die( -1 );
}
2013-11-14 12:13:34 +00:00
2016-11-24 17:50:24 +00:00
$sorting_id = absint( $_POST['id'] );
$previd = absint( isset( $_POST['previd'] ) ? $_POST['previd'] : 0 );
$nextid = absint( isset( $_POST['nextid'] ) ? $_POST['nextid'] : 0 );
$menu_orders = wp_list_pluck( $wpdb->get_results( "SELECT ID, menu_order FROM {$wpdb->posts} WHERE post_type = 'product' ORDER BY menu_order ASC, post_title ASC" ), 'menu_order', 'ID' );
$index = 0;
2013-11-14 12:13:34 +00:00
2016-11-24 17:50:24 +00:00
foreach ( $menu_orders as $id => $menu_order ) {
$id = absint( $id );
2013-11-14 12:13:34 +00:00
2016-11-24 17:50:24 +00:00
if ( $sorting_id === $id ) {
continue;
}
2016-11-24 17:50:24 +00:00
if ( $nextid === $id ) {
$index ++;
2013-11-14 12:13:34 +00:00
}
2016-11-24 17:50:24 +00:00
$index ++;
$menu_orders[ $id ] = $index;
$wpdb->update( $wpdb->posts, array( 'menu_order' => $index ), array( 'ID' => $id ) );
2017-02-25 15:42:35 +00:00
/**
* When a single product has gotten it's ordering updated.
* $id The product ID
* $index The new menu order
*/
do_action( 'woocommerce_after_single_product_ordering', $id, $index );
2013-11-14 12:13:34 +00:00
}
2016-11-24 17:50:24 +00:00
if ( isset( $menu_orders[ $previd ] ) ) {
$menu_orders[ $sorting_id ] = $menu_orders[ $previd ] + 1;
2016-12-05 14:21:09 +00:00
} elseif ( isset( $menu_orders[ $nextid ] ) ) {
2016-11-24 17:50:24 +00:00
$menu_orders[ $sorting_id ] = $menu_orders[ $nextid ] - 1;
} else {
$menu_orders[ $sorting_id ] = 0;
}
$wpdb->update( $wpdb->posts, array( 'menu_order' => $menu_orders[ $sorting_id ] ), array( 'ID' => $sorting_id ) );
2013-11-14 12:13:34 +00:00
do_action( 'woocommerce_after_product_ordering', $sorting_id, $menu_orders );
2016-11-24 17:50:24 +00:00
wp_send_json( $menu_orders );
2019-02-15 17:09:45 +00:00
// phpcs:enable
2014-07-10 12:33:05 +00:00
}
/**
2016-11-24 17:50:24 +00:00
* Handle a refund via the edit order screen.
*
* @throws Exception To return errors.
2014-07-10 12:33:05 +00:00
*/
public static function refund_line_items() {
ob_start();
2014-07-10 12:33:05 +00:00
check_ajax_referer( 'order-item', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) ) {
wp_die( -1 );
}
2019-02-15 17:09:45 +00:00
$order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0;
$refund_amount = isset( $_POST['refund_amount'] ) ? wc_format_decimal( sanitize_text_field( wp_unslash( $_POST['refund_amount'] ) ), wc_get_price_decimals() ) : 0;
$refunded_amount = isset( $_POST['refunded_amount'] ) ? wc_format_decimal( sanitize_text_field( wp_unslash( $_POST['refunded_amount'] ) ), wc_get_price_decimals() ) : 0;
$refund_reason = isset( $_POST['refund_reason'] ) ? sanitize_text_field( wp_unslash( $_POST['refund_reason'] ) ) : '';
$line_item_qtys = isset( $_POST['line_item_qtys'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['line_item_qtys'] ) ), true ) : array();
$line_item_totals = isset( $_POST['line_item_totals'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['line_item_totals'] ) ), true ) : array();
$line_item_tax_totals = isset( $_POST['line_item_tax_totals'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['line_item_tax_totals'] ) ), true ) : array();
$api_refund = isset( $_POST['api_refund'] ) && 'true' === $_POST['api_refund'];
$restock_refunded_items = isset( $_POST['restock_refunded_items'] ) && 'true' === $_POST['restock_refunded_items'];
$refund = false;
2019-01-07 13:29:09 +00:00
$response = array();
2014-07-10 13:49:04 +00:00
try {
$order = wc_get_order( $order_id );
2014-07-25 10:29:24 +00:00
$order_items = $order->get_items();
$max_refund = wc_format_decimal( $order->get_total() - $order->get_total_refunded(), wc_get_price_decimals() );
2014-07-10 13:49:04 +00:00
if ( ! $refund_amount || $max_refund < $refund_amount || 0 > $refund_amount ) {
2019-02-15 17:09:45 +00:00
throw new Exception( __( 'Invalid refund amount', 'woocommerce' ) );
2014-07-10 13:49:04 +00:00
}
2019-02-15 17:09:45 +00:00
if ( wc_format_decimal( $order->get_total_refunded(), wc_get_price_decimals() ) !== $refunded_amount ) {
throw new Exception( __( 'Error processing refund. Please try again.', 'woocommerce' ) );
}
// Prepare line items which we are refunding.
2014-07-24 14:34:14 +00:00
$line_items = array();
2019-03-05 14:51:58 +00:00
$item_ids = array_unique( array_merge( array_keys( $line_item_qtys ), array_keys( $line_item_totals ) ) );
2014-07-24 14:34:14 +00:00
foreach ( $item_ids as $item_id ) {
2018-02-22 15:37:01 +00:00
$line_items[ $item_id ] = array(
'qty' => 0,
'refund_total' => 0,
'refund_tax' => array(),
);
2014-07-24 14:34:14 +00:00
}
foreach ( $line_item_qtys as $item_id => $qty ) {
$line_items[ $item_id ]['qty'] = max( $qty, 0 );
}
foreach ( $line_item_totals as $item_id => $total ) {
$line_items[ $item_id ]['refund_total'] = wc_format_decimal( $total );
2014-07-24 14:34:14 +00:00
}
foreach ( $line_item_tax_totals as $item_id => $tax_totals ) {
2017-03-29 12:13:00 +00:00
$line_items[ $item_id ]['refund_tax'] = array_filter( array_map( 'wc_format_decimal', $tax_totals ) );
2014-07-24 14:34:14 +00:00
}
2017-02-08 16:20:42 +00:00
// Create the refund object.
2018-02-22 15:37:01 +00:00
$refund = wc_create_refund(
array(
'amount' => $refund_amount,
'reason' => $refund_reason,
'order_id' => $order_id,
'line_items' => $line_items,
'refund_payment' => $api_refund,
'restock_items' => $restock_refunded_items,
)
);
2014-07-10 13:49:04 +00:00
if ( is_wp_error( $refund ) ) {
throw new Exception( $refund->get_error_message() );
2014-07-10 13:49:04 +00:00
}
2017-02-08 16:20:42 +00:00
if ( did_action( 'woocommerce_order_fully_refunded' ) ) {
2019-01-07 13:29:09 +00:00
$response['status'] = 'fully_refunded';
}
2014-07-10 13:49:04 +00:00
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
2014-07-10 13:49:04 +00:00
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
2019-01-07 13:29:09 +00:00
wp_send_json_success( $response );
2013-11-14 12:13:34 +00:00
}
2014-07-10 15:39:10 +00:00
/**
2015-11-03 13:31:20 +00:00
* Delete a refund.
2014-07-10 15:39:10 +00:00
*/
public static function delete_refund() {
check_ajax_referer( 'order-item', 'security' );
2019-02-15 17:09:45 +00:00
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['refund_id'] ) ) {
wp_die( -1 );
}
2019-02-15 17:09:45 +00:00
$refund_ids = array_map( 'absint', is_array( $_POST['refund_id'] ) ? wp_unslash( $_POST['refund_id'] ) : array( wp_unslash( $_POST['refund_id'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2016-03-23 16:32:12 +00:00
foreach ( $refund_ids as $refund_id ) {
if ( $refund_id && 'shop_order_refund' === get_post_type( $refund_id ) ) {
2016-11-24 15:31:05 +00:00
$refund = wc_get_order( $refund_id );
$order_id = $refund->get_parent_id();
2017-01-04 14:05:13 +00:00
$refund->delete( true );
2016-03-23 16:32:12 +00:00
do_action( 'woocommerce_refund_deleted', $refund_id, $order_id );
}
2014-07-10 15:39:10 +00:00
}
wp_die();
2014-07-10 15:39:10 +00:00
}
/**
* Triggered when clicking the rating footer.
*/
public static function rated() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( -1 );
}
update_option( 'woocommerce_admin_footer_text_rated', 1 );
wp_die();
}
2015-06-08 22:41:35 +00:00
/**
2015-11-03 13:31:20 +00:00
* Create/Update API key.
2019-02-15 17:09:45 +00:00
*
2019-03-05 13:05:55 +00:00
* @throws Exception On invalid or empty description, user, or permissions.
2015-06-08 22:41:35 +00:00
*/
public static function update_api_key() {
ob_start();
global $wpdb;
check_ajax_referer( 'update-api-key', 'security' );
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( -1 );
2015-06-08 22:41:35 +00:00
}
2019-01-07 13:29:09 +00:00
$response = array();
2015-06-08 22:41:35 +00:00
try {
if ( empty( $_POST['description'] ) ) {
throw new Exception( __( 'Description is missing.', 'woocommerce' ) );
}
if ( empty( $_POST['user'] ) ) {
throw new Exception( __( 'User is missing.', 'woocommerce' ) );
}
if ( empty( $_POST['permissions'] ) ) {
throw new Exception( __( 'Permissions is missing.', 'woocommerce' ) );
}
2019-02-15 17:09:45 +00:00
$key_id = isset( $_POST['key_id'] ) ? absint( $_POST['key_id'] ) : 0;
2015-09-07 15:29:03 +00:00
$description = sanitize_text_field( wp_unslash( $_POST['description'] ) );
2019-02-15 17:09:45 +00:00
$permissions = ( in_array( wp_unslash( $_POST['permissions'] ), array( 'read', 'write', 'read_write' ), true ) ) ? sanitize_text_field( wp_unslash( $_POST['permissions'] ) ) : 'read';
2015-06-08 22:41:35 +00:00
$user_id = absint( $_POST['user'] );
// Check if current user can edit other users.
if ( $user_id && ! current_user_can( 'edit_user', $user_id ) ) {
if ( get_current_user_id() !== $user_id ) {
throw new Exception( __( 'You do not have permission to assign API Keys to the selected user.', 'woocommerce' ) );
}
}
2015-06-08 22:41:35 +00:00
if ( 0 < $key_id ) {
$data = array(
'user_id' => $user_id,
'description' => $description,
'permissions' => $permissions,
2015-06-08 22:41:35 +00:00
);
$wpdb->update(
$wpdb->prefix . 'woocommerce_api_keys',
$data,
array( 'key_id' => $key_id ),
array(
'%d',
'%s',
'%s',
2015-06-08 22:41:35 +00:00
),
array( '%d' )
);
2019-01-07 13:29:09 +00:00
$response = $data;
$response['consumer_key'] = '';
$response['consumer_secret'] = '';
$response['message'] = __( 'API Key updated successfully.', 'woocommerce' );
2015-06-08 22:41:35 +00:00
} else {
$consumer_key = 'ck_' . wc_rand_hash();
$consumer_secret = 'cs_' . wc_rand_hash();
$data = array(
'user_id' => $user_id,
'description' => $description,
'permissions' => $permissions,
'consumer_key' => wc_api_hash( $consumer_key ),
'consumer_secret' => $consumer_secret,
'truncated_key' => substr( $consumer_key, -7 ),
2015-06-08 22:41:35 +00:00
);
$wpdb->insert(
$wpdb->prefix . 'woocommerce_api_keys',
$data,
array(
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
2015-06-08 22:41:35 +00:00
)
);
2019-01-07 13:29:09 +00:00
$key_id = $wpdb->insert_id;
$response = $data;
$response['consumer_key'] = $consumer_key;
$response['consumer_secret'] = $consumer_secret;
$response['message'] = __( 'API Key generated successfully. Make sure to copy your new keys now as the secret key will be hidden once you leave this page.', 'woocommerce' );
$response['revoke_url'] = '<a style="color: #a00; text-decoration: none;" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'revoke-key' => $key_id ), admin_url( 'admin.php?page=wc-settings&tab=advanced&section=keys' ) ), 'revoke' ) ) . '">' . __( 'Revoke key', 'woocommerce' ) . '</a>';
2015-06-08 22:41:35 +00:00
}
} catch ( Exception $e ) {
wp_send_json_error( array( 'message' => $e->getMessage() ) );
}
// wp_send_json_success must be outside the try block not to break phpunit tests.
2019-01-07 13:29:09 +00:00
wp_send_json_success( $response );
2015-06-08 22:41:35 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Load variations via AJAX.
*/
public static function load_variations() {
ob_start();
check_ajax_referer( 'load-variations', 'security' );
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
if ( ! current_user_can( 'edit_products' ) || empty( $_POST['product_id'] ) ) {
wp_die( -1 );
}
2019-02-15 17:09:45 +00:00
// Set $post global so its available, like within the admin screens.
global $post;
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$loop = 0;
$product_id = absint( $_POST['product_id'] );
2019-02-15 17:09:45 +00:00
$post = get_post( $product_id ); // phpcs:ignore
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$product_object = wc_get_product( $product_id );
$per_page = ! empty( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : 10;
$page = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
2018-02-22 15:37:01 +00:00
$variations = wc_get_products(
array(
'status' => array( 'private', 'publish' ),
'type' => 'variation',
'parent' => $product_id,
'limit' => $per_page,
'page' => $page,
'orderby' => array(
'menu_order' => 'ASC',
'ID' => 'DESC',
),
'return' => 'objects',
)
);
if ( $variations ) {
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
foreach ( $variations as $variation_object ) {
$variation_id = $variation_object->get_id();
$variation = get_post( $variation_id );
2019-01-21 16:02:28 +00:00
$variation_data = array_merge( get_post_custom( $variation_id ), wc_get_product_variation_attributes( $variation_id ) ); // kept for BW compatibility.
2018-02-22 15:37:01 +00:00
include 'admin/meta-boxes/views/html-variation-admin.php';
$loop++;
}
}
wp_die();
}
/**
2015-11-03 13:31:20 +00:00
* Save variations via AJAX.
*/
public static function save_variations() {
ob_start();
check_ajax_referer( 'save-variations', 'security' );
2019-02-15 17:09:45 +00:00
// Check permissions again and make sure we have what we need.
2015-07-08 16:55:29 +00:00
if ( ! current_user_can( 'edit_products' ) || empty( $_POST ) || empty( $_POST['product_id'] ) ) {
wp_die( -1 );
}
2018-02-22 15:37:01 +00:00
$product_id = absint( $_POST['product_id'] );
2015-07-27 17:16:52 +00:00
WC_Admin_Meta_Boxes::$meta_box_errors = array();
WC_Meta_Box_Product_Data::save_variations( $product_id, get_post( $product_id ) );
do_action( 'woocommerce_ajax_save_product_variations', $product_id );
2019-02-15 17:09:45 +00:00
$errors = WC_Admin_Meta_Boxes::$meta_box_errors;
if ( $errors ) {
echo '<div class="error notice is-dismissible">';
2015-07-27 17:16:52 +00:00
foreach ( $errors as $error ) {
echo '<p>' . wp_kses_post( $error ) . '</p>';
}
2019-02-15 17:09:45 +00:00
echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . esc_html__( 'Dismiss this notice.', 'woocommerce' ) . '</span></button>';
2015-07-27 17:16:52 +00:00
echo '</div>';
delete_option( 'woocommerce_meta_box_errors' );
}
wp_die();
}
2015-07-07 01:50:35 +00:00
/**
2016-11-24 15:31:05 +00:00
* Bulk action - Toggle Enabled.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-07 01:50:35 +00:00
*/
2015-07-14 10:39:48 +00:00
private static function variation_bulk_action_toggle_enabled( $variations, $data ) {
foreach ( $variations as $variation_id ) {
$variation = wc_get_product( $variation_id );
$variation->set_status( 'private' === $variation->get_status( 'edit' ) ? 'publish' : 'private' );
$variation->save();
2015-07-14 10:39:48 +00:00
}
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Toggle Downloadable Checkbox.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_toggle_downloadable( $variations, $data ) {
self::variation_bulk_toggle( $variations, 'downloadable' );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Toggle Virtual Checkbox.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_toggle_virtual( $variations, $data ) {
self::variation_bulk_toggle( $variations, 'virtual' );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Toggle Manage Stock Checkbox.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_toggle_manage_stock( $variations, $data ) {
self::variation_bulk_toggle( $variations, 'manage_stock' );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Set Regular Prices.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_regular_price( $variations, $data ) {
self::variation_bulk_set( $variations, 'regular_price', $data['value'] );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Set Sale Prices.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_sale_price( $variations, $data ) {
self::variation_bulk_set( $variations, 'sale_price', $data['value'] );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
/**
* Bulk action - Set Stock Status as In Stock.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
* @used-by bulk_edit_variations
*/
private static function variation_bulk_action_variable_stock_status_instock( $variations, $data ) {
self::variation_bulk_set( $variations, 'stock_status', 'instock' );
}
/**
* Bulk action - Set Stock Status as Out of Stock.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
* @used-by bulk_edit_variations
*/
private static function variation_bulk_action_variable_stock_status_outofstock( $variations, $data ) {
self::variation_bulk_set( $variations, 'stock_status', 'outofstock' );
}
2017-11-14 22:18:03 +00:00
/**
* Bulk action - Set Stock Status as On Backorder.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2017-11-14 22:18:03 +00:00
* @used-by bulk_edit_variations
*/
private static function variation_bulk_action_variable_stock_status_onbackorder( $variations, $data ) {
self::variation_bulk_set( $variations, 'stock_status', 'onbackorder' );
}
2015-07-14 10:39:48 +00:00
/**
2016-11-24 15:31:05 +00:00
* Bulk action - Set Stock.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_stock( $variations, $data ) {
if ( ! isset( $data['value'] ) ) {
2015-08-11 12:47:12 +00:00
return;
2015-07-14 10:39:48 +00:00
}
$quantity = wc_stock_amount( wc_clean( $data['value'] ) );
foreach ( $variations as $variation_id ) {
$variation = wc_get_product( $variation_id );
if ( $variation->managing_stock() ) {
$variation->set_stock_quantity( $quantity );
} else {
$variation->set_stock_quantity( null );
2015-07-14 10:39:48 +00:00
}
$variation->save();
2015-07-14 10:39:48 +00:00
}
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Set Weight.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_weight( $variations, $data ) {
self::variation_bulk_set( $variations, 'weight', $data['value'] );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Set Length.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_length( $variations, $data ) {
self::variation_bulk_set( $variations, 'length', $data['value'] );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Set Width.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_width( $variations, $data ) {
self::variation_bulk_set( $variations, 'width', $data['value'] );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Set Height.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_height( $variations, $data ) {
self::variation_bulk_set( $variations, 'height', $data['value'] );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Set Download Limit.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_download_limit( $variations, $data ) {
self::variation_bulk_set( $variations, 'download_limit', $data['value'] );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Set Download Expiry.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_download_expiry( $variations, $data ) {
self::variation_bulk_set( $variations, 'download_expiry', $data['value'] );
2015-07-14 10:39:48 +00:00
}
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Delete all.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_delete_all( $variations, $data ) {
if ( isset( $data['allowed'] ) && 'true' === $data['allowed'] ) {
foreach ( $variations as $variation_id ) {
$variation = wc_get_product( $variation_id );
2017-01-04 14:05:13 +00:00
$variation->delete( true );
2015-07-14 10:39:48 +00:00
}
}
}
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Sale Schedule.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_sale_schedule( $variations, $data ) {
if ( ! isset( $data['date_from'] ) && ! isset( $data['date_to'] ) ) {
2015-08-11 12:47:12 +00:00
return;
2015-07-14 10:39:48 +00:00
}
2015-07-14 10:39:48 +00:00
foreach ( $variations as $variation_id ) {
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$variation = wc_get_product( $variation_id );
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
if ( 'false' !== $data['date_from'] ) {
$variation->set_date_on_sale_from( wc_clean( $data['date_from'] ) );
2015-07-14 10:39:48 +00:00
}
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
if ( 'false' !== $data['date_to'] ) {
$variation->set_date_on_sale_to( wc_clean( $data['date_to'] ) );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$variation->save();
2015-07-14 10:39:48 +00:00
}
}
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Increase Regular Prices.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_regular_price_increase( $variations, $data ) {
self::variation_bulk_adjust_price( $variations, 'regular_price', '+', wc_clean( $data['value'] ) );
2015-07-14 10:39:48 +00:00
}
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Decrease Regular Prices.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_regular_price_decrease( $variations, $data ) {
self::variation_bulk_adjust_price( $variations, 'regular_price', '-', wc_clean( $data['value'] ) );
2015-07-14 10:39:48 +00:00
}
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Increase Sale Prices.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_sale_price_increase( $variations, $data ) {
self::variation_bulk_adjust_price( $variations, 'sale_price', '+', wc_clean( $data['value'] ) );
2015-07-14 10:39:48 +00:00
}
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Decrease Sale Prices.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param array $data Data to set.
*
2016-06-07 13:19:36 +00:00
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_action_variable_sale_price_decrease( $variations, $data ) {
self::variation_bulk_adjust_price( $variations, 'sale_price', '-', wc_clean( $data['value'] ) );
2015-07-14 10:39:48 +00:00
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Bulk action - Set Price.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param string $field price being adjusted _regular_price or _sale_price.
* @param string $operator + or -.
* @param string $value Price or Percent.
*
* @used-by bulk_edit_variations
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_adjust_price( $variations, $field, $operator, $value ) {
foreach ( $variations as $variation_id ) {
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$variation = wc_get_product( $variation_id );
$field_value = $variation->{"get_$field"}( 'edit' );
2015-07-14 10:39:48 +00:00
if ( '%' === substr( $value, -1 ) ) {
2018-02-22 15:37:01 +00:00
$percent = wc_format_decimal( substr( $value, 0, -1 ) );
$field_value += round( ( $field_value / 100 ) * $percent, wc_get_price_decimals() ) * "{$operator}1";
2015-07-14 10:39:48 +00:00
} else {
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$field_value += $value * "{$operator}1";
2015-07-14 10:39:48 +00:00
}
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$variation->{"set_$field"}( $field_value );
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$variation->save();
2015-07-14 10:39:48 +00:00
}
}
2015-07-07 01:50:35 +00:00
2015-07-14 10:39:48 +00:00
/**
* Bulk set convenience function.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param string $field Field to set.
* @param string $value to set.
2015-07-14 10:39:48 +00:00
*/
private static function variation_bulk_set( $variations, $field, $value ) {
2015-07-14 10:39:48 +00:00
foreach ( $variations as $variation_id ) {
$variation = wc_get_product( $variation_id );
$variation->{ "set_$field" }( wc_clean( $value ) );
$variation->save();
}
}
/**
* Bulk toggle convenience function.
2018-02-22 15:37:01 +00:00
*
2019-02-15 17:09:45 +00:00
* @param array $variations List of variations.
* @param string $field Field to toggle.
*/
private static function variation_bulk_toggle( $variations, $field ) {
foreach ( $variations as $variation_id ) {
2018-02-22 15:37:01 +00:00
$variation = wc_get_product( $variation_id );
$prev_value = $variation->{ "get_$field" }( 'edit' );
$variation->{ "set_$field" }( ! $prev_value );
$variation->save();
2015-07-14 10:39:48 +00:00
}
}
/**
2015-11-03 13:31:20 +00:00
* Bulk edit variations via AJAX.
2018-02-22 15:37:01 +00:00
*
* @uses WC_AJAX::variation_bulk_set()
2016-06-07 13:19:36 +00:00
* @uses WC_AJAX::variation_bulk_adjust_price()
* @uses WC_AJAX::variation_bulk_action_variable_sale_price_decrease()
* @uses WC_AJAX::variation_bulk_action_variable_sale_price_increase()
* @uses WC_AJAX::variation_bulk_action_variable_regular_price_decrease()
* @uses WC_AJAX::variation_bulk_action_variable_regular_price_increase()
* @uses WC_AJAX::variation_bulk_action_variable_sale_schedule()
* @uses WC_AJAX::variation_bulk_action_delete_all()
* @uses WC_AJAX::variation_bulk_action_variable_download_expiry()
* @uses WC_AJAX::variation_bulk_action_variable_download_limit()
* @uses WC_AJAX::variation_bulk_action_variable_height()
* @uses WC_AJAX::variation_bulk_action_variable_width()
* @uses WC_AJAX::variation_bulk_action_variable_length()
* @uses WC_AJAX::variation_bulk_action_variable_weight()
* @uses WC_AJAX::variation_bulk_action_variable_stock()
* @uses WC_AJAX::variation_bulk_action_variable_sale_price()
* @uses WC_AJAX::variation_bulk_action_variable_regular_price()
* @uses WC_AJAX::variation_bulk_action_toggle_manage_stock()
* @uses WC_AJAX::variation_bulk_action_toggle_virtual()
* @uses WC_AJAX::variation_bulk_action_toggle_downloadable()
* @uses WC_AJAX::variation_bulk_action_toggle_enabled
2015-07-14 10:39:48 +00:00
*/
public static function bulk_edit_variations() {
ob_start();
check_ajax_referer( 'bulk-edit-variations', 'security' );
2019-02-15 17:09:45 +00:00
// Check permissions again and make sure we have what we need.
2015-07-14 10:39:48 +00:00
if ( ! current_user_can( 'edit_products' ) || empty( $_POST['product_id'] ) || empty( $_POST['bulk_action'] ) ) {
wp_die( -1 );
2015-07-14 10:39:48 +00:00
}
$product_id = absint( $_POST['product_id'] );
2019-02-15 17:09:45 +00:00
$bulk_action = wc_clean( wp_unslash( $_POST['bulk_action'] ) );
$data = ! empty( $_POST['data'] ) ? wc_clean( wp_unslash( $_POST['data'] ) ) : array();
2015-07-14 10:39:48 +00:00
$variations = array();
if ( apply_filters( 'woocommerce_bulk_edit_variations_need_children', true ) ) {
2018-02-22 15:37:01 +00:00
$variations = get_posts(
array(
'post_parent' => $product_id,
'posts_per_page' => -1,
'post_type' => 'product_variation',
'fields' => 'ids',
'post_status' => array( 'publish', 'private' ),
)
);
2015-07-14 10:39:48 +00:00
}
if ( method_exists( __CLASS__, "variation_bulk_action_$bulk_action" ) ) {
call_user_func( array( __CLASS__, "variation_bulk_action_$bulk_action" ), $variations, $data );
} else {
do_action( 'woocommerce_bulk_edit_variations_default', $bulk_action, $data, $product_id, $variations );
2015-07-07 01:50:35 +00:00
}
do_action( 'woocommerce_bulk_edit_variations', $bulk_action, $data, $product_id, $variations );
2015-07-07 01:50:35 +00:00
WC_Product_Variable::sync( $product_id );
wc_delete_product_transients( $product_id );
wp_die();
2015-07-07 01:50:35 +00:00
}
/**
* Handle submissions from assets/js/settings-views-html-settings-tax.js Backbone model.
*/
public static function tax_rates_save_changes() {
2019-02-15 17:09:45 +00:00
// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
if ( ! isset( $_POST['wc_tax_nonce'], $_POST['changes'] ) ) {
wp_send_json_error( 'missing_fields' );
wp_die();
}
2019-02-15 17:09:45 +00:00
$current_class = ! empty( $_POST['current_class'] ) ? wp_unslash( $_POST['current_class'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2019-02-15 17:09:45 +00:00
if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_tax_nonce'] ), 'wc_tax_nonce-class:' . $current_class ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
wp_send_json_error( 'bad_nonce' );
wp_die();
}
$current_class = WC_Tax::format_tax_rate_class( $current_class );
2019-02-15 17:09:45 +00:00
// Check User Caps.
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'missing_capabilities' );
wp_die();
}
2019-02-15 17:09:45 +00:00
$changes = wp_unslash( $_POST['changes'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
foreach ( $changes as $tax_rate_id => $data ) {
if ( isset( $data['deleted'] ) ) {
if ( isset( $data['newRow'] ) ) {
// So the user added and deleted a new row.
// That's fine, it's not in the database anyways. NEXT!
continue;
}
WC_Tax::_delete_tax_rate( $tax_rate_id );
}
2018-02-22 15:37:01 +00:00
$tax_rate = array_intersect_key(
2019-02-15 17:09:45 +00:00
$data,
array(
2018-02-22 15:37:01 +00:00
'tax_rate_country' => 1,
'tax_rate_state' => 1,
'tax_rate' => 1,
'tax_rate_name' => 1,
'tax_rate_priority' => 1,
'tax_rate_compound' => 1,
'tax_rate_shipping' => 1,
'tax_rate_order' => 1,
)
);
if ( isset( $tax_rate['tax_rate'] ) ) {
$tax_rate['tax_rate'] = wc_format_decimal( $tax_rate['tax_rate'] );
}
if ( isset( $data['newRow'] ) ) {
$tax_rate['tax_rate_class'] = $current_class;
$tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate );
} elseif ( ! empty( $tax_rate ) ) {
WC_Tax::_update_tax_rate( $tax_rate_id, $tax_rate );
}
if ( isset( $data['postcode'] ) ) {
$postcode = array_map( 'wc_clean', $data['postcode'] );
$postcode = array_map( 'wc_normalize_postcode', $postcode );
WC_Tax::_update_tax_rate_postcodes( $tax_rate_id, $postcode );
}
if ( isset( $data['city'] ) ) {
WC_Tax::_update_tax_rate_cities( $tax_rate_id, array_map( 'wc_clean', array_map( 'wp_unslash', $data['city'] ) ) );
}
}
2018-01-18 17:47:26 +00:00
WC_Cache_Helper::incr_cache_prefix( 'taxes' );
WC_Cache_Helper::get_transient_version( 'shipping', true );
2018-02-22 15:37:01 +00:00
wp_send_json_success(
array(
'rates' => WC_Tax::get_rates_for_tax_class( $current_class ),
)
);
2019-02-15 17:09:45 +00:00
// phpcs:enable
}
/**
* Handle submissions from assets/js/wc-shipping-zones.js Backbone model.
*/
public static function shipping_zones_save_changes() {
if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['changes'] ) ) {
2015-12-15 17:48:03 +00:00
wp_send_json_error( 'missing_fields' );
wp_die();
}
2019-02-15 17:09:45 +00:00
if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_zones_nonce'] ), 'wc_shipping_zones_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
wp_send_json_error( 'bad_nonce' );
wp_die();
}
2019-02-15 17:09:45 +00:00
// Check User Caps.
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'missing_capabilities' );
wp_die();
}
2019-02-15 17:09:45 +00:00
$changes = wp_unslash( $_POST['changes'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
foreach ( $changes as $zone_id => $data ) {
if ( isset( $data['deleted'] ) ) {
if ( isset( $data['newRow'] ) ) {
// So the user added and deleted a new row.
// That's fine, it's not in the database anyways. NEXT!
continue;
}
WC_Shipping_Zones::delete_zone( $zone_id );
continue;
}
2018-02-22 15:37:01 +00:00
$zone_data = array_intersect_key(
2019-02-15 17:09:45 +00:00
$data,
array(
2018-02-22 15:37:01 +00:00
'zone_id' => 1,
'zone_order' => 1,
)
);
2015-12-10 12:31:03 +00:00
if ( isset( $zone_data['zone_id'] ) ) {
$zone = new WC_Shipping_Zone( $zone_data['zone_id'] );
if ( isset( $zone_data['zone_order'] ) ) {
$zone->set_zone_order( $zone_data['zone_order'] );
}
$zone->save();
}
}
2018-02-22 15:37:01 +00:00
wp_send_json_success(
array(
'zones' => WC_Shipping_Zones::get_zones( 'json' ),
2018-02-22 15:37:01 +00:00
)
);
2015-12-15 17:48:03 +00:00
}
2015-12-16 15:16:52 +00:00
/**
* Handle submissions from assets/js/wc-shipping-zone-methods.js Backbone model.
*/
public static function shipping_zone_add_method() {
if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['zone_id'], $_POST['method_id'] ) ) {
wp_send_json_error( 'missing_fields' );
wp_die();
2015-12-16 15:16:52 +00:00
}
2019-02-15 17:09:45 +00:00
if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_zones_nonce'] ), 'wc_shipping_zones_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2015-12-16 15:16:52 +00:00
wp_send_json_error( 'bad_nonce' );
wp_die();
2015-12-16 15:16:52 +00:00
}
2019-02-15 17:09:45 +00:00
// Check User Caps.
2015-12-16 15:16:52 +00:00
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'missing_capabilities' );
wp_die();
2015-12-16 15:16:52 +00:00
}
2019-02-15 17:09:45 +00:00
$zone_id = wc_clean( wp_unslash( $_POST['zone_id'] ) );
2016-09-24 02:22:57 +00:00
$zone = new WC_Shipping_Zone( $zone_id );
2019-02-15 17:09:45 +00:00
$instance_id = $zone->add_shipping_method( wc_clean( wp_unslash( $_POST['method_id'] ) ) );
2015-12-16 15:16:52 +00:00
2018-02-22 15:37:01 +00:00
wp_send_json_success(
array(
'instance_id' => $instance_id,
'zone_id' => $zone->get_id(),
'zone_name' => $zone->get_zone_name(),
'methods' => $zone->get_shipping_methods( false, 'json' ),
)
);
2015-12-16 15:16:52 +00:00
}
2015-12-15 17:48:03 +00:00
/**
* Handle submissions from assets/js/wc-shipping-zone-methods.js Backbone model.
*/
public static function shipping_zone_methods_save_changes() {
2016-10-10 17:41:07 +00:00
if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['zone_id'], $_POST['changes'] ) ) {
2015-12-15 17:48:03 +00:00
wp_send_json_error( 'missing_fields' );
wp_die();
2015-12-15 17:48:03 +00:00
}
2019-02-15 17:09:45 +00:00
if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_zones_nonce'] ), 'wc_shipping_zones_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2015-12-15 17:48:03 +00:00
wp_send_json_error( 'bad_nonce' );
wp_die();
2015-12-15 17:48:03 +00:00
}
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'missing_capabilities' );
wp_die();
2015-12-15 17:48:03 +00:00
}
global $wpdb;
2019-02-15 17:09:45 +00:00
$zone_id = wc_clean( wp_unslash( $_POST['zone_id'] ) );
2015-12-15 17:48:03 +00:00
$zone = new WC_Shipping_Zone( $zone_id );
2019-02-15 17:09:45 +00:00
$changes = wp_unslash( $_POST['changes'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2016-09-24 02:22:57 +00:00
2016-10-10 17:41:07 +00:00
if ( isset( $changes['zone_name'] ) ) {
$zone->set_zone_name( wc_clean( $changes['zone_name'] ) );
2016-09-24 02:22:57 +00:00
}
2016-10-10 17:41:07 +00:00
if ( isset( $changes['zone_locations'] ) ) {
2016-09-24 02:22:57 +00:00
$zone->clear_locations( array( 'state', 'country', 'continent' ) );
2016-10-10 17:41:07 +00:00
$locations = array_filter( array_map( 'wc_clean', (array) $changes['zone_locations'] ) );
2016-09-24 02:22:57 +00:00
foreach ( $locations as $location ) {
2019-02-15 17:09:45 +00:00
// Each posted location will be in the format type:code.
2016-09-24 02:22:57 +00:00
$location_parts = explode( ':', $location );
switch ( $location_parts[0] ) {
2018-02-22 15:37:01 +00:00
case 'state':
2016-09-24 02:22:57 +00:00
$zone->add_location( $location_parts[1] . ':' . $location_parts[2], 'state' );
2018-02-22 15:37:01 +00:00
break;
case 'country':
2016-09-24 02:22:57 +00:00
$zone->add_location( $location_parts[1], 'country' );
2018-02-22 15:37:01 +00:00
break;
case 'continent':
2016-09-24 02:22:57 +00:00
$zone->add_location( $location_parts[1], 'continent' );
2018-02-22 15:37:01 +00:00
break;
2016-09-24 02:22:57 +00:00
}
}
}
2016-10-10 17:41:07 +00:00
if ( isset( $changes['zone_postcodes'] ) ) {
2016-09-24 02:22:57 +00:00
$zone->clear_locations( 'postcode' );
2016-10-10 17:41:07 +00:00
$postcodes = array_filter( array_map( 'strtoupper', array_map( 'wc_clean', explode( "\n", $changes['zone_postcodes'] ) ) ) );
2016-09-24 02:22:57 +00:00
foreach ( $postcodes as $postcode ) {
$zone->add_location( $postcode, 'postcode' );
}
}
2015-12-15 17:48:03 +00:00
2016-10-10 17:41:07 +00:00
if ( isset( $changes['methods'] ) ) {
foreach ( $changes['methods'] as $instance_id => $data ) {
$method_id = $wpdb->get_var( $wpdb->prepare( "SELECT method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d", $instance_id ) );
2016-10-10 17:41:07 +00:00
if ( isset( $data['deleted'] ) ) {
$shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id );
$option_key = $shipping_method->get_instance_option_key();
if ( $wpdb->delete( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'instance_id' => $instance_id ) ) ) {
delete_option( $option_key );
do_action( 'woocommerce_shipping_zone_method_deleted', $instance_id, $method_id, $zone_id );
}
continue;
}
2015-12-15 17:48:03 +00:00
2018-02-22 15:37:01 +00:00
$method_data = array_intersect_key(
2019-02-15 17:09:45 +00:00
$data,
array(
2018-02-22 15:37:01 +00:00
'method_order' => 1,
'enabled' => 1,
)
);
2015-12-15 17:48:03 +00:00
2016-10-10 17:41:07 +00:00
if ( isset( $method_data['method_order'] ) ) {
$wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'method_order' => absint( $method_data['method_order'] ) ), array( 'instance_id' => absint( $instance_id ) ) );
}
2016-10-10 17:41:07 +00:00
if ( isset( $method_data['enabled'] ) ) {
$is_enabled = absint( 'yes' === $method_data['enabled'] );
if ( $wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'is_enabled' => $is_enabled ), array( 'instance_id' => absint( $instance_id ) ) ) ) {
do_action( 'woocommerce_shipping_zone_method_status_toggled', $instance_id, $method_id, $zone_id, $is_enabled );
}
}
}
2015-12-15 17:48:03 +00:00
}
2016-09-24 02:22:57 +00:00
$zone->save();
2018-02-22 15:37:01 +00:00
wp_send_json_success(
array(
'zone_id' => $zone->get_id(),
'zone_name' => $zone->get_zone_name(),
'methods' => $zone->get_shipping_methods( false, 'json' ),
)
);
}
2016-03-24 17:26:40 +00:00
/**
* Save method settings
*/
public static function shipping_zone_methods_save_settings() {
if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['instance_id'], $_POST['data'] ) ) {
wp_send_json_error( 'missing_fields' );
wp_die();
2016-03-24 17:26:40 +00:00
}
2019-02-15 17:09:45 +00:00
if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_zones_nonce'] ), 'wc_shipping_zones_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2016-03-24 17:26:40 +00:00
wp_send_json_error( 'bad_nonce' );
wp_die();
2016-03-24 17:26:40 +00:00
}
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'missing_capabilities' );
wp_die();
2016-03-24 17:26:40 +00:00
}
$instance_id = absint( $_POST['instance_id'] );
$zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id );
$shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id );
2019-02-15 17:09:45 +00:00
$shipping_method->set_post_data( wp_unslash( $_POST['data'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$shipping_method->process_admin_options();
2016-03-24 17:26:40 +00:00
WC_Cache_Helper::get_transient_version( 'shipping', true );
2018-02-22 15:37:01 +00:00
wp_send_json_success(
array(
'zone_id' => $zone->get_id(),
'zone_name' => $zone->get_zone_name(),
'methods' => $zone->get_shipping_methods( false, 'json' ),
'errors' => $shipping_method->get_errors(),
)
);
2016-03-24 17:26:40 +00:00
}
/**
* Handle submissions from assets/js/wc-shipping-classes.js Backbone model.
*/
public static function shipping_classes_save_changes() {
if ( ! isset( $_POST['wc_shipping_classes_nonce'], $_POST['changes'] ) ) {
wp_send_json_error( 'missing_fields' );
wp_die();
}
2019-02-15 17:09:45 +00:00
if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_classes_nonce'] ), 'wc_shipping_classes_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
wp_send_json_error( 'bad_nonce' );
wp_die();
}
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'missing_capabilities' );
wp_die();
}
2019-02-15 17:09:45 +00:00
$changes = wp_unslash( $_POST['changes'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
foreach ( $changes as $term_id => $data ) {
$term_id = absint( $term_id );
if ( isset( $data['deleted'] ) ) {
if ( isset( $data['newRow'] ) ) {
// So the user added and deleted a new row.
// That's fine, it's not in the database anyways. NEXT!
continue;
}
wp_delete_term( $term_id, 'product_shipping_class' );
continue;
}
$update_args = array();
if ( isset( $data['name'] ) ) {
$update_args['name'] = wc_clean( $data['name'] );
}
if ( isset( $data['slug'] ) ) {
$update_args['slug'] = wc_clean( $data['slug'] );
}
if ( isset( $data['description'] ) ) {
$update_args['description'] = wc_clean( $data['description'] );
}
if ( isset( $data['newRow'] ) ) {
$update_args = array_filter( $update_args );
if ( empty( $update_args['name'] ) ) {
2016-05-12 11:18:35 +00:00
continue;
}
$inserted_term = wp_insert_term( $update_args['name'], 'product_shipping_class', $update_args );
$term_id = is_wp_error( $inserted_term ) ? 0 : $inserted_term['term_id'];
} else {
wp_update_term( $term_id, 'product_shipping_class', $update_args );
}
do_action( 'woocommerce_shipping_classes_save_class', $term_id, $data );
}
$wc_shipping = WC_Shipping::instance();
2018-02-22 15:37:01 +00:00
wp_send_json_success(
array(
'shipping_classes' => $wc_shipping->get_shipping_classes(),
)
);
}
/**
* Toggle payment gateway on or off via AJAX.
*
* @since 3.4.0
*/
public static function toggle_gateway_enabled() {
2019-02-15 17:09:45 +00:00
if ( current_user_can( 'manage_woocommerce' ) && check_ajax_referer( 'woocommerce-toggle-payment-gateway-enabled', 'security' ) && isset( $_POST['gateway_id'] ) ) {
// Load gateways.
$payment_gateways = WC()->payment_gateways->payment_gateways();
// Get posted gateway.
$gateway_id = wc_clean( wp_unslash( $_POST['gateway_id'] ) );
foreach ( $payment_gateways as $gateway ) {
if ( ! in_array( $gateway_id, array( $gateway->id, sanitize_title( get_class( $gateway ) ) ), true ) ) {
continue;
}
$enabled = $gateway->get_option( 'enabled', 'no' );
if ( ! wc_string_to_bool( $enabled ) ) {
if ( $gateway->needs_setup() ) {
wp_send_json_error( 'needs_setup' );
wp_die();
} else {
$gateway->update_option( 'enabled', 'yes' );
}
} else {
// Disable the gateway.
$gateway->update_option( 'enabled', 'no' );
}
wp_send_json_success( ! wc_string_to_bool( $enabled ) );
wp_die();
}
}
wp_send_json_error( 'invalid_gateway_id' );
wp_die();
}
2013-11-14 12:13:34 +00:00
}
WC_AJAX::init();