如何将变量传递给Woocomerce变量产品的变量属性?

时间:2015-11-21 16:58:43

标签: woocommerce

我有一个函数,它应该创建一个变体,并用值,变量产品的属性填充变体。为了变得更加灵活,除了

之外,我没有为产品属性创建价值

我已经尝试了一百种方法使价值观坚持变异,但他们不这样做。变化是在后端创建的,但它没有任何值的分布? PLEEEEASE帮助!!!!我现在在这4天了。 ;(

归因于该产品属性的唯一值是">> EMPTY<<<"值。 /////////////变量$ data - >数组(Key - > Value),以便键==产品属于$ product - > $ 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);
    }
}
}

/////////////////////////////////////////////// ////////////////////// //////////////////////////// ////////////////////////////////////////输出///////// ////////////////////////////////////////////////// ///////////

//产品的属性 - >

数组([a_color] =>数组([name] => a_color [value] =>>> EMPTY<< [position] => 0 [is_visible] => 0 [is_variation ] => 1 [is_taxonomy] => 0)[a_creasing] =>数组([name] => a_creasing [value] =>>> EMPTY<< [position] => 1 [ is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_finishing] =>数组([name] => a_finishing [value] =>>> EMPTY<< [position] => 2 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_finishing_desc] =>数组([name] => a_finishing_desc [value] => >> EMPTY<< [position] => 3 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_format_dir] =>数组([name] => ; a_format_dir [value] =>>> EMPTY<< [position] => 4 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_page] => ;数组([name] => a_page [value] =>>> EMPTY<< [position] => 5 [is_visib le] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_paper] =>数组([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] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_perforation] =>数组([name] => a_perforation [value] =>>> EMPTY<< [position] => 8 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[a_processing] =>数组([name] => a_processing [value] =>>> EMPTY<< [position] => 9 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[product] =>数组([name] => product [value] =>>> EMPTY<< [position] => 10 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0)[shipping] =>数组([name] => shipping [value] =>>> EMPTY<< [position] => 11 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0))

////我想在Variation

中引入的数据数组

Array([languageCode] => Array([name] => en)[countryCode] =>数组([name] => GB)[saveItem] =>数组([name] => ;按名称保存项目)[itemIdentifier] =>数组([名称] => asdasadasds)[产品] =>数组([名称] =>传单)[a_page] =>数组([名称] = > A_1stg)[a_format] =>数组([名称] => A_105_x_148_mm_DIN-A-6)[a_format_dir] =>数组([名称] => A_panel_format)[a_paper] =>数组([名称] ] => A_115gqm_Bilderdruck)[a_paper_desc] =>数组([name] => A_paper_gloss_no_core)[a_color] =>数组([name] => A_4 / 0-farbig_Euroskala)[a_finishing] =>数组( [name] => A_Ohne_Finishing)[a_finishing_desc] =>数组([名称] => A_Ohne_Veredelung)[a_processing] =>数组([名称] => A_Cutt)[a_creasing] =>数组([名称] ] => A_No_Groove)[a_perforation] =>数组([名称] => A_No_Perfo)[数量] =>数组([名称] => 100_quantity)[shipping] =>数组([名称] = > st andard)[priceNet] =>数组([name] => 8,31)[priceGross] =>数组([name] => 9,97)[shippingNet] =>数组([name] => 4,13)[shippingGross] =>数组([name] => 4,96)[final_price] =>数组([name] => 17.916))get_post_meta之后的变体的属性(757,' _product_attributes',true))数组([0] =>数组([languageCode] =>数组( [name] => en)[countryCode] =>数组([name] => GB)[saveItem] =>数组([name] =>按名称保存项目)[itemIdentifier] =>数组([name] => asdasadasds)[product] => Array([name] => flyer)[a_page] =>数组([name] => A_1stg)[a_format] =>数组([ name] => A_105_x_148_mm_DIN-A-6)[a_format_dir] =>数组([name] => A_panel_format)[a_paper] =>数组([name] => A_115gqm_Bilderdruck)[a_paper_desc] =>数组([name] => A_paper_gloss_no_core)[a_color] =>数组([name] => A_4 / 0-farbig_Euroskala)[a_finishing] =>数组([name] => A_Ohne_Finishing)[a_finishing_desc] => ;数组([名称] => A_Ohne_Veredelung)[a_processing] =>数组([名称] => A_Cutt)[a_creasing] =>数组([名称] => A_No_Groove)[a_perforati on] =>数组([name] => A_No_Perfo)[数量] =>数组([name] => 100_quantity)[shipping] =>数组([name] =>标准)[priceNet] =>数组([name] => 8,31)[priceGross] =>数组([name] => 9,97)[shippingNet] =>数组([name] => 4,13)[shippingGross] =>数组([name] => 4,96)[final_price] =>数组([name] => 17.916)))

1 个答案:

答案 0 :(得分:0)

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

如果属性标记为分类,那么它需要slug而不是基于文本的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或string值填充$ attr_val。

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

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