php 在WooCommerce管理变量产品中启用其他变体图像

fruv7luv  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(109)

从1周开始,我尝试编写自定义的PHP和jQuery代码,以上传额外的变体图像到每个变体。我写了一些代码,但它不能正常工作。
使用下面的代码,我是:
1.无法删除选定的图像。
1.无法看到我上传的每个变化的图像(在前端部分)
任何帮助都是高度赞赏。

function custom_allow_multiple_variation_images( $loop, $variation_data, $variation ) {
    $variation_id = $variation->ID;
    $field_name = 'variation_image';
    $attachment_ids = get_post_meta( $variation_id, $field_name, true );

    echo '<div class="upload_variation_image">';
    echo '<h4>Fotoğraf</h4>';
    echo '<input type="hidden" name="' . $field_name . '[' . $loop . ']" id="' . $field_name . '" class="upload_image" value="' . esc_attr( implode( ',', (array) $attachment_ids ) ) . '" />';
    echo '<button class="upload_image_button button">Upload Image</button>';

    if ( $attachment_ids ) {
        $attachment_ids = explode( ',', $attachment_ids );
        foreach ( $attachment_ids as $attachment_id ) {
            $image_src = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
            if ( $image_src ) {
                echo '<div class="uploaded_image_wrapper">';
                echo '<img src="' . esc_url( $image_src[0] ) . '" alt="Uploaded Image" />';
                echo '<span class="delete_variation_image" data-attachment_id="' . $attachment_id . '">Sil</span>';
                echo '</div>';
            }
        }
    }

    echo '</div>';
}

add_action( 'woocommerce_variation_options', 'custom_allow_multiple_variation_images', 10, 3 );

function custom_save_variation_image( $variation_id ) {
    $field_name = 'variation_image';
    $attachment_ids = isset( $_POST[ $field_name ] ) ? array_map( 'absint', explode( ',', wc_clean( $_POST[ $field_name ][0] ) ) ) : array();

    update_post_meta( $variation_id, $field_name, implode( ',', $attachment_ids ) );
}

add_action( 'woocommerce_save_product_variation', 'custom_save_variation_image', 10, 1 );

add_action( 'woocommerce_save_product_variation', 'custom_save_variation_image', 10, 1 );

function custom_variation_image_scripts() {
    ?>
    <script>
        jQuery(document).ready(function($) {
            
            function custom_add_variation_image($wrapper) {
                var file_frame;
                if (file_frame) {
                    file_frame.open();
                    return;
                }
                file_frame = wp.media.frames.file_frame = wp.media({
                    title: 'Variation Image Select',
                    button: {
                        text: 'Add Additional Image'
                    },
                    multiple: true
                });

                file_frame.on('select', function() {
                    var attachment_ids = [];
                    var selection = file_frame.state().get('selection');
                    selection.map(function(attachment) {
                        attachment = attachment.toJSON();
                        attachment_ids.push(attachment.id);
                    });
                    $wrapper.find('.upload_image').val(attachment_ids.join(','));
                    $wrapper.find('.uploaded_image_wrapper').remove();
                    attachment_ids.forEach(function(attachment_id) {
                        var image_url = wp.media.attachment(attachment_id).get('url');
                        $wrapper.append('<div class="uploaded_image_wrapper"><img src="' + image_url + '" alt="Uploaded Image" /><span class="delete_variation_image" data-attachment_id="' + attachment_id + '">Sil</span></div>');
                    });
                    $wrapper.find('.upload_image').trigger('change'); // Trigger change event when selecting a variation image
                });
                file_frame.open();
            }

           
            function custom_remove_variation_image($wrapper, attachment_id) {
                var attachment_ids = $wrapper.find('.upload_image').val().split(',');
                var index = attachment_ids.indexOf(attachment_id);
                if (index !== -1) {
                    attachment_ids.splice(index, 1);
                    $wrapper.find('.upload_image').val(attachment_ids.join(','));
                }
                $wrapper.find('.uploaded_image_wrapper[data-attachment_id="' + attachment_id + '"]').remove();
                $wrapper.find('.upload_image').trigger('change'); // Trigger change event when removing a variation image
            }

            
            function custom_detect_variation_changes() {
                $('form.cart').on('change', '.variation-control', function() {
                    $('.single_add_to_cart_button').prop('disabled', false);
                });

                // When a variation image changes, enable "Add to Cart" button
                $('form.cart').on('change', '.upload_image', function() {
                    $('.single_add_to_cart_button').prop('disabled', false);
                });
            }

            // Medya yükleyici olaylarını ekle
            $(document).on('click', '.upload_image_button', function(e) {
                e.preventDefault();
                custom_add_variation_image($(this).closest('.upload_variation_image'));
            });

            $(document).on('click', '.delete_variation_image', function() {
                var attachment_id = $(this).data('attachment_id');
                custom_remove_variation_image($(this).closest('.upload_variation_image'), attachment_id);
            });

            
            custom_detect_variation_changes();
        });
    </script>
    <style>
        .uploaded_image_wrapper {
            margin: 10px 0;
            display: inline-block;
        }

        .uploaded_image_wrapper img {
            max-width: 100px;
            height: auto;
            margin-right: 10px;
        }

        .delete_variation_image {
            cursor: pointer;
            color: #a00;
        }
    </style>
    <?php
}

add_action( 'admin_footer', 'custom_variation_image_scripts' );

字符串
在@LoicTheAztec回答后,我可以从变体选项卡添加变体图像,但产品页面上没有显示任何内容。你可以看到下面的图像关于我的最新情况x1c 0d1x

mu0hgdu0

mu0hgdu01#

由于我已经实现了类似的自定义插件...下面找到一个简化和紧凑的工作代码版本。

PHP

add_action( 'woocommerce_variation_options', 'add_additional_variation_image_ids', 10, 3 );
function add_additional_variation_image_ids( $loop, $variation_data, $variation ) {
    $variation_id     = $variation->ID;
    $variation_object = wc_get_product($variation_id);

    $attachment_ids   = $variation_object->get_meta( 'additional_img_ids' ); // Get Attachments Ids
    $attachment_ids   = empty($attachment_ids) ? array( 0 => '' ) : $attachment_ids;
    $count            = count($attachment_ids);
    $placeholder      = esc_url( wc_placeholder_img_src() );

    $upload_img_txt   = esc_html__( 'Upload an image', 'woocommerce' );
    $remove_img_txt   = esc_html__( 'Remove this image', 'woocommerce' );
    $add_txt          = esc_html__( 'Add', 'woocommerce' );
    $remove_txt       = esc_html__( 'Remove', 'woocommerce' );

    echo '<div class="custom-uploads">
    <h4>' . __( 'Additional images:', 'woocommerce' ) . '</h4>';

    // Loop through each existing attachment image ID
    foreach( $attachment_ids as $index => $image_id ) {
        // Add an Image field
        printf('<div class="image-box"><p class="form-row form-row-wide upload_image">
            <a href="#" class="upload_image_button tips %s" data-tip="%s" rel="%s"><img src="%s" />
            <input type="hidden" name="additional_img_ids-%s-%s" class="upload_image_id" value="%s" /></a>
            <p></div>', 
            $image_id ? 'remove' : '', $image_id ? $remove_img_txt : $upload_img_txt, $variation_id,
            $image_id ? esc_url( wp_get_attachment_thumb_url( $image_id ) ) : $placeholder, $loop, $index, $image_id
        );
    }
    // Add the buttons
    printf('<div class="buttons-box"><p>
        <button type="button" class="add-slot" data-loop="%d">%s</button>
        <button type="button" class="remove-slot" data-loop="%d"%s>%s</button>
        <input type="hidden" name="slot-index-%d" value="%s" /><input type="hidden" name="ph-img-%s" value="%s" /></a><p></div>', 
        $loop, $add_txt, $loop, $count == 1 ? ' style="display:none;"' : '', $remove_txt, $loop, $count, $loop, $placeholder
    );
    echo '</div>';
}

add_action( 'woocommerce_admin_process_variation_object', 'save_additional_variation_image_ids', 10, 2 );
function save_additional_variation_image_ids( $variation, $i ) 
{
    if ( isset($_POST["slot-index-{$i}"]) && $_POST["slot-index-{$i}"] > 1 ) {
        $attachment_ids = array(); // Initialize

        // Loop through each posted attachment Id for the current variation
        for( $k = 0; $k < $_POST["slot-index-{$i}"]; $k++ ) {
            if ( isset($_POST["additional_img_ids-{$i}-{$k}"]) && ! empty($_POST["additional_img_ids-{$i}-{$k}"]) ) {
                $attachment_ids[$k] =esc_attr($_POST["additional_img_ids-{$i}-{$k}"]); // Set it in the array
            }
        }
        if( ! empty($attachment_ids) ) {
            $variation->update_meta_data( 'additional_img_ids', $attachment_ids ); // save
        }
    }
}

字符串

JavaScript / jQuery(嵌入在函数中,可从外部文件入队)

add_action( 'admin_footer', 'additional_variation_image_js' );
function additional_variation_image_js() {
    global $pagenow, $typenow;

if( in_array( $pagenow, array('post.php', 'post-new.php') ) && $typenow === 'product' ) :
?>
<script>
    jQuery(function($) {
        $('body').on('click', '.add-slot', function(){
            const parent      = $(this).closest('.custom-uploads'),
                loop          = $(this).data('loop');
            
            var lastImageBox  = $(parent).find('.image-box:last'),
                hiddenInput   = $('[name=slot-index-'+loop+']'),
                index         = parseInt(hiddenInput.val()),
                inputNameUpd  = 'additional_img_ids-'+loop+'-'+index,
                inputPropsUpd = {'name': inputNameUpd, 'value': null}
                placeholder   = $('[name=ph-img-'+loop+']').val(),
                clonedImgBox  = lastImageBox.clone().insertBefore('.buttons-box'); // Insert a clone

            clonedImgBox.find('.upload_image_id').prop(inputPropsUpd);
            clonedImgBox.find('img').prop('src', placeholder);
            clonedImgBox.find('a').removeClass('remove');
            hiddenInput.val(index+1);

            if ( index == 1 ) {
                $(this).parent().find('.remove-slot').show();
            }
        }).on('click', '.remove-slot', function(){
            const parent     = $(this).closest('.custom-uploads'),
                loop         = $(this).data('loop');
                
            var lastImageBox = $(parent).find('.image-box:last'),
                hiddenInput  = $('[name=slot-index-'+loop+']'),
                index        = parseInt(hiddenInput.val());

            lastImageBox.remove();
            hiddenInput.val(index-1);
            
            if ( index == 2 ) {
                $(this).hide();
            }
        });
    });
</script>
<?php
endif;
}

CSS(嵌入在函数中,可从外部文件入队)

add_action( 'admin_head', 'additional_variation_image_css', 9999 );
function additional_variation_image_css() {
    global $pagenow, $typenow;

    if( in_array( $pagenow, array('post.php', 'post-new.php') ) && $typenow === 'product' ) :
    ?>
    <style>
        .custom-uploads .image-box,
        .custom-uploads .buttons-box {
            float:left;
            width:76px;
        }

        .custom-uploads .upload_image,
        .buttons-box p {
            margin-top:0;
            width:auto;
            float:none;
        }
        .custom-uploads a {
            margin-right:12px;
        }
        .add-slot {
            margin:6px 0;
        }
    </style>
    <?php
    endif;
}


代码放在子主题的functions.php文件或插件文件中。测试和作品。
你会得到类似这样的东西:
x1c 0d1x的数据
因此,现在,可以轻松地从产品变体中添加或删除其他图像...对于每个变体,使用唯一的自定义字段将它们保存为附件ID数组。

相关问题