Woocommerce 3.0 GET变化名
问题描述:
嗨,我有下面的代码,将其与旧版本Woocommerce工作取得的变化名称的提取物及其价格,现在不会在WC upgarde之后再工作到3.0Woocommerce 3.0 GET变化名
function shortcode_handler($atts, $content = "", $shortcodename = "", $meta = "")
{
$output = "";
$meta['el_class'];
global $woocommerce, $product;
if(!is_object($woocommerce) || !is_object($woocommerce->query) || empty($product)) return;
// $product = wc_get_product();
$output .= "<div class='av-woo-calendar-button ".$meta['el_class']."'>";
ob_start();?>
<table cellspacing="0" cellpadding="2">
<thead>
<tr>
<th scope="col" style="text-align:left; background-color:rgba(155, 199, 239, 0.5); vertical-align:middle;"><?php _e('Cruise', 'whale_dolphins'); ?></th>
<th scope="col" style="text-align:center;background-color:rgba(155, 199, 239, 0.5);"><?php _e('Places<br/>available', 'whale_dolphins'); ?></th>
</tr>
</thead>
<tbody>
<?php
$args = array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_stock',
'value' => array('', false, null),
'compare' => 'NOT IN'
)
)
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
$product = new WC_Product_Variation($loop->post->ID);
if (get_the_title($loop->post->post_parent) == 'CSR Expeditions Payment' || get_the_title($loop->post->post_parent) == 'Pagamento Spedizioni CSR')
{
$variation_formatted_name = $product->get_formatted_name();
$variation_name_array = explode("–", $variation_formatted_name);
$variation_name = $variation_name_array[2];
$variation_price = $variation_name_array[3];
$variation_sku = $variation_name_array[0];
$variation_name_only = explode(":", $variation_name);
$variation_name_only = $variation_name_only[1];
?>
<tr>
<td><?php echo $variation_name_only ."–" . $variation_price; ?></td>
<?php
if (intval ($product->stock) >0)
{ ?>
<td style="text-align:center; font-size: 13px; background-color:#70C940; color:rgba(255,255,255,1);"><?php echo intval ($product->stock); ?></td>
<?php
}
else
{ ?>
<td style="text-align:center; font-size: 13px; background-color:#D11E1B;color:rgba(255,255,255,1);"> <?php echo intval ($product->stock); ?></td>
<?php
}
?>
</tr>
<?php
}
endwhile;
?>
</tbody>
</table>
<?php
$output .= ob_get_clean();
$output .= "</div>";
return $output;
}
特别是我想象的代码下面的部分是修改
$product = new WC_Product_Variation($loop->post->ID);
if (get_the_title($loop->post->post_parent) == 'CSR Expeditions Payment' || get_the_title($loop->post->post_parent) == 'Pagamento Spedizioni CSR')
{
$variation_formatted_name = $product->get_formatted_name();
$variation_name_array = explode("–", $variation_formatted_name);
$variation_name = $variation_name_array[2];
$variation_price = $variation_name_array[3];
$variation_sku = $variation_name_array[0];
$variation_name_only = explode(":", $variation_name);
$variation_name_only = $variation_name_only[1];
我如何能解决这个问题的任何建议?非常感谢您
答
议决
我发现它在这里贴在下面的anyboduìy可能需要它的解决方案。
随着新Woocommerce如下“产品名(#variation_id)” $product->get_formatted_name();
返回字符串的命令
随着variation_id然而也能够获得从postmeta表属性元件蛞蝓。使用slug,您可以从术语表中获取属性元素名称。
$product = new WC_Product_Variation ($loop->post->ID);
if (get_the_title($loop->post->post_parent) == 'CSR Expeditions Payment' || get_the_title($loop->post->post_parent) == 'Pagamento Spedizioni CSR')
{
//1) I need to get the variation id == post id in wp_postmeta table
// get the name and id of the variation in a string
$variation_formatted_name = $product->get_formatted_name();
//explode the string to get an array of the string
$variation_formatted_name_array = explode("#", $variation_formatted_name);
//get only the variation id (which comes along with a closed parenthesis
$variation_id_ = $variation_formatted_name_array[1];
//explode the string in an array made by the variation id and the parenthesis
$variation_id = explode(")", $variation_id_);
//get only variation id
$variation_id_only = $variation_id[0];
//2)
//get the taxonomy needed
$taxonomy = 'pa_csr-dates';
//get the meta value (= attribute slug) from the postmeta table through the variation_id (= post_id)
$meta = get_post_meta($variation_id_only, 'attribute_'.$taxonomy, true);
//get the variation name from the terms table through the slug (=$meta)
$term = get_term_by('slug', $meta, $taxonomy);
//get the variation name (attribute element custom name)
$variation_name_only = $term->name;
$variation_price = $product->get_price();
}
例如'$产品 - >股票;'应现在[**'$产品 - > get_stock_quantity();'**](https://docs.woocommerce.com/wc-apidocs/我们可以使用$ product = wc_get_product($ loop-> post-> ID);你可以试试'$ product = wc_get_product($ loop-> post-> > ID);'...你必须在对象和数组上使用'print_r()'或'var_dump()'来测试它... – LoicTheAztec
Hi @LoicTheAztec,我试过你的建议,但是没有奏效。关于股票,$ product-> stock的方式与$ product-> get_stock_quantity()相同。我可以得到适当的库存数量。但是,问题仍然存在于变体名称中。即使我使用函数wc_get_product –
顺便说一下,代码基于以下示例https://mikejolley.com/2011/12/05/woocommerce-output-我只能得到变体slug,而不能获得全名, A-简单打印的-stockinventory报告/ –