如何修复“此产品目前无货,无法使用。”更新版本后出现错误

时间:2018-12-29 00:29:46

标签: php wordpress woocommerce

我有一个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);
}

我希望在产品首页上看到新的变化。相反,我收到错误消息“该产品当前无货且不可用。”

0 个答案:

没有答案