Home

Friday, January 23, 2015

How to add product in woocommerce using programmatically with wpml

function woocommerce_add_products_dynamicaly()
 {
        //empty cart
        global $woocommerce;
        $woocommerce->cart->empty_cart();
        // Remove default cart message
        $woocommerce->clear_messages();
       
        //price
        $invoiceprice = 1;
       
        //Generate title
        $timestampedtitle = rand()." Date: ".date("d/m/Y")." Amount: £".$invoiceprice;
       
        //Generate message
        $message = date_timestamp_get(date_create())." Date: ".date('m/d/Y h:i:s a', time())." Invoice amount: £".$invoiceprice;

        $post = array(
        'post_author' => '2',
        'post_status' => "publish",
        'post_title' => $timestampedtitle,
        'post_content' => $message,
        'post_parent' => '',
        'post_type' => "product",
        //'post_status' => 'private',
        );




        //Create post
        $post_id = wp_insert_post( $post, $wp_error );
        if($post_id){
        $attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true);
        add_post_meta($post_id, '_thumbnail_id', $attach_id);
        }
        $sku = randomsku();
        wp_set_object_terms($post_id, 'simple', 'product_type');
        update_post_meta( $post_id, '_visibility', 'search' );
        update_post_meta( $post_id, '_stock_status', 'instock');
        update_post_meta( $post_id, '_virtual', 'yes');
        update_post_meta( $post_id, '_regular_price', $invoiceprice );
        update_post_meta( $post_id, '_sale_price', $invoiceprice );
        update_post_meta( $post_id, '_purchase_note', "" );
        update_post_meta( $post_id, '_featured', "no" );
        update_post_meta( $post_id, '_weight', "" );
        update_post_meta( $post_id, '_length', "" );
        update_post_meta( $post_id, '_width', "" );
        update_post_meta( $post_id, '_height', "" );
        update_post_meta($post_id, '_sku', $sku);
        update_post_meta( $post_id, '_product_attributes', array());
        update_post_meta( $post_id, '_sale_price_dates_from', "" );
        update_post_meta( $post_id, '_sale_price_dates_to', "" );
        update_post_meta( $post_id, '_price', $invoiceprice );
        update_post_meta( $post_id, '_sold_individually', "yes" );
        update_post_meta( $post_id, '_manage_stock', "yes" );
        update_post_meta( $post_id, '_backorders', "no" );
        update_post_meta( $post_id, '_stock', "1" );
        update_post_meta( $post_id, '_et_pb_page_layout', 'et_full_width_page' );

         $languages = icl_get_languages('skip_missing=0&orderby=code');
        if(!empty($languages)){
            foreach($languages as $l){           
                if($l['active']==0)
                {
                    mwm_wpml_translate_post( $post_id, 'product', $l['language_code'],$sku);           
                }
            }
        }
               
        if( $woocommerce->cart )
        {
            $woocommerce->cart->add_to_cart( $post_id, $quantity=1 );
        }
            $url = $woocommerce->cart->get_checkout_url();
            header("Location: $url");
 }
add_shortcode('calculation_product', 'woocommerce_add_products_dynamicaly');


function mwm_wpml_translate_post( $post_id, $post_type, $lang,$sku )
{
        // Include WPML API
        include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
        //price
        $invoiceprice = 1;
       
        //Generate title
        $timestampedtitle = rand()." Date: ".date("d/m/Y")." Amount: £".$invoiceprice;
       
        //Generate message
        $message = date_timestamp_get(date_create())." Date: ".date('m/d/Y h:i:s a', time())." Invoice amount: £".$invoiceprice;
       
        // Insert translated post
        $post = array(
        'post_author' => '2',
        'post_status' => "publish",
        'post_title' => $timestampedtitle,
        'post_content' => $message,
        'post_parent' => '',
        'post_type' => "product",
        //'post_status' => 'private',
        );
        $post_translated_id = wp_insert_post(  $post, $wp_error  );
       
        // Get trid of original post
        $trid = wpml_get_content_trid( 'post_' . $post_type, $post_id );
       
        // Get default language
        $default_lang = wpml_get_default_language();
       
        // Associate original post and translated post
        global $wpdb;
        $wpdb->update( $wpdb->prefix.'icl_translations', array( 'trid' => $trid, 'language_code' => $lang, 'source_language_code' => $default_lang ), array( 'element_id' => $post_translated_id ) );
       
        //price
        $invoiceprice = 1;
        //Generate message
        $message = date_timestamp_get(date_create())." Date: ".date('m/d/Y h:i:s a', time())." Invoice amount: £".$invoiceprice;
       
        wp_set_object_terms($post_translated_id, 'simple', 'product_type');               
        update_post_meta( $post_translated_id, '_visibility', 'search' );
        update_post_meta( $post_translated_id, '_stock_status', 'instock');
        update_post_meta( $post_translated_id, '_virtual', 'yes');
        update_post_meta( $post_translated_id, '_regular_price', $invoiceprice );
        update_post_meta( $post_translated_id, '_sale_price', $invoiceprice );
        update_post_meta( $post_translated_id, '_purchase_note', "" );
        update_post_meta( $post_translated_id, '_featured', "no" );
        update_post_meta( $post_translated_id, '_weight', "" );
        update_post_meta( $post_translated_id, '_length', "" );
        update_post_meta( $post_translated_id, '_width', "" );
        update_post_meta( $post_translated_id, '_height', "" );
        update_post_meta($post_translated_id, '_sku', $sku);
        update_post_meta( $post_translated_id, '_product_attributes', array());
        update_post_meta( $post_translated_id, '_sale_price_dates_from', "" );
        update_post_meta( $post_translated_id, '_sale_price_dates_to', "" );
        update_post_meta( $post_translated_id, '_price', $invoiceprice );
        update_post_meta( $post_translated_id, '_sold_individually', "yes" );
        update_post_meta( $post_translated_id, '_manage_stock', "yes" );
        update_post_meta( $post_translated_id, '_backorders', "no" );
        update_post_meta( $post_translated_id, '_stock', "1" );
        update_post_meta( $post_translated_id, '_et_pb_page_layout', 'et_full_width_page' );
}


 function randomsku()
 {
        $characters = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
        $characters_length = (strlen($characters)-1)/2;
        $token = explode(",",$characters);
        // length of the password
        $string_length=10;
        for($i=0;$i<$string_length;$i++)
        {
            $rand = rand(0,$characters_length);
            $rand_str.= $token[$rand];
        }
        return $rand_str;
 }

1 comment:

  1. Thank you for sharing excellent information. Your website is very cool. Fully useful your blog post... Online shopping site for women's clothing in india

    ReplyDelete

rathoddhirendra.blogspot.com-Google pagerank and Worth