2017-11-22 7 views
1

をテーマ設定WP用Woocommerceグループ化された製品の機能を構築し、私が最初に製品単一のページにリダイレクトすることなく、1つのページに私のカートに製品の選択を行うために私のためのオプションを戻すには、この機能を内蔵していますWooCommerceで

/** 
* [browns_add_to_cart Add to Cart Button function - filtering if simple or grouped or variable product -> button] 
* @return 
*/ 
function brown_foods_variables_add_to_cart(){ 

    global $product; 

    $link = array(
     'url' => '', 
     'label' => '', 
     'class' => '' 
    ); 
    switch ($product->get_type()) { 
     case "variable" : 
      $link['url'] = apply_filters('woocommerce_variable_add_to_cart', get_permalink($product->get_id())); 
      $link['label'] = apply_filters('variable_add_to_cart_text', __('Select options', 'woocommerce')); 
     break; 
     case "grouped" : 
      $link['url'] = apply_filters('grouped_add_to_cart_url', get_permalink($product->get_id())); 
      $link['label'] = apply_filters('grouped_add_to_cart_text', __('View options', 'woocommerce')); 
     break; 
     case "external" : 
      $link['url'] = apply_filters('external_add_to_cart_url', get_permalink($product->get_id())); 
      $link['label'] = apply_filters('external_add_to_cart_text', __('Read More', 'woocommerce')); 
     break; 
     default : 
      if ($product->is_purchasable()) { 
       $link['url'] = apply_filters('add_to_cart_url', esc_url($product->add_to_cart_url())); 
       $link['label'] = apply_filters('add_to_cart_text', __('Add to cart', 'woocommerce')); 
       $link['class'] = apply_filters('add_to_cart_class', 'add_to_cart_button'); 
      } else { 
       $link['url'] = apply_filters('not_purchasable_url', get_permalink($product->get_id())); 
       $link['label'] = apply_filters('not_purchasable_text', __('Read More', 'woocommerce')); 
      } 
     break; 
    } 
    // If there is a simple product. 
    if ($product->get_type() == 'simple') { 
     ?> 
     <form action="<?php echo esc_url($product->add_to_cart_url()); ?>" class="simple-cart cart" method="post" enctype="multipart/form-data"> 
     <?php 
     // Displays the quantity box. 
     woocommerce_quantity_input(); 

     if ($price_html = $product->get_price_html()) : 
      ?> 
      <span class="food-price"><?php echo $price_html; ?></span> 
      <?php 
     endif; 

     // Display the submit button. 
     echo sprintf('<button type="submit" data-product_id="%s" data-product_sku="%s" 
            data-quantity="1" class="%s button product_type_simple">%s</button>', 
      esc_attr($product->get_id()), 
      esc_attr($product->get_sku()), 
      esc_attr($link['class']), 
      esc_html($link['label'])); 
     ?> 
    </form> 

    <?php } 

    elseif ($product->get_type() == 'grouped') { 

     require get_template_directory() . '/woocommerce/single-product/add-to-cart/grouped.php'; 

    } 
    else 
    { 
     echo apply_filters('woocommerce_loop_add_to_cart_link', 
      sprintf('<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>', 
       esc_url($product->add_to_cart_url()), 
       esc_attr(isset($quantity) ? $quantity : 1), 
       esc_attr($product->get_id()), 
       esc_attr($product->get_sku()), 
       esc_attr(isset($class) ? $class : 'button'), 
       esc_html($product->add_to_cart_text()) 
      ), 
     $product); 
    } 
} 
グループ化された製品のオプションを返すために私を得るために

Warning: Invalid argument supplied for foreach() in .....\woocommerce\single-product\add-to-cart\grouped.php.

すべてのヘルプ:

は、製品がグループ化されたただし際に、他の文で返さないと、製品のオプションは、このように私はgrouped.php、これはエラーを返しますelseif ($product->get_type() == 'grouped')を必要としていました。

答えて

1

あなたはこのコードを交換しようとする必要があります。これにより

<?php } 

    elseif ($product->get_type() == 'grouped') { 

     require get_template_directory() . '/woocommerce/single-product/add-to-cart/grouped.php'; 

    } 
    else 
    { 

を:foreachループで不足している引数として

<?php } 

    elseif ($product->get_type() == 'grouped') { 

     foreach($product->get_children() as $children_id){ 
      $grouped_products[] = wc_get_product($children_id); 
     } 

     wc_get_template('single-product/add-to-cart/grouped.php', array('grouped_products' => $grouped_products)); 

    } 
    else 
    { 

は確か $grouped_productsです。

+0

あなたは魔術師です。魅力的な作品 – omukiguy