将产品差异添加到产品页面

时间:2014-11-07 02:34:26

标签: php jquery woocommerce

我正在处理http://fitboxmeals.com/order/,我想在我的woocommerce主题的function.php中使用这段代码,以便我的产品变体显示在膳食产品页面上。



if ( ! function_exists( ‘woocommerce_template_loop_add_to_cart’ ) ) { function woocommerce_template_loop_add_to_cart() { global $product; if ($product->product_type == "variable") { woocommerce_variable_add_to_cart(); } else { woocommerce_get_template(
'loop/add-to-cart.php' ); } } }




代码有效但现在当我将产品添加到购物车时,我想重置变体,以便用户可以在将产品添加到购物车后再次为同一产品或不同产品选择这些变体。



if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly global $woocommerce, $product, $post; ?>

<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>

<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr( json_encode( $available_variations ) ) ?>">
  <?php if ( ! empty( $available_variations ) ) : ?>
  <table class="variations" cellspacing="0">
    <tbody>
      <?php $loop=0 ; foreach ( $attributes as $name=>$options ) : $loop++; ?>
      <tr>
        <td class="label">
          <label for="<?php echo sanitize_title($name); ?>">
            <?php echo wc_attribute_label( $name ); ?>
          </label>
        </td>
        <td class="value">
          <select id="<?php echo esc_attr( sanitize_title( $name ) ); ?>" name="attribute_<?php echo sanitize_title( $name ); ?>">
            <option value="">
              <?php echo __( 'Choose an option', ETHEME_DOMAIN ) ?>&hellip;</option>
            <?php if ( is_array( $options ) ) { if ( isset( $_REQUEST[ 'attribute_' . sanitize_title( $name ) ] ) ) { $selected_value=$ _REQUEST[ 'attribute_' . sanitize_title( $name ) ]; } elseif ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) { $selected_value=$
            selected_attributes[ sanitize_title( $name ) ]; } else { $selected_value='' ; } // Get terms if this is a taxonomy - ordered if ( taxonomy_exists( $name ) ) { $orderby=w c_attribute_orderby( $name ); switch ( $orderby ) { case 'name' : $args=a
            rray( 'orderby'=>'name', 'hide_empty' => false, 'menu_order' => false ); break; case 'id' : $args = array( 'orderby' => 'id', 'order' => 'ASC', 'menu_order' => false, 'hide_empty' => false ); break; case 'menu_order' : $args = array( 'menu_order' => 'ASC',
            'hide_empty' => false ); break; } $terms = get_terms( $name, $args ); foreach ( $terms as $term ) { if ( ! in_array( $term->slug, $options ) ) continue; echo '
            <option value="' . esc_attr( $term->slug ) . '"
            ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>'; } } else { foreach ( $options as $option ) { echo '
            <option value="' . esc_attr( sanitize_title( $option ) ) . '"
            ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>'; } } } ?>
          </select>
          <?php if ( sizeof($attributes)==$ loop ) echo '<a class="reset_variations" href="#reset">' . __( 'Clear selection', ETHEME_DOMAIN ) . '</a>'; ?>
        </td>
      </tr>
      <?php endforeach;?>
    </tbody>
  </table>

  <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

  <div class="single_variation_wrap" style="display:none;">
    <?php do_action( 'woocommerce_before_single_variation' ); ?>

    <div class="single_variation"></div>

    <div class="variations_button">
      <?php woocommerce_quantity_input(); ?>
      <button type="submit" class="single_add_to_cart_button button big active alt"><span><?php echo $product->single_add_to_cart_text(); ?></span>
      </button>
    </div>

    <input type="hidden" name="add-to-cart" value="<?php echo $product->id; ?>" />
    <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
    <input type="hidden" name="variation_id" value="" />

    <?php do_action( 'woocommerce_after_single_variation' ); ?>
  </div>

  <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>

  <?php else : ?>

  <p class="stock out-of-stock">
    <?php _e( 'This product is currently out of stock and unavailable.', ETHEME_DOMAIN ); ?>
  </p>

  <?php endif; ?>

</form>

<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
&#13;
&#13;
&#13;

我希望这会有所帮助,任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:0)

事件处理程序应该真正附加在javascript / jQuery中,而不是HTML属性,但是因为你的setTimeout适合你,所以应该这样:

<form onsubmit="
    $.ajax({
        url: location.href,
        type: 'post',
        headers: {
            'enctype': 'multipart/form-data'
        },
        data: $(this).serialize()
    }).then(function() {
        //tidy the page
        $('select.whatever').each(function() {
            this.selectedIndex = 0;
        }).eq(0).trigger('change');
    });
    return false;" class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr( json_encode( $available_variations ) ) ?>" >

我从未尝试过以这种方式定义如此广泛的事件处理程序,但是,AFAIK,它有可能工作(在调试时)。

您需要将“what”更改为应用于产品变体选择菜单的类名。目前它们似乎具有相同的ID,因此您应该只需将id=更改为class=标记中的<select>

答案 1 :(得分:0)

此插件可在产品页面上的变体表中完美运行: http://www.eggplantstudios.ca/woocommerce-product-variation-add-cart-grid/