在WoodMart迷你购物车小部件中为简单和变量产品添加总重量

huangapple go评论120阅读模式
英文:

Adding Total Weight for both simple and variable product In woodmart mini cart widget

问题

以下是您要翻译的代码部分:

  1. /* 在小购物车小部件页脚中显示总重量 */
  2. function display_mini_cart_total_weight() {
  3. if ( ! WC()->cart->is_empty() ) {
  4. $total_weight = 0;
  5. foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  6. $product = $cart_item['data'];
  7. $variation_id = $cart_item['variation_id'];
  8. $weight = 0;
  9. if ( $variation_id ) {
  10. // 获取所选的变种
  11. $variation = wc_get_product( $variation_id );
  12. if ( $variation ) {
  13. // 获取变种的重量
  14. $weight = $variation->get_weight();
  15. }
  16. } else {
  17. // 获取产品的重量
  18. $weight = $product->get_weight();
  19. }
  20. $quantity = $cart_item['quantity'];
  21. // 计算当前产品的重量
  22. $product_weight = $weight * $quantity;
  23. // 将产品的重量添加到总重量中
  24. $total_weight += $product_weight;
  25. }
  26. // 在小购物车小部件页脚中输出总重量
  27. $total_weight_display = $total_weight . '千克'; // 在总重量后添加 '千克'
  28. echo '<tr class="total-weight-row">
  29. <td colspan="3" class="total-weight-cell">
  30. <p class="total-weight-label woocommerce-mini-cart__total">' . __('总重量:', 'chahar-4-rahewordpress') . '</p>
  31. <p class="total-weight-value woocommerce-Price-amount amount">' . $total_weight_display . '</p>
  32. </td>
  33. </tr>';
  34. }
  35. }
  36. add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );

请注意,代码已经翻译成中文。

英文:

I am using Woodmart theme and on mini cart widget and I want to show total weight and total price of simple and variable product.
So I have modified the code but it is not working and has these problem below:

1. (Total weight): when a simple or variable product added to cart the total weight is shown half of the product weight. For example if the product weight set to 0.5 , when added to cart , on mini cart the total weight is shown 0.25.

2. (Total Price): when a simple or variable product added to cart the total price is shown half of the product price. For example if the product price based on weight (0.5) is 7500, when added to cart , on mini cart the total price is shown 3750.

在WoodMart迷你购物车小部件中为简单和变量产品添加总重量

Any help is appreciated. So many thanks.
Here is my code:

  1. /* Display the total weight in the mini cart shopping cart widget footer*/
  2. function display_mini_cart_total_weight() {
  3. if ( ! WC()-&gt;cart-&gt;is_empty() ) {
  4. $total_weight = 0;
  5. foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item ) {
  6. $product = $cart_item[&#39;data&#39;];
  7. $variation_id = $cart_item[&#39;variation_id&#39;];
  8. $weight = 0;
  9. if ( $variation_id ) {
  10. // Get the selected variation
  11. $variation = wc_get_product( $variation_id );
  12. if ( $variation ) {
  13. // Get the weight of the variation
  14. $weight = $variation-&gt;get_weight();
  15. }
  16. } else {
  17. // Get the weight of the product
  18. $weight = $product-&gt;get_weight();
  19. }
  20. $quantity = $cart_item[&#39;quantity&#39;];
  21. // Calculate the weight for the current product
  22. $product_weight = $weight * $quantity;
  23. // Add the product&#39;s weight to the total weight
  24. $total_weight += $product_weight;
  25. }
  26. // Output the total weight in the mini cart shopping cart widget footer
  27. $total_weight_display = $total_weight . &#39; Kg&#39;; // Append &#39; Kg&#39; to the total weight
  28. echo &#39;&lt;tr class=&quot;total-weight-row&quot;&gt;
  29. &lt;td colspan=&quot;3&quot; class=&quot;total-weight-cell&quot;&gt;
  30. &lt;p class=&quot;total-weight-label woocommerce-mini-cart__total&quot;&gt;&#39; . __(&#39;Total Weight:&#39;, &#39;chahar-4-rahewordpress&#39;) . &#39;&lt;/p&gt;
  31. &lt;p class=&quot;total-weight-value woocommerce-Price-amount amount&quot;&gt;&#39; . $total_weight_display . &#39;&lt;/p&gt;
  32. &lt;/td&gt;
  33. &lt;/tr&gt;&#39;;
  34. }
  35. }
  36. add_action( &#39;woocommerce_widget_shopping_cart_before_buttons&#39;, &#39;display_mini_cart_total_weight&#39; );

答案1

得分: 1

以下是您要求的代码的中文翻译部分:

  1. /* 在小购物车挂件底部显示总重量和价格 */
  2. function display_mini_cart_total_weight() {
  3. if ( ! WC()->cart->is_empty() ) {
  4. $total_weight = 0;
  5. $total_price = 0;
  6. foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  7. $weight = 0;
  8. $price = 0;
  9. $product = $cart_item['data'];
  10. $variation_id = $cart_item['variation_id'];
  11. if ( $variation_id ) {
  12. // 获取所选变种
  13. $variation = wc_get_product( $variation_id );
  14. if ( $variation ) {
  15. // 获取变种的重量和价格
  16. $weight = $variation->get_weight();
  17. $price = $variation->get_price();
  18. }
  19. } else {
  20. // 获取产品的重量和价格
  21. $weight = $product->get_weight();
  22. $price = $product->get_price();
  23. }
  24. $quantity = $cart_item['quantity'];
  25. if( $quantity < 1 ){
  26. $quantity = 1;
  27. }
  28. // 计算当前产品的重量和价格
  29. $product_weight = $weight * $quantity;
  30. $product_price = $price * $quantity;
  31. // 将产品的重量和价格添加到总重量和价格中
  32. $total_weight += $product_weight;
  33. $total_price += $product_price;
  34. }
  35. // 在小购物车挂件底部输出总重量和价格
  36. $total_weight_display = $total_weight . ' Kg'; // 在总重量后附加' Kg'
  37. $total_price_display = wc_price( $total_price ); // 将总价格格式化为 WooCommerce 价格
  38. echo '<tr class="total-weight-row">
  39. <td colspan="3" class="total-weight-cell">
  40. <p class="total-weight-label woocommerce-mini-cart__total">' . __('总重量:', 'chahar-4-rahewordpress') . '</p>
  41. <p class="total-weight-value woocommerce-Price-amount amount">' . $total_weight_display . '</p>
  42. </td>
  43. </tr>';
  44. echo '<tr class="total-price-row">
  45. <td colspan="3" class="total-price-cell">
  46. <p class="total-price-label woocommerce-mini-cart__total">' . __('总价格 1:', 'chahar-4-rahewordpress') . '</p>
  47. <p class="total-price-value woocommerce-Price-amount amount">' . $total_price_display . '</p>
  48. </td>
  49. </tr>';
  50. }
  51. }
  52. add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'display_mini_cart_total_weight' );

请注意,我已将代码翻译为中文,但其中的变量名和函数名等部分保持不变。

英文:

You can check if the quantity is less than 1 then you have to consider min qty as 1. Check the below code.

  1. /* Display the total weight and price in the mini cart shopping cart widget footer */
  2. function display_mini_cart_total_weight() {
  3. if ( ! WC()-&gt;cart-&gt;is_empty() ) {
  4. $total_weight = 0;
  5. $total_price = 0;
  6. foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item ) {
  7. $weight = 0;
  8. $price = 0;
  9. $product = $cart_item[&#39;data&#39;];
  10. $variation_id = $cart_item[&#39;variation_id&#39;];
  11. if ( $variation_id ) {
  12. // Get the selected variation
  13. $variation = wc_get_product( $variation_id );
  14. if ( $variation ) {
  15. // Get the weight and price of the variation
  16. $weight = $variation-&gt;get_weight();
  17. $price = $variation-&gt;get_price();
  18. }
  19. } else {
  20. // Get the weight and price of the product
  21. $weight = $product-&gt;get_weight();
  22. $price = $product-&gt;get_price();
  23. }
  24. $quantity = $cart_item[&#39;quantity&#39;];
  25. if( $quantity &lt; 1 ){
  26. $quantity = 1;
  27. }
  28. // Calculate the weight and price of the current product
  29. $product_weight = $weight * $quantity;
  30. $product_price = $price * $quantity;
  31. // Add the product&#39;s weight and price to the total weight and price
  32. $total_weight += $product_weight;
  33. $total_price += $product_price;
  34. }
  35. // Output the total weight and price in the mini cart shopping cart widget footer
  36. $total_weight_display = $total_weight . &#39; Kg&#39;; // Append &#39; Kg&#39; to the total weight
  37. $total_price_display = wc_price( $total_price ); // Format the total price as WooCommerce price
  38. echo &#39;&lt;tr class=&quot;total-weight-row&quot;&gt;
  39. &lt;td colspan=&quot;3&quot; class=&quot;total-weight-cell&quot;&gt;
  40. &lt;p class=&quot;total-weight-label woocommerce-mini-cart__total&quot;&gt;&#39; . __(&#39;Total Weight:&#39;, &#39;chahar-4-rahewordpress&#39;) . &#39;&lt;/p&gt;
  41. &lt;p class=&quot;total-weight-value woocommerce-Price-amount amount&quot;&gt;&#39; . $total_weight_display . &#39;&lt;/p&gt;
  42. &lt;/td&gt;
  43. &lt;/tr&gt;&#39;;
  44. echo &#39;&lt;tr class=&quot;total-price-row&quot;&gt;
  45. &lt;td colspan=&quot;3&quot; class=&quot;total-price-cell&quot;&gt;
  46. &lt;p class=&quot;total-price-label woocommerce-mini-cart__total&quot;&gt;&#39; . __(&#39;Total Price 1:&#39;, &#39;chahar-4-rahewordpress&#39;) . &#39;&lt;/p&gt;
  47. &lt;p class=&quot;total-price-value woocommerce-Price-amount amount&quot;&gt;&#39; . $total_price_display . &#39;&lt;/p&gt;
  48. &lt;/td&gt;
  49. &lt;/tr&gt;&#39;;
  50. }
  51. }
  52. add_action( &#39;woocommerce_widget_shopping_cart_before_buttons&#39;, &#39;display_mini_cart_total_weight&#39; );

huangapple
  • 本文由 发表于 2023年7月6日 19:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定