[英]Problems creat plugin woocommerce
我正在嘗試制作一個將在文章頁面上顯示已定義數量列表的插件。 我有2個問題:
1 /當產品可變或簡單時,我似乎無法包含php文件。 2 /我想在購物車頁面中看到預定義數量的下拉菜單
插件的目的是限制mini和maxi產品的數量
這是我嘗試的代碼
<?php
/*
Plugin Name: Woo Best Drop Down
Plugin URI: http://www.fr
Description: Drop Down
Version: 1.0
Author: xxx
Author URI: http://xxx.fr
*/
function tab_woo_drop_dwon() {
?>
<li class="woo_best_drop_down_tab"><a href="#woo_best_drop_down_tab"><?php _e('Woo Best Drop Down', 'woo-best-drop-down'); ?></a></li>
<?php }
add_action('woocommerce_product_write_panel_tabs', 'tab_woo_drop_dwon');
function woo_tab_best_drop_down() {
global $post;
$woo_tab_best_drop_down = array(
'hop' => get_post_meta($post->ID, 'woo_best_drop_down_text', true),
'enabled' => get_post_meta($post->ID, 'woo_best_drop_down_enabled', true),
);
?>
<div id="woo_best_drop_down_tab" class="panel woocommerce_options_panel">
<div class="options_group">
<p class="form-field">
<?php woocommerce_wp_checkbox( array( 'id' => 'woo_best_drop_down_enabled', 'label' => __('Activer le seuil du stock faible?', 'woo-best-drop-down'), 'description' => __('Cochez la case pour activer le stock faible personalisé pour ce produit', 'woo-best-drop-down') ) ); ?>
</p>
</div>
<div class="options_group woo_tab_best_drop_down">
<p class="form-field">
<label><?php _e('Seuil du stock faible pour cet article', 'woo-icon-stock'); ?></label>
<input type="textarea" name="woo_best_drop_down_text" value="<?php echo @$woo_tab_best_drop_down['hop']; ?>" placeholder="<?php _e('Saisissez le seuil de stock', 'woo-best-drop-down'); ?>" />
</p>
</div>
</div>
<?php }
add_action('woocommerce_product_write_panels', 'woo_tab_best_drop_down');
function woo_best_drop_down_custom_tab( $post_id ) {
update_post_meta( $post_id, 'woo_best_drop_down_enabled', ( isset($_POST['woo_best_drop_down_enabled']) && $_POST['woo_best_drop_down_enabled'] ) ? 'yes' : 'no' );
update_post_meta( $post_id, 'woo_best_drop_down_text', $_POST['woo_best_drop_down_text']); }
add_action('woocommerce_process_product_meta', 'woo_best_drop_down_custom_tab');
if ( $product->product_type == 'simple' ){include 'simple_drop_down.php';}
elseif ( $product->product_type == 'variable' ) {include 'variable_drop_down.php';}
?>
簡單刪除dwon.php
<?php
add_action( 'woocommerce_after_add_to_cart_button' , 'add_woo_best_drop_dwon');
function add_woo_best_drop_dwon(){
global $post,$product,$woocommerce;
$truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);
$values = explode(',',$truc);
$validate = get_post_meta($post->ID, 'woo_best_drop_down_enabled', true);
if (( $validate == 'yes' ) ){
echo "<style>.quantity.buttons_added {
display: none !important;}</style><div class='quantity_select'><select name='quantity'>";
foreach($values as $v){
echo "<option value='$v'>$v</option>";}
echo "</select></div>";
}}
?>
可變放置dwon.php
<?php
add_action( 'woocommerce_after_single_variation' , 'add_woo_best_drop_dwon');
function add_woo_best_drop_dwon(){
global $post,$product,$woocommerce;
$truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);
$values = explode(',',$truc);
$validate = get_post_meta($post->ID, 'woo_best_drop_down_enabled', true);
if (( $validate == 'yes' ) ){
echo "<style>.quantity.buttons_added {
display: none !important;}</style><div class='quantity_select'><select name='quantity'>";
foreach($values as $v){
echo "<option value='$v'>$v</option>";}
echo "</select></div>";
}}
?>
謝謝
我找到了解決方案。 我為可變和簡單產品創建了一個文件。 我已經刪除了文件drop_down.php和simple_drop_down.php,我已經創建了drop_down.php並編寫了這個代碼:
<?php
add_action( 'woocommerce_after_add_to_cart_button' , 'add_woo_best_drop_dwon');
function add_woo_best_drop_dwon(){
global $post,$product,$woocommerce;
$truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);
$values = explode(',',$truc);
$validate = get_post_meta($post->ID, 'woo_best_drop_down_enabled', true);
if (( $validate == 'yes' ) ){
echo "<style>.quantity.buttons_added {
display: none !important;}</style><div class='quantity_select'><select name='quantity'>";
foreach($values as $v){
echo "<option value='$v'>$v</option>";}
echo "</select></div>";
}}
apply_filters( 'woocommerce_quantity_input' , 'add_woo_best_drop_down_variable');
function add_woo_best_drop_down_variable(){
global $post,$product,$woocommerce;
$truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);
$values = explode(',',$truc);
$validate = get_post_meta($post->ID, 'woo_best_drop_down_enabled', true);
if (( $validate == 'yes' ) ){
echo "<style>.quantity.buttons_added {
display: none !important;}</style><div class='quantity_select'><select name='quantity'>";
foreach($values as $v){
echo "<option value='$v'>$v</option>";}
echo "</select></div>";
}}
?>
現在我的問題在購物車中。 如果您進入頁面購物車,則所選產品的數量不會下降。 有想法在頁面購物車中添加下拉數量嗎?
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.