php Woocommerce - 在循环内显示变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13645085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:50:26  来源:igfitidea点击:

Woocommerce - Displaying variations inside the loop

phpwordpressvariablesloopswoocommerce

提问by Adam Collingburn

<?php

    switch ( $product->product_type ) {
        case "variable" :
            $link   = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
            $label  = apply_filters( 'variable_add_to_cart_text', __('Select options', 'woocommerce') );
        break;
        case "grouped" :
            $link   = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
            $label  = apply_filters( 'grouped_add_to_cart_text', __('View options', 'woocommerce') );
        break;
        case "external" :
            $link   = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
            $label  = apply_filters( 'external_add_to_cart_text', __('Read More', 'woocommerce') );
        break;
        default :
            $link   = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
            $label  = apply_filters( 'add_to_cart_text', __('Add to cart', 'woocommerce') );
        break;
    }

    printf('<a href="%s" rel="nofollow" data-product_id="%s" class="add_to_cart_button button product_type_%s">%s</a>', $link, $product->id, $product->product_type, $label);

?>

I'm trying to get variations to display inside the loop so customers can add variable products to their cart from the shop page (please see screenshot below)...

我正在尝试让变化显示在循环内,以便客户可以从商店页面将可变产品添加到他们的购物车(请参见下面的屏幕截图)...

http://cl.ly/image/42401k0X0X2I

http://cl.ly/image/42401k0X0X2I

I know I need to include the function-

我知道我需要包括功能-

get_available_variations();

And i'm pretty sure this already returns an array, it's just putting that array into a select dropdown + listing the variations (S,M,L,XL) and having a link to add that variation to the basket.

而且我很确定这已经返回了一个数组,它只是将该数组放入选择下拉列表中 + 列出变体(S、M、L、XL)并有一个链接将该变体添加到篮子中。

Cheers!

干杯!

回答by Bryce Wilson

The variations dropdown template file for single post pages is located here: woocommerce\templates\single-product\add-to-cart\variable.php

单个帖子页面的变体下拉模板文件位于:woocommerce\templates\single-product\add-to-cart\variable.php

Which requires the following script to pass the product variable information:

这需要以下脚本来传递产品变量信息:

<script type="text/javascript">
var product_variations_<?php echo $post->ID; ?> = <?php echo json_encode( $available_variations ) ?>;
</script>

as well as the following hidden field:

以及以下隐藏字段:

<input type="hidden" name="variation_id" value="" /> - where the value is the variation ID

I hope that is a start others can help build upon.

我希望这是其他人可以帮助建立的一个开始。

回答by Veda Holcomb

I found your post while trying to solve the same problem. I finally found...

我在尝试解决同样的问题时发现了您的帖子。我终于找到了...

function woocommerce_variable_add_to_cart() {
    global $product;

    // Enqueue variation scripts
    wp_enqueue_script( 'wc-add-to-cart-variation' );

    // Load the template
    woocommerce_get_template( 'single-product/add-to-cart/variable.php', array(
            'available_variations'  => $product->get_available_variations(),
            'attributes'            => $product->get_variation_attributes(),
            'selected_attributes'   => $product->get_variation_default_attributes()
        ) );
}
}

in

woocommerce-template.php

woocommerce-template.php

This works for me in loop/add-to-cart.php

这在 loop/add-to-cart.php 中对我有用

switch ( $product->product_type ) {
        case "variable" :
            $link   = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
            $label  = woocommerce_variable_add_to_cart();
        break;

Let me know if this helps :)

让我知道这是否有帮助:)

回答by Guilherme Marconi

I found on Remi Corson's bloga simple way to do that.
Change the hrefvalue of the cart button with the following:

我在Remi Corson 的博客上找到了一种简单的方法来做到这一点。 使用以下内容
更改href购物车按钮的值:

http://example.com/cart/?add-to-cart=[PRODUCT_ID]&variation_id=[VARIATION_ID]&attribute_pa_[ATTRIBUTE_SLUG]=[ATTRIBUTE_SLUG_VALUE]

Example:

例子:

http://example.com/cart/?add-to-cart=123&variation_id=456&attribute_pa_colour=black

With the get_available_variations();function is easy to get the variation values. For the product ID, you can use get_the_ID();function.

使用该get_available_variations();函数很容易得到变化值。对于产品 ID,您可以使用get_the_ID();函数。