Jan*_*vic 5 php wordpress checkout woocommerce
我想在 Woocommerce 结帐页面中向特定运输类别设置一条消息。
\n我尝试使用:
\nadd_action( \'woocommerce_review_order_before_order_total\', \'cart_items_shipping_class_message\', 20, 1 );\nfunction cart_items_shipping_class_message( $cart ){\n\n $shipping_class_id = 14; // Your shipping class Id\n\n // Loop through cart items\n foreach( WC()->cart->get_cart() as $cart_item ){\n // Check cart items for specific shipping class, displaying a notice\n //if( $cart_item[\'data\']->get_shipping_class_id() == $shipping_class_id ){\n var_dump($cart_item[\'data\']->get_shipping_class_id());\n echo "Do pozn\xc3\xa1mky k objedn\xc3\xa1vce napi\xc5\xa1te adresu Z\xc3\xa1silkovny nebo ZBoxu, kam chcete objedn\xc3\xa1vku doru\xc4\x8dit.";\n //break;\n //}\n } \n}\nRun Code Online (Sandbox Code Playgroud)\n但是当我尝试var_dump选择运输类别时,它总是返回我int(0)。
我使用了类似的东西:Cart Message for a Specific Shipping Class in WooCommerce,因为它对我不起作用。
\nWooCommerce 答案中特定运输类别的购物车消息仍然适用于上一个 woocommerce 版本。
\n您的问题代码有效并显示运输类别 Id\xe2\x80\xa6 所以还有其他问题造成了麻烦。
\n确保购物车商品已设置所需的运输类别。
\n尝试执行以下操作,在结账时根据特定运输类别slug显示一条消息:
\nadd_action( \'woocommerce_review_order_before_order_total\', \'checkout_shipping_class_message\' );\nfunction checkout_shipping_class_message(){\n // Here your shipping class slugs in the array\n $shipping_classes = array(\'truck\');\n // Here your shipping message\n $message = __("Please add some shipping details in order notes field.", "woocommerce");\n\n // Loop through cart items\n foreach( WC()->cart->get_cart() as $cart_item ){\n $shipping_class = $cart_item[\'data\']->get_shipping_class();\n \n // echo \'<pre>\' . print_r($shipping_class, true) . \'</pre>\'; // Uncomment for testing\n\n // Check cart items for specific shipping class, displaying a message\n if( in_array($shipping_class, $shipping_classes ) ){\n echo \'<tr class="shipping-note">\n <td colspan="2"><strong>\'.__("Note", "woocommerce").\':</strong> \'.$message.\'</td>\n </tr>\';\n break;\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
\n| 归档时间: |
|
| 查看次数: |
1609 次 |
| 最近记录: |