Forum Replies Created

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter syafiqsyazre

    (@syafiqsyazre)

    Hi @crslz

    Okay noted. Thanks for your clarification

    Regards,

    Thread Starter syafiqsyazre

    (@syafiqsyazre)

    Hi @crslz

    The theme that I am using already override the woocommerce default template. What happen if i update the theme?

    Regards,

    Thread Starter syafiqsyazre

    (@syafiqsyazre)

    Hi @crslz,

    If i update the woocommerce plugin, is it the overridden template will disappear or remain there?

    Regards,

    Thread Starter syafiqsyazre

    (@syafiqsyazre)

    Hi @crslz

    Thanks for your help on the code. Already found a solution for that. Really appreciate it.

    Thanks & Regards,

    Thread Starter syafiqsyazre

    (@syafiqsyazre)

    Hi,

    That explains everything, thanks!

    Regards,

    Thread Starter syafiqsyazre

    (@syafiqsyazre)

    Hi @crslz

    Thanks for your code, it’s working now. Really appreciate your help.

    Only one thing I need to figure out right now. As I assigning the first image of gallery_image_ids as the main image_id, I need to hide/remove that first image in the gallery_image_ids because the image is showing twice in the product page gallery.

    Not sure which one is the best practice either I just remove it by using javascript or need to include into the function.

    Regards,

    Thread Starter syafiqsyazre

    (@syafiqsyazre)

    Hi @crslz

    Thanks for your help with the code. I’ve edited some of the code you provided and successfully filter the gallery image id by who’s logged in.

    function woocommerce_product_get_image_id_callback( $image_id, $product ) {	
        
        $new_image_id = "";
        $new_image_id = $image_id;
        
        // Check current user role
        $user = wp_get_current_user();
        if ( in_array( 'user1', (array) $user->roles ) ) {
            $currentUserRole = "user1";
        }
        else if ( in_array( 'user2', (array) $user->roles ) ) {
            $currentUserRole = "user2";
        }
        
        // Initializing array variable
        $itemsAtts = array();
        
        // Get all the gallery image id
        $attachment_ids = $product->get_gallery_attachment_ids();
        
        // Only assign the alt text that equal with user role to the array variable
        foreach( $attachment_ids as $attachment_id ) {
          $alt_text = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
          if ($alt_text == $currentUserRole) {
              $itemsAtts[] = $attachment_id;
          }  
        }
        
        // Check if image with alt text exist or not, if not just display placeholder image
        if (empty($itemsAtts)) {
             $image_id = $new_image_id;
        }
        else {
            $image_id = $itemsAtts[0];
        }
        
        // Specify an image gallery that has an alt text set
        $product->set_gallery_image_ids($itemsAtts);
        return $image_id;
    
    }
    add_filter( 'woocommerce_product_variation_get_image_id', 'woocommerce_product_get_image_id_callback', 10, 2 );
    add_filter( 'woocommerce_product_get_image_id', 'woocommerce_product_get_image_id_callback', 10, 2 );

    However, it is possible to hide the product variations if the image id is not specified. Means that, the user cannot see the product variations and add the product into the cart.

    Best regards,

    Thread Starter syafiqsyazre

    (@syafiqsyazre)

    Hi @crslz,

    Thanks for your prompt reply.

    What I am trying to achieve right now is to filter the product image using alt text that I entered.

    For example; I upload image-1(alt text=”user-1″) and image-2(alt text=”user-2″) in the product gallery of Product 1.

    When User 1 is logged into the Store, only image-1 is shown in the product gallery and image-2 is hidden. I am trying to match the alt text of the image with the current user who logged into the store.

    Is it doable?

    • This reply was modified 5 years, 2 months ago by syafiqsyazre.
    • This reply was modified 5 years, 2 months ago by syafiqsyazre.
Viewing 8 replies - 1 through 8 (of 8 total)