修改WooThemes' Min / Max Qty'插件使用以下代码?

时间:2016-07-08 08:24:34

标签: php wordpress woocommerce

我需要一些帮助才能让以下工作:

我正在使用Wordpress插件Woocommerce创建一个电子商务网站,并且有许多产品具有以网格格式显示的变体。您可能知道默认的Woocommerce显示变体的方式是使用下拉菜单,这对于我定位的用户市场来说并不实际。相反,我从www.eggplantstudios.ca/woocommerce-product-variation-add-cart-grid/找到了一段代码,它改变了变体的显示,以网格格式显示,每个都有自己的'添加到车'按钮。

这很有效。

但是,我的下一个挑战是找到一些可以为不同变量设置最小数量限制订单的东西。我研究并发现了一个由WooThemes提供的插件' Min / max数量'。我使用文档安装并设置了插件,在后端设置了限制,我准备好了。

不幸的是,这个插件没有工作,并且无论我订购多少都显示错误消息。请参见链接here

我的第一步是联系Woothemes并询问可能导致问题的原因。他们带着步骤找回问题并告诉我将主题更改为默认的Wordpress。这是否有效。

所以我的猜测是我从www.eggplantstudios.ca/woocommerce-product-variation-add-cart-grid/添加的代码与插件冲突...:((

现在对Php的经验很少,我不知道如何推进它以使其正常运行。我真的想保持网格,因为我认为这是一种更好的显示变化的方式。

有人能指出我可能导致问题的地方吗?这是一个简单的修复?

我感谢任何帮助:)

由于

我的functions.php文件中的代码:

function woocommerce_variable_add_to_cart(){
    global $product, $post;

    $variations = find_valid_variations();

    // Check if the special 'price_grid' meta is set, if it is, load the default template:
    if ( get_post_meta($post->ID, 'price_grid', true) ) {
        // Enqueue variation scripts
        wp_enqueue_script( 'wc-add-to-cart-variation' );

        // Load the template
        wc_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()
            ) );
        return;
    }

    // Cool, lets do our own template!
    ?>





    <table class="variations variations-grid" cellspacing="0">
        <thead>
            <tr>
                <th>Container Size</th>
                <th>Height</th> 
                <th>Description</th>
                <th>Quantity</th>
            </tr>
        </thead>
        <tbody>


 <?php
            foreach ($variations as $key => $value) {
                if( !$value['variation_is_visible'] ) continue;
            ?>   
            <tr>
                    <?php foreach($value['attributes'] as $key => $val ) {
                        $val = str_replace(array('','_'), ' ', $val);
                        printf( '<td class="attr attr-%s">%s</td>  ', $key, ucwords($val) );
                    } 

                    ?>
                   <td class="variation-description">
                   <?php $desc = $value['variation_description'] ?>

                   <?php if($desc) { ?>

                        <?php echo $desc ?>

                    <?php  }?>

               </td>
<td> <?php woocommerce_quantity_input(); ?></td>
                <td>
                    <?php echo $value['price_html'];?>
                </td>
                <td>
                    <?php if( $value['is_in_stock'] ) { ?>
                    <form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>

                        <?php
                        if(!empty($value['attributes'])){
                            foreach ($value['attributes'] as $attr_key => $attr_value) {
                            ?>
                            <input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
                            <?php
                            }
                        }
                        ?>
                        <button type="submit" class="single_add_to_cart_button btn btn-primary"><span class="glyphicon glyphicon-tag"></span> Add to cart</button>
                        <input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
                        <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
                        <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $post->ID ); ?>" />

                    </form>

                    <?php } else { ?>
                        <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
                    <?php } ?>
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
    <?php
}

function find_valid_variations() {
    global $product;

    $variations = $product->get_available_variations();
    $attributes = $product->get_attributes();
    $new_variants = array();

    // Loop through all variations
    foreach( $variations as $variation ) {

        // Peruse the attributes.

        // 1. If both are explicitly set, this is a valid variation
        // 2. If one is not set, that means any, and we must 'create' the rest.

        $valid = true; // so far
        foreach( $attributes as $slug => $args ) {
            if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {
                // Exists

            } else {
                // Not exists, create
                $valid = false; // it contains 'anys'
                // loop through all options for the 'ANY' attribute, and add each
                foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
                    $attribute = trim( $attribute );
                    $new_variant = $variation;
                    $new_variant['attributes']["attribute_$slug"] = $attribute;
                    $new_variants[] = $new_variant;
                }

            }
        }

        // This contains ALL set attributes, and is itself a 'valid' variation.
        if( $valid )
            $new_variants[] = $variation;

    }

    return $new_variants;
} 

0 个答案:

没有答案