Ajax Woocommerce-无法删除产品变体
使用WooCommerce 2.6.1 我无法删除可变产品的产品变体:ajax调用后,记录仍在数据库中。 似乎ajax调用没有通过:放入错误日志(print_r('remove_variation',true));不输出任何内容(wc ajax.php类中的第387行)。 操作被添加到类的构造函数中。函数public function remove_variation()只是没有被调用 有没有人遇到过同样的问题,并找到了解决办法Ajax Woocommerce-无法删除产品变体,ajax,wordpress,woocommerce,Ajax,Wordpress,Woocommerce,使用WooCommerce 2.6.1 我无法删除可变产品的产品变体:ajax调用后,记录仍在数据库中。 似乎ajax调用没有通过:放入错误日志(print_r('remove_variation',true));不输出任何内容(wc ajax.php类中的第387行)。 操作被添加到类的构造函数中。函数public function remove_variation()只是没有被调用 有没有人遇到过同样的问题,并找到了解决办法 /** * Trash a variation, don't de
/**
* Trash a variation, don't delete it permanently.
*
* This is hooked to
* Hijack WooCommerce's WC_AJAX::remove_variation() "Delete Variation" Trash a variation if it is a subscription variation via ajax function
*/
public static function remove_variations() {
if ( isset( $_POST['variation_id'] ) ) { // removing single variation
error_log("here3");
check_ajax_referer( 'delete-variation', 'security' );
$variation_ids = array( $_POST['variation_id'] );
error_log($_POST['variation_id']);
} else { // removing multiple variations
error_log("here4");
check_ajax_referer( 'delete-variations', 'security' );
$variation_ids = (array) $_POST['variation_ids'];
}
foreach ( $variation_ids as $variation_id ) {
$variation_post = get_post( $variation_id );
error_log(print_r($variation_post, ));
if ( $variation_post && $variation_post->post_type == 'product_variation' ) {
$variation_product = get_product( $variation_id );
if ( $variation_product && $variation_product->is_type( 'subscription_variation' ) ) {
wp_trash_post( $variation_id );
}
}
}
die();
}
删除&&$variation\u product->is\u type('subscription\u variation')以解决无法删除的变体问题。应提供补丁,已报告问题。完全删除变体
这是第三版的+
(此代码将放在函数或自定义插件中:而不是Rest api)
删除变体不会删除样例
这只会使样例被禁用。(这可能有点尴尬)
如果你不想在你的产品组合的变化了,你应该删除它。
如果您已经使用slug(SKU)中的可识别字符串创建了变体,则可以使用以下代码
function delete_variation($product_cat="all",$Sku__search_string="",$force_delete=false,$document_it=false){
// 'posts_per_page' => -1: goes through all posts
if($product_cat=="all") {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
} else {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'product_cat' => $product_cat
);
}
$query = new WP_Query($args);
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
$product = wc_get_product($post_id);
$product_id = $product->get_id(); //same as post_id
//if you want to see what you are doing put $document_it to true;
if($document_it) echo "<br>*********** $product_id ****************<br>";
if($Sku__search_string!="") {
$variations = $product->get_available_variations();
foreach($variations as $variation){
//get the SKU slug of the variation
$sku=$variation["sku"];
//get the variation id
$variation_id=$variation['variation_id'];
if($document_it) echo "varid $variation_id <br>";
//and then get the actual variation product
$var_product=wc_get_product($variation_id);
//Check if the search_string is in the slug
//You can modify this, or modify the $args above,
//if you have other criteria
if(stripos($sku,$Sku__search_string)>0){
$is_deleted=false;
//here the variation is deleted
$is_deleted=$var_product->delete($force_delete);
if($is_deleted){
if($document_it) echo "<br>Deleted: $sku";
} else {
if($document_it) echo "<br>Not deleted: $sku";
}
}
}
}
}
}
function delete\u variation($product\u cat=“all”、$Sku\u search\u string=“”、$force\u delete=false、$document\u it=false){
//“每页帖子”=>-1:浏览所有帖子
如果($product_cat==“all”){
$args=数组(
“post_类型”=>“产品”,
“每页帖子数”=>-1,
);
}否则{
$args=数组(
“post_类型”=>“产品”,
“每页帖子数”=>-1,
“产品目录”=>$product目录
);
}
$query=新的WP\u查询($args);
而($query->have_posts()){
$query->the_post();
$post_id=获取_id();
$product=wc\U get\U product($post\U id);
$product\U id=$product->get\U id();//与post\U id相同
//如果你想知道你在做什么,就把$document_设置为true;
if($document_-it)echo“
**************$product_-id***********************
”;
如果($Sku\u搜索\u字符串!=“”){
$variations=$product->get_available_variations();
foreach($变更为$变更){
//获取变体的SKU段塞
$sku=$variation[“sku”];
//获取变体id
$variation_id=$variation['variation_id'];
如果($document_it)回显“varid$variation_id
”;
//然后得到实际变化的乘积
$var\u product=wc\u get\u product($variation\u id);
//检查搜索字符串是否在段塞中
//您可以修改它,或者修改上面的$args,
//如果你有其他标准
如果(stripos($sku,$sku\u search\u string)>0{
$is_deleted=false;
//此处删除了变体
$is\u deleted=$var\u product->delete($force\u delete);
如果($已删除){
如果($document_it)回显“
已删除:$sku”;
}否则{
如果($document_it)回显“
未删除:$sku”;
}
}
}
}
}
}
如果你想这似乎是因为WooCommerce订阅插件。我建议将此提交给Woo支持部门,以便他们能够修复它。完成了。一个星期以来,这些支持都没有反应,我在这里发帖后,他们神奇地醒了过来。不过,我最终调试了自己,因为他们对我的结论表示怀疑。因此,我指出了问题的症结所在(见下面我的答案),现在艰苦的工作已经完成,他们应该能够提供补丁。注意:这是一个商业插件,我不应该被迫贡献。