Commit a937fbe22e1 for woocommerce
commit a937fbe22e1006337b829bf5e21bc2bfb8ead9f2
Author: Luigi Teschio <gigitux@gmail.com>
Date: Mon Nov 23 11:33:04 2026 +0100
Fix product ownership checks in variation AJAX handlers (#71251)
(cherry picked from commit a4a052b1167fba65810d3718a1d169c6428b2a3b)
diff --git a/plugins/woocommerce/includes/class-wc-ajax.php b/plugins/woocommerce/includes/class-wc-ajax.php
index b85fab6e60e..5cc355fe73c 100644
--- a/plugins/woocommerce/includes/class-wc-ajax.php
+++ b/plugins/woocommerce/includes/class-wc-ajax.php
@@ -866,6 +866,29 @@ class WC_AJAX {
wp_die( -1 );
}
+ /**
+ * Check whether the current user can edit each variation.
+ *
+ * @param int[] $variation_ids Variation IDs.
+ * @param int|null $product_id Expected parent product ID, or null to allow any editable parent.
+ * @return bool
+ */
+ private static function can_edit_variations( $variation_ids, $product_id = null ) {
+ foreach ( $variation_ids as $variation_id ) {
+ $parent_id = wp_get_post_parent_id( $variation_id );
+
+ if ( null !== $product_id && $product_id !== $parent_id ) {
+ return false;
+ }
+
+ if ( ! current_user_can( 'edit_product', $parent_id ) ) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
/**
* Delete variations via ajax function.
*
@@ -874,9 +897,13 @@ class WC_AJAX {
public static function remove_variations() {
check_ajax_referer( 'delete-variations', 'security' );
- if ( current_user_can( 'edit_products' ) && isset( $_POST['variation_ids'] ) ) {
+ if ( isset( $_POST['variation_ids'] ) ) {
$variation_ids = array_map( 'absint', (array) wp_unslash( $_POST['variation_ids'] ) );
+ if ( ! self::can_edit_variations( $variation_ids ) ) {
+ wp_die( -1 );
+ }
+
foreach ( $variation_ids as $variation_id ) {
if ( 'product_variation' === get_post_type( $variation_id ) ) {
$variation = wc_get_product( $variation_id );
@@ -896,7 +923,7 @@ class WC_AJAX {
public static function save_attributes() {
check_ajax_referer( 'save-attributes', 'security' );
- if ( ! current_user_can( 'edit_products' ) || ! isset( $_POST['data'], $_POST['post_id'] ) ) {
+ if ( ! isset( $_POST['data'], $_POST['post_id'] ) || ! current_user_can( 'edit_product', absint( $_POST['post_id'] ) ) ) {
wp_die( -1 );
}
@@ -945,7 +972,7 @@ class WC_AJAX {
public static function add_attributes_and_variations() {
check_ajax_referer( 'add-attributes-and-variations', 'security' );
- if ( ! current_user_can( 'edit_products' ) || ! isset( $_POST['data'], $_POST['post_id'] ) ) {
+ if ( ! isset( $_POST['data'], $_POST['post_id'] ) || ! current_user_can( 'edit_product', absint( $_POST['post_id'] ) ) ) {
wp_die( -1 );
}
@@ -1006,7 +1033,7 @@ class WC_AJAX {
public static function add_variation() {
check_ajax_referer( 'add-variation', 'security' );
- if ( ! current_user_can( 'edit_products' ) || ! isset( $_POST['post_id'], $_POST['loop'] ) ) {
+ if ( ! isset( $_POST['post_id'], $_POST['loop'] ) || ! current_user_can( 'edit_product', absint( $_POST['post_id'] ) ) ) {
wp_die( -1 );
}
@@ -1034,19 +1061,15 @@ class WC_AJAX {
public static function link_all_variations() {
check_ajax_referer( 'link-variations', 'security' );
- if ( ! current_user_can( 'edit_products' ) ) {
+ $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
+
+ if ( ! $post_id || ! current_user_can( 'edit_product', $post_id ) ) {
wp_die( -1 );
}
wc_maybe_define_constant( 'WC_MAX_LINKED_VARIATIONS', 50 );
wc_set_time_limit( 0 );
- $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
-
- if ( ! $post_id ) {
- wp_die();
- }
-
$product = wc_get_product( $post_id );
$number_created = self::create_all_product_variations( $product );
@@ -3220,7 +3243,7 @@ class WC_AJAX {
check_ajax_referer( 'load-variations', 'security' );
- if ( ! current_user_can( 'edit_products' ) || empty( $_POST['product_id'] ) ) {
+ if ( empty( $_POST['product_id'] ) || ! current_user_can( 'edit_product', absint( $_POST['product_id'] ) ) ) {
wp_die( -1 );
}
@@ -3270,12 +3293,20 @@ class WC_AJAX {
check_ajax_referer( 'save-variations', 'security' );
- // Check permissions again and make sure we have what we need.
- if ( ! current_user_can( 'edit_products' ) || empty( $_POST ) || empty( $_POST['product_id'] ) ) {
+ if ( empty( $_POST['product_id'] ) ) {
+ wp_die( -1 );
+ }
+
+ $product_id = absint( wp_unslash( $_POST['product_id'] ) );
+ if ( ! current_user_can( 'edit_product', $product_id ) ) {
+ wp_die( -1 );
+ }
+
+ $variation_ids = isset( $_POST['variable_post_id'] ) ? array_map( 'absint', (array) wp_unslash( $_POST['variable_post_id'] ) ) : array();
+ if ( ! self::can_edit_variations( $variation_ids, $product_id ) ) {
wp_die( -1 );
}
- $product_id = absint( $_POST['product_id'] );
WC_Admin_Meta_Boxes::$meta_box_errors = array();
WC_Meta_Box_Product_Data::save_variations( $product_id, get_post( $product_id ) );
@@ -3849,7 +3880,7 @@ class WC_AJAX {
check_ajax_referer( 'bulk-edit-variations', 'security' );
// Check permissions again and make sure we have what we need.
- if ( ! current_user_can( 'edit_products' ) || empty( $_POST['product_id'] ) || empty( $_POST['bulk_action'] ) ) {
+ if ( empty( $_POST['product_id'] ) || empty( $_POST['bulk_action'] ) || ! current_user_can( 'edit_product', absint( $_POST['product_id'] ) ) ) {
wp_die( -1 );
}