Welcome to Stack Overflow!(欢迎使用Stack Overflow!)
To answer your questions:(要回答您的问题:)
"Verify that the code is valid"(“验证代码是否有效”)
The code looks quite good (especially if this is really your first time messing with PHP)(代码看起来不错(特别是如果这确实是您第一次弄乱PHP))
"and (code) does not create problems in future woocommerce updates."(“并且(代码)在将来的woocommerce更新中不会产生问题。”)
This question is really hard to answer.(这个问题真的很难回答。)
"Now I am trying to put these two lines of information in the product page. via shortcode? or php again ?"(“现在,我正在尝试将这两行信息放入产品页面中。通过简码?还是再次使用php?”)
Probably with more filters/actions, like you have done in the current code.(可能具有更多过滤器/操作,就像您在当前代码中所做的那样。) And if that doesn't seem possible, maybe by altering/overriding some template(s).(如果这似乎不可能,则可以通过更改/覆盖某些模板来实现。) Shortcodes don't necessarily make much sense, unless you want to insert the data into content area's in the backend when adding/editing products.(除非您想在添加/编辑产品时将数据插入后端的内容区域中,否则简码不一定有意义。)
"Also improve the code"(“还改进代码”)
This doesn't seem necessary.(这似乎没有必要。)
"and add a prefix to Estimated Quantity."(“并在预估数量上添加前缀。”)
Can you elaborate on what you mean by this?(您能详细说明一下这是什么意思吗?)
ps.(ps。) I am probably off to bed soon, so I'll check again tomorrow.(我可能很快就要睡觉了,所以明天再检查一次。)
Update(更新资料)
I send qty and product_id to the server with AJAX and then the server gives back qty * _estimated_quantity;(我使用AJAX将qty和product_id发送到服务器,然后服务器返回qty * _estimated_quantity;)
To get qty
from the box on your screen and to set the right value in Estimated quantity
, I need you to make a screenshot of their code.(为了从屏幕上的框中获取qty
并在“ Estimated quantity
设置正确的值,我需要您对其代码进行屏幕截图。) Open the page in Google Chrome, right click > inspect element on the quantity box and then make a screenshot of the code in the Elements
tab.(在Google Chrome浏览器中打开该页面,右键单击>在数量框中检查元素,然后在“ Elements
标签中制作代码的屏幕截图。) Also do the same for the Estimated quantity
box (that has 15 in it in the screenshot).(也对“ Estimated quantity
框(在屏幕快照中有15个)执行相同的操作。)
I also assume your website is located on the root of your domain.(我还假设您的网站位于您域的根目录上。) So on www.example.com and not on www.example.com/wordpress/ (The Ajax call needs the right location).(因此,请访问www.example.com,而不要访问www.example.com/wordpress/(Ajax调用需要正确的位置)。)
You can add this PHP to your functions.php
(您可以将此PHP添加到您的functions.php
)
<?php
add_action("wp_ajax_product_get_estimated_quantity", "product_get_estimated_quantity"); // calls our function if user is logged in
add_action("wp_ajax_nopriv_product_get_estimated_quantity", "product_get_estimated_quantity"); // calls our function if user is not logged in, we do not do any security checks on which user etc though, but I don't think it's relevant in our case.
function product_get_estimated_quantity () {
$product_id = $_REQUEST["product_id"];
if (!is_numeric($product_id)) { // check if really a number, for security purposes
die();
}
$qty = $_REQUEST["qty"];
if (!is_numeric($qty)) { // check if really a number, for security purposes
die();
}
// No need to get product object, can get metadata directly with get_post_meta
// // get estimated_quantity per kg by product_id
// $args = array(
// 'include' => array( $product_id ),
// );
// $products = wc_get_products( $args );
// foreach ($products as $product) {
// }
// echo $product->get_id();
$estimated_quantity_per_kg = get_post_meta( $product_id, '_estimated_quantity', true);
// $estimated_quantity_per_kg = 15; // this line is only for my local testing, since my products dont have _estimated_quantity meta
echo ((int) $estimated_quantity_per_kg) * ((int) $qty);
die();
}
?>
And add this javascript/jquery anywhere you can (assuming that you know how to, its also easy to find out with google (How to add javascript to Wordpress)).(并在任何可能的地方添加此javascript / jquery(假设您知道该怎么做,使用google也很容易找到它(如何将Javascript添加到Wordpress))。)
jQuery(document).ready(function($) {
if( $('body.single-product').length) { // if on a single product page
$( document ).on( 'change', '.quantity input[type=number]', function() { // selector for the qty box is probably already correct
var qty = $( this ).val();
var product_id = $(this).closest('.product').attr('id');
product_id = product_id.split('-');
product_id = product_id[1];
jQuery.ajax({
type : "post",
dataType : "json",
url : '/wp-admin/admin-ajax.php', // only works if Wordpress is installed in root of your domain. if you installed in www.example.com/wordpress/, then add /wordpress/ before this
data : {action: "product_get_estimated_quantity", product_id : product_id, qty: qty},
success: function(response) {
// right click -> inspect element to find the right selector for estimated quantity (the 15 from your photoshop)
response = parseInt(response);
$('#snuwerd > div').val('Estimated quantity: ' + response);
}
});
return false;
});
}
});
In case you want to know how I added the javascript, i made a file in child-theme-directory/js/child.js.(如果您想知道如何添加JavaScript,我在child-theme-directory / js / child.js中创建了一个文件。) Then added the Jquery to it.(然后向其中添加Jquery。) And then I added this to functions.php
:(然后我将其添加到functions.php
:)
function theme_enqueue_child_scripts() {
$file = '/js/child.js';
$cache_buster = date("YmdHis", filemtime( get_stylesheet_directory() . $file )); // no uri
wp_enqueue_script( 'child', get_stylesheet_directory_uri() . $file, array( 'jquery' ), $cache_buster, true ); // true = footer
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_child_scripts' );
Update(更新资料)
I added the selector for the Estimated quantity field in the javascript, and the quantity selector was already correct, so please re-copy the javascript.(我在javascript中为“估计数量”字段添加了选择器,并且数量选择器已经正确,因此请重新复制javascript。)
I looked at the screenshot where you add in JS and PHP, but the way you add PHP is not good enough for this PHP.(我看了您在其中添加JS和PHP的屏幕截图,但是添加PHP的方式对于此PHP来说还不够好。) Where you add PHP in the screenshot, it only counts for that page and it needs to be sidewide.(在屏幕快照中添加PHP的位置,它仅计入该页面,并且需要横向放置。) The Ajax call will not be able to access PHP that is added to a specific page.(Ajax调用将无法访问添加到特定页面的PHP。) If Elementor doesn't have a side wide place to add PHP and if you don't want to change the functions.php
of your theme, then maybe you can use this plugin: https://wordpress.org/plugins/my-custom-functions/ .(如果Elementor没有足够的地方添加PHP,并且您不想更改主题的functions.php
,则可以使用此插件: https : //wordpress.org/plugins/my-自定义功能/ 。) Also, don't forget to actually add the PHP I provided, I didn't see it in your JS/PHP screenshot.(另外,不要忘记实际添加我提供的PHP,在您的JS / PHP屏幕截图中也没有看到它。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…