У меня есть скрипт HP для обновления варианта WooCommerce.После запуска кода у меня появились новые варианты в бэкэнде, но они не будут отображаться в интерфейсе.Вместо этого я получаю сообщение об ошибке: «На данный момент товар отсутствует на складе и недоступен.»
function UpdateProduct() {
// Update BASE product
extract($_POST);
// Product Array
$BaseProdArray = array(
'ID' => $pid,
'post_title' => $title,
'post_content' => utf8_encode($desc)
);
// Disable filtering
kses_remove_filters();
WriteLog('Filters removed.');
// Update the Base Product
wp_update_post( $BaseProdArray, $wp_error );
// Delete current Featured Image
// Check if post has featured image
if (has_post_thumbnail($pid)) {
// Get featured image id from this post
$feat_img_id = get_post_thumbnail_id($pid);
// Delete featured image from post
wp_delete_attachment($feat_img_id, true);
}
// Upload new Featured Image
SetProductFeaturedImage($pid,$feat_img,$slug,$title);
// Update Custom Fields
update_post_meta($pid,'custom_permalink',$cust_permalink);
update_post_meta($pid,'levertijd_select',$lev);
update_post_meta($pid,'wpseo_title',$seo_title);
update_post_meta($pid,'wpseo_metadescription',$seo_desc);
update_post_meta($pid,'wpseo_focuskeyword',$seo_kw);
update_post_meta($pid,'_yoast_wpseo_focuskw',$seo_kw);
update_post_meta($pid,'_yoast_wpseo_focuskw_text_input',$seo_kw);
update_post_meta($pid,'_yoast_wpseo_metakeywords',$tag);
update_post_meta($pid,'_yoast_wpseo_metadesc',$seo_desc);
// Update HYP price
update_post_meta( $pidhyp, '_regular_price', $price_hyp );
update_post_meta( $pidhyp, '_price', $price_hyp );
wc_delete_product_transients( $pidhyp );
// Update BIN price
update_post_meta( $pidbin, '_regular_price', $price_bin );
update_post_meta( $pidbin, '_price', $price_bin );
wc_delete_product_transients( $pidbin );
// Update NAC price
update_post_meta( $pidnac, '_regular_price', $price_nac );
update_post_meta( $pidnac, '_price', $price_nac );
wc_delete_product_transients( $pidnac );
// Update MUL price
update_post_meta( $pidmul, '_regular_price', $price_mul );
update_post_meta( $pidmul, '_price', $price_mul );
wc_delete_product_transients( $pidmul );
// Delete ALL variations
global $product;
$product = wc_get_product($pid);
$variation_ids = array();
$targeted_attribute = 'pa_opties';
if( $product->is_type('variable') ){
foreach($product->get_available_variations() as $variation_values ){
foreach($variation_values['attributes'] as $key => $attribute_value ){
$variation_ids[] = $variation_values['variation_id'];
wp_trash_post($variation_values['variation_id']);
}
}
}
// Delete Attributes
$variatieOpties = wp_get_post_terms($pid, 'pa_opties');
if (!empty($variatieOpties)) {
foreach ($variatieOpties as $variatieOptie) {
wp_remove_object_terms($pid, $variatieOptie->term_id, 'pa_opties');
}
}
// Set Variations Attributes
if ($hyp=='yes') {
$hypattribute = $attrhyp;
}
if ($bin=='yes') {
$binattribute = 'Binaurale';
}
if ($nac=='yes') {
$nacattribute = 'Nachtsessie';
}
if ($mul=='yes') {
$mulattribute = 'Multiliminal';
}
$thedata = Array(
'pa_opties' => Array(
'name' => 'pa_opties',
'value' => '',
'is_visible' => '0',
'is_variation' => '1',
'is_taxonomy' => '1'
)
);
update_post_meta ($pid,'_product_attributes',$thedata);
$avail_attributes = array($hypattribute,$binattribute,$nacattribute,$mulattribute);
wp_set_object_terms ($pid,'variable','product_type', true);
wp_set_object_terms ($pid, $avail_attributes, 'pa_opties', true);
// HYP: Add 1 Variation (HYP)
if ($hyp=='yes') {
// Re-Add this variation
AddVariationHyp($pid,$price_hyp,$sku,$download_path,$download_name_hyp,$hypattribute);
}
// BIN: Add 1 Variation (BIN)
if ($bin=='yes') {
AddVariationBin($pid,$price_bin,$sku,$download_path,$download_name_bin,$binattribute);
}
// NAC: Add 1 Variation (NAC)
if ($nac=='yes') {
AddVariationNac($pid,$price_nac,$sku,$download_path,$download_name_nac,$nacattribute);
}
// MUL: Add 1 Variation (MUL)
if ($mul=='yes') {
AddVariationMul($pid,$price_mul,$sku,$download_path,$download_name_mul,$mulattribute);
}
// Update tags
$TagsArr = wp_get_object_terms( $pid,'product_tag' );
foreach($TagsArr as $t) {
WriteLog($t->name);
wp_remove_object_terms( $pid, $t->name, 'product_tag' );
}
$tag = strtolower($tag);
if(isset($tag)) {
if(preg_match('/,/',$tag)) {
$TagsArr=explode(',',$tag);
foreach($TagsArr as $t)
AttachTerms($pid,$t,'product_tag');
}
else
AttachTerms($pid,$tag,'product_tag');
}
// Re enable filtering
kses_init_filters();
}
function AddVariationHyp($PID, $price_hyp, $sku, $download_path, $download_name_hyp, $hypattribute) {
$VarHypProdArray = array(
'post_author' => 1,
'post_title' => '',
'post_name' => '',
'post_status' => "publish",
'post_parent' => $PID,
'post_type' => "product_variation",
'guid' => home_url() . '/?product_variation=product-' . $PID . '-variation-' . 'HYP'
);
$hypattribute = strtolower($hypattribute);
// Insert Variation Hyp
$var_hyp_PID = wp_insert_post( $VarHypProdArray );
echo 'PIDHYP='.$var_hyp_PID."\r\n";
// General download data
update_post_meta($var_hyp_PID, 'attribute_pa_opties', $hypattribute);
update_post_meta($var_hyp_PID, '_price', $price_hyp);
update_post_meta($var_hyp_PID, '_regular_price', $price_hyp);
update_post_meta($var_hyp_PID, '_sku', $sku . 'HYP');
update_post_meta($var_hyp_PID, '_stock_status', 'instock');
update_post_meta($var_hyp_PID, '_downloadable', 'yes');
update_post_meta($var_hyp_PID, '_virtual', 'yes');
update_post_meta($var_hyp_PID, '_download_limit', '5' );
update_post_meta($var_hyp_PID, '_download_expiry', '30');
update_post_meta($var_hyp_PID, '_download_type', 'music');
// Download File #1 - Intro
$downloadArrayHyp = array(
'name' => $download_name_hyp . ' - Intro',
'file' => $download_path . $download_name_hyp . ' - Intro.mp3'
);
$file_path = md5($download_path . $download_name_hyp . ' - Intro.mp3');
$_file_paths[ $file_path ] = $downloadArrayHyp;
do_action( 'woocommerce_process_product_file_download_paths', $var_hyp_PID, 0, $downloadArrayHyp );
update_post_meta( $var_hyp_PID, '_downloadable_files', $_file_paths);
// Download File #2 - Hyp
$downloadArrayHyp = array(
'name' => $download_name_hyp . ' - ' . ucwords($hypattribute),
'file' => $download_path . $download_name_hyp . ' - ' . ucwords($hypattribute) . '.mp3'
);
$file_path = md5($download_path . $download_name_hyp . ' - ' . ucwords($hypattribute));
$_file_paths[ $file_path ] = $downloadArrayHyp;
do_action( 'woocommerce_process_product_file_download_paths', $var_hyp_PID, 0, $downloadArrayHyp );
update_post_meta( $var_hyp_PID, '_downloadable_files', $_file_paths);
}
Я ожидаю увидеть новые варианты на главной странице продукта.Вместо этого я получаю сообщение об ошибке «На данный момент товар отсутствует на складе и недоступен».