• Resolved Gourav RR

    (@gouravrr)


    Hello,

    I am creating lots of products programatically, simple products are displayed correct on front-end. and also Variable products with some variations are showing correct, but the translation of this variable product is not showing correct(out of stock), but in back-end everything is fine, unless I go to each variable product and do manual update from there and update the translation of this product; then the variable products are also displayed correct.

    what is the issue here ???

    after creating a product I used the
    wc_delete_product_transients( $post_id );
    at the end of my update function.

    Woocommerce Version : 2.5.5
    WPML Plugins Versions : see attachment
    https://drive.google.com/file/d/0B1BSUwinVFKzdm9zU2tFQnlqMmc/view?usp=sharing

Viewing 12 replies - 1 through 12 (of 12 total)
  • Hello and thank you for contacting us.
    I am not sure why you are experiencing this problem.
    Are you also running the cleanup of transients for the variations as well?

    How are you creating these products programmatically? Can you share some code?

    Thread Starter Gourav RR

    (@gouravrr)

    //inserting product in NL language

    
    $post = array(
                'post_author'  => 1,
                'post_content' => $product_desc,
                'post_status'  => 'publish',
                'post_title'   => $product_name,
                'post_parent'  => '',
                'post_type'    => 'product'
            );
            $_POST['icl_post_language'] = $language_code = 'nl';
            $post_id = wp_insert_post($post);
            $nl_post_id = $post_id;
            $trigid = wpml_get_content_trid('post_product', $nl_post_id);
            if (!$post_id){
                return false;
            }
    

    after that I ‘ll update all post metas…
    and creating attributes and variations.

    wc_delete_product_transients( $nl_post_id );

    //inserting product in FR language

    
    $_POST['icl_post_language'] = $language_code = 'fr';
            $post_fr = array(
                'post_author'  => 1,
                'post_content' => $product_data['description1'],
                'post_status'  => 'publish',
                'post_title'   => $product_data['name'],
                'post_parent'  => '',
                'post_type'    => 'product'
            );
            $fr_post_id = wp_insert_post( $post_fr );
            $sitepress->set_element_language_details($fr_post_id, 'post_product', $trigid, $language_code);
    

    after that I ‘ll update all post metas…
    and creating attributes and variations.

    wc_delete_product_transients( $fr_post_id );

    Products in NL language are works fine but FR product(NL translations)
    are not showing fine on frontend(out of stock)

    can you help me out
    how can I clear th variation’s transient cache … ???

    I talked with our developers about this.
    You might want to try:

    
                   delete_transient( 'wc_product_children_' . $tr_product_id );
                   delete_transient( '_transient_wc_product_children_ids_' . $tr_product_id );
    

    Where $tr_product_id is the product id

    Thread Starter Gourav RR

    (@gouravrr)

    Hello,

    
    delete_transient( 'wc_product_children_' . $tr_product_id );
                   delete_transient( '_transient_wc_product_children_ids_' . $tr_product_id );
    
    

    this won’t work anymore,
    is there another way to solve this ???

    Can you give me more details please?
    Why this is not working for you?

    Thread Starter Gourav RR

    (@gouravrr)

    I am creating the variations for each variable product after creating the, product

    //inserting product variations
    function insert_product_variations ($post_id, $variations) {
        global $wpdb;
        global $posts_table;
    
        $post_title = get_the_title($post_id);
        $variable_ids = array();
        $tickets = new WC_Product_Variable($post_id);
        $variables = $tickets->get_available_variations();
    
        $data = $wpdb->get_results("DELETE FROM $posts_table WHERE post_parent=$post_id AND post_type='product_variation'");
    
        foreach ($variations as $index => $variation) {
            $variation_post = array(
                'post_title'  => $post_title,
                'post_name'   => $post_title,
                'post_status' => 'publish',
                'post_parent' => $post_id,
                'post_type'   => 'product_variation',
                'guid'        => home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $index
            );
            $variation_post_id = wp_insert_post($variation_post);
            foreach ($variation['attributes'] as $attribute => $value) {   
                $attribute_term = get_term_by('name', $value, 'pa_'.$attribute);
                update_post_meta($variation_post_id, 'attribute_pa_'.$attribute, $attribute_term->slug);
            }
            update_post_meta($variation_post_id, '_price', $variation['price']);
            update_post_meta($variation_post_id, '_regular_price', $variation['price']);
            update_post_meta($variation_post_id, '_sale_price', $variation['sale_price']);
            update_post_meta($variation_post_id, '_sale_price_dates_from', $variation['sale_price_dates_from']);
            update_post_meta($variation_post_id, '_sale_price_dates_to', $variation['sale_price_dates_to']);
            update_post_meta($variation_post_id, '_sku', $variation['sku']);
            update_post_meta($variation_post_id, '_manage_stock', $variation['manage_stock']);
            update_post_meta($variation_post_id, '_stock', $variation['stock']);
            update_post_meta($variation_post_id, '_backorders', $variation['backorders']);
        }
    }
    
    //inserting product attributs
    function insert_product_attributes ($post_id, $available_attributes, $variations) {
        foreach ($available_attributes as $attribute) {
            $values = array();
            foreach ($variations as $variation) {
                $attribute_keys = array_keys($variation['attributes']);
                foreach ($attribute_keys as $key) {
                    if ($key === $attribute) {
                        $values[] = $variation['attributes'][$key];
                    }
                }
            }
            $values = array_unique($values);
            wp_set_object_terms($post_id, $values, 'pa_' . $attribute);
        }
        $product_attributes_data = array();
        foreach ($available_attributes as $attribute) {
            $product_attributes_data['pa_'.$attribute] = array(
                'name'         => 'pa_'.$attribute,
                'value'        => '',
                'is_visible'   => '1',
                'is_variation' => '1',
                'is_taxonomy'  => '1'
            );
        }
        update_post_meta($post_id, '_product_attributes', $product_attributes_data);
    }

    Is this the whole code? Can you provide the full code so that our developers can review it.
    It might be that you have not created the proper relationships between the variations.

    Thread Starter Gourav RR

    (@gouravrr)

    Hello,

    when I am upadting the products programatically i delete all existing variations and create new variations.

    i know its wrong but how can i maintain the relationship and update existing variations of products???

    Thank you,
    GouravRR

    • This reply was modified 7 years, 11 months ago by Gourav RR.

    This is very wrong.
    Usually, when you create and translate variations – they have an entry in icl_translations table in the database that make them linked one to another between the languages.
    Maybe you should also update that entry as well.

    Thread Starter Gourav RR

    (@gouravrr)

    Hello @botzev,

    I ‘ve made changes in my code and resolved the above mentioned issue for the translated product.
    But there is a bit small issue is remaining, i.e. the product attributes are not set for the translation
    and when I goto the WooCommerce > WooCommerce Multilingual > Attributes
    and Synchronize attributes and update product variations, then everything is fine on frontend,

    is there any code snippet for this “Synchronize attributes and update product variations”,
    or
    how can I run this Synchronization programatically ??
    or
    can I execute this function wcml_sync_product_variations($taxonomy); to sync attributes.??

    Thank you,
    GouravRR

    • This reply was modified 7 years, 11 months ago by Gourav RR.

    Maybe you should take a look at the functions trbl_sync_variations() and wcml_sync_product_variations() – they might give you some ideas on how to implement it because they are the ones that are called when you press that button

    Closing this due to inactivity.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Translated Product Showing Out of Stock’ is closed to new replies.