Php Woocommerce:从插件获取一些定制购物车项目数据值

Php Woocommerce:从插件获取一些定制购物车项目数据值,php,arrays,wordpress,woocommerce,cart,Php,Arrays,Wordpress,Woocommerce,Cart,我正在尝试从购物车中的产品中获取特定值。 我需要这个值作为在特定类别中设置价格的乘数,我已经完成了大部分工作,除了我在获取这个值时遇到问题,我通过一个名为uni-cpo的插件设置了这个值 从var_转储(裁剪) 我需要获取浮点值2000,但在数组中导航时遇到问题 add_action( 'woocommerce_before_cart', 'get_sqm', 99); function get_sqm($length){ global $woocommerce; $leng

我正在尝试从购物车中的产品中获取特定值。 我需要这个值作为在特定类别中设置价格的乘数,我已经完成了大部分工作,除了我在获取这个值时遇到问题,我通过一个名为uni-cpo的插件设置了这个值

从var_转储(裁剪)

我需要获取浮点值2000,但在数组中导航时遇到问题

add_action( 'woocommerce_before_cart', 'get_sqm', 99);
function get_sqm($length){
     global $woocommerce;
     $length = 0;
     foreach ($woocommerce->cart->cart_contents as $cart_item) {
         $item_id = $cart_item['6299'];
         if (isset($cart_item['data']->uni_nov_cpo_stof_sqm->uni_nov_cpo_stof_sqm->value)) {
             $length = $cart_item['data']->uni_nov_cpo_stof_sqm->uni_nov_cpo_stof_sqm->value;
             break;
         }
     }
     echo '<span class="hello">hello' . $length . '</span>';
    //Debug, comment to disable
    if (current_user_can('administrator') ) {
        echo '<pre>';
                var_dump( $cart_item );
            
        echo '</pre>';
    
    } //Debug end
     return $length;
 }
add_action('woocommerce_-before_-cart','get_-sqm',99);
函数get_sqm($length){
全球商业;
$length=0;
foreach($woocommerce->cart->cart\u内容作为$cart\u项目){
$item_id=$cart_item['6299'];
if(isset($cart_项目['data']->uni_nov_cpo_stof_sqm->uni_nov_cpo_stof_sqm->value)){
$length=$cart_item['data']->uni_nov_cpo_stof_sqm->uni_nov_cpo_stof_sqm->value;
打破
}
}
回显“你好”。$length;
add_action( 'woocommerce_before_cart', 'get_sqm', 99 );
function get_sqm() {
     $targeted_id = 6299;
     $length = 0;
     
     foreach (WC()->cart->get_cart() as $cart_item) {
         $product_id = $cart_item['product_id'];

         if ( isset( $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'] ) ) {
             $length = $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'];

             echo '<span class="hello">hello' . $length . '</span>';
             break;
         }
     }
     // return $length; // Not needed in an action hook
 }
//调试,注释以禁用 if(当前用户可以(“管理员”)){ 回声';
add_action( 'woocommerce_before_cart', 'get_sqm', 99 );
function get_sqm() {
     $targeted_id = 6299;
     $length = 0;
     
     foreach (WC()->cart->get_cart() as $cart_item) {
         $product_id = $cart_item['product_id'];

         if ( isset( $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'] ) ) {
             $length = $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'];

             echo '<span class="hello">hello' . $length . '</span>';
             break;
         }
     }
     // return $length; // Not needed in an action hook
 }
var_dump($cart_项目); 回声';
add_action( 'woocommerce_before_cart', 'get_sqm', 99 );
function get_sqm() {
     $targeted_id = 6299;
     $length = 0;
     
     foreach (WC()->cart->get_cart() as $cart_item) {
         $product_id = $cart_item['product_id'];

         if ( isset( $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'] ) ) {
             $length = $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'];

             echo '<span class="hello">hello' . $length . '</span>';
             break;
         }
     }
     // return $length; // Not needed in an action hook
 }
}//调试结束 返回$length; }

我不知道,对于我的代码的某些部分,我在正确的轨道上,但是在数组中导航的时候却偏离了方向。如果有人能帮上忙,给我指出正确的方向,或者提出解决方案,我将永远感激。已经挣扎了几个星期,到了这个地步。学习很痛苦!:)

根据裁剪的
var_dump
,要获得所需的值(2000),请尝试以下重新访问的代码:

add_action('woocommerce_-before_-cart','get_-sqm',99);
函数get_sqm(){
$targeted_id=6299;
$length=0;
foreach(WC()->cart->get_cart()作为$cart_项目){
$product_id=$cart_项目['product_id'];
如果(isset($cart_item[''u cpo_nov']['uni_nov_cpo_stof_sqm']['value'])){
$length=$cart_item[''u cpo_nov']['uni_nov_cpo_stof_sqm']['value'];
回显“你好”。$length;
add_action( 'woocommerce_before_cart', 'get_sqm', 99 );
function get_sqm() {
     $targeted_id = 6299;
     $length = 0;
     
     foreach (WC()->cart->get_cart() as $cart_item) {
         $product_id = $cart_item['product_id'];

         if ( isset( $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'] ) ) {
             $length = $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'];

             echo '<span class="hello">hello' . $length . '</span>';
             break;
         }
     }
     // return $length; // Not needed in an action hook
 }
打破 } } //return$length;//在操作挂钩中不需要 }

代码进入活动子主题(或活动主题)的functions.php文件。它应该会起作用。

效果很好。非常感谢你!关于动作钩,我只是为了得到一个视觉反馈而添加了它。这只是一个函数。