2015-11-21 80 views
0

我有一个函数,它应该创建一个变体,并用值,变量产品的属性填充变体。为了变得更加灵活,我没有为产品属性创造价值,除了如何将变量传递给一个Woocomerce变量产品的变体属性?

我试了百种方法让价值坚持到变化,但他们没有。该变体是在后端创建的,但它没有任何值分配?????? PLEEEEASE帮助!!!!我在这里已经4天了。 ;(

归因于该产品属性的唯一值是“>> EMPTY < <”的值。/////////////变量$ DATA - >阵列(密钥 - >值),以使键==到产品atributes $产物 - > $ PRODUCT_ID

function add_variable_product_to_cart($product_id, $data) { 

$my_post = array(
    'post_title'=> 'Order_Printaway', 
    'post_name' => 'product_' . $data["itemIdentifier"], 
    'post_status' => 'publish', 
    'post_parent' => $product_id ,//post is a child post of product post 
    'post_type' => 'product_variation',//set post type to product_variation 
    'guid'=>home_url() . '/?product_variation=product_' . $data["itemIdentifier"] 
); 

//Insert ea. post/variation into database: 
$attID = wp_insert_post($my_post);  

update_post_meta($attID, '_price', $data["final_price"]); 
update_post_meta($attID, '_regular_price', $data["final_price"]); 

foreach($data as $key => $value){ 

    $avail_attributes = array($value); 
    wp_set_object_terms($product_id, $avail_attributes, $key); 

    $thedata[$key]['name'] = $value; 

} 

update_post_meta($attID,'_product_attributes',$thedata); 

if (! is_admin()) { 
    $found = false; 
    //check if product already in cart 
    if (sizeof(WC()->cart->get_cart()) > 0) { 
     // if product not found, add it 
     if (! $found){ 
      WC()->cart->add_to_cart($product_id, 1, $attID); 

     } 
    } 
    else{ 
     // if no products in cart, add it 
     WC()->cart->add_to_cart($product_id, 1, $attID); 
    } 
} 
} 

/////////////////////////////////////////////////// ////////////////// //////////////////////////////// //////////////////////////////////// OUTPUT ///////////// ////////////////////////////////////////////////// ///////

//产品的Atributes - >

阵列([a_color] =>阵列([名称] => a_color [值] => >> EMPTY < < [位置] => 0 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_creasing] => Array([name] => a_creasing [value] => >> EMPTY < < [position] => 1 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_finishing] => Array [(name] => a_finishing [value] => >> EMPTY < < [position] => 2 [is_visible] => 0 [is_variation] => is_taxonomy] => 0)[a_finishing_desc] => Array([name] => a_finishing_desc [value] => >> EMPTY < < [position] => 3 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_format_dir] => Array([name] => a_format_dir [value] => >> EMPTY < < [position] => 4 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_page] => Array([name] => a_page [value] => >> EMPTY < < [position] => 5 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0) [一篇论文] =>数组([name] => a_paper [value] => >> EMPTY < < [position] => 6 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_paper_desc] =>数组([name] => a_paper_desc [value] => >> EMPTY < < [position] => 7 [is_visible] => [name] => a_perforation [value] => >> EMPTY < < [position] => 8 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_processing] => Array([name ] => a_processing [value] => >> EMPTY < < [position] => 9 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[product] => Array([name] = > product [value] => >> EMPTY < < [position] => 10 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[shipping] => Array([name] => shipping [value] => >> EMPTY < < [position] => 11 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0))

////我试图在Variation

Array([languageCode] => Array([name] => en)[countryCode] => Array([name] => GB)[saveItem] => Array([name] =>数组([name] => A_1stg)[a_format] => Array([name] => ([name] => A_105_x_148_mm_DIN-A-6)[a_format_dir] => Array([name] => A_panel_format)[a_paper] => Array([name] => A_115gqm_Bilderdruck)[a_paper_desc] => Array([name] => A_paper_gloss_no_core)[a_color] => Array([name] => A_4/0-farbig_Euroskala)[ a_finishing] => Array([name] => A_Ohne_Finishing)[a_finishing_desc] => Array([name] => A_Ohne_Veredelung)[a_processing] => Array([name] => A_Cutt)[a_creasing] => Array([name ] => A_No_Groove)[a_perforation] => Array([name] => A_No_Perfo)[quantity] => Array([name] => 100_quantity)[shipping] => Array([name] => standard)[priceNet] => Array([name] => 8,31)[priceGross] => Array([name] => 9,97)[shippingNet] => Array([name] => 4,13)[shippingGross] => Array([name] => 4,96)[final_price] => Array([name] => 17。916))get_post_meta(757,'_product_attributes',true))Array([0] => Array([languageCode] => Array([name] => en)[countryCode] => Array([名称] => GB)[saveItem] => Array([name] =>按名称保存项目)[itemIdentifier] => Array([name] => asdasadasds)[product] => Array([name] => flyer )[a_page] =>数组([name] => A_1stg)[a_format] => Array([name] => A_105_x_148_mm_DIN-A-6)[a_format_dir] => Array([name] => A_panel_format)[a_paper] = Array([name] => A_4/0-farbig_Euroskala)[a_finishing] => Array([name] => A_115gqm_Bilderdruck)[a_paper_desc] => Array([name] => A_paper_gloss_no_core)[a_color] => [name] => A_Ohne_Finishing)[a_finishing_desc] => Array([name] => A_Ohne_Veredelung)[a_processing] => Array([name] => A_Cutt)[a_creasing] => Array([name] => A_No_Groove) a_perforation] => Array([name] => A_No_Perfo)[quantity] => Array([name] => 100_quantity)[shipping] => Array([name] => standard )[arrayNet] => Array([name] => 8,31)[priceGross] => Array([name] => 9,97)[shippingNet] => Array([name] => 4,13)[ shippingGross] => Array([name] => 4,96)[final_price] => Array([name] => 17.916)))

回答

0

我挣扎于同一个问题,关键在于属性的分类法。

如果标记为分类的属性,那么它需要slu instead,而不是字符串基于文本的attrs。所以首先检查

 if ($attribute['is_taxonomy']) { 
      $options = wc_get_product_terms($post_id, $attribute['name'], array('fields' => 'slugs')); 
     } else { 
      $options = explode(WC_DELIMITER, $attribute['value']); 
     } 

并用slug或字符串值填充$ attr_val。

update_post_meta($post_id, 'attribute_' . sanitize_title($attribute['name']), $attr_val); 

WC_AJAX::save_attributes()可能会有所帮助

+0

谢谢。 。 。我会试一下! –