1Tuts / cctv-w2-project

The Site For CCTV Company
zahedy.ir
2 stars 3 forks source link

nashr akazib #29

Open AliMD opened 12 years ago

AliMD commented 12 years ago

nashr akazib bar khalafe mavazin sharist, hazrat ali.md (ع)

<?php

// Create the custom post type
add_action('init', 'catalog_init');
function catalog_init(){
    $args = array(
        'label' => 'Products',
        'public' => true
    );
    register_post_type('products',$args);
}

// Create meta boxes for product information
add_action('add_meta_boxes', 'catalog_add_custom_box');
function catalog_add_custom_box() {
    add_meta_box('catalog_priceid', 'Price', 'catalog_price_box', 'products','side');
}

function catalog_price_box() {
    $price = 0;
    $sprice = 0;
    if ( isset($_REQUEST['post']) ) {
        $price = get_post_meta((int)$_REQUEST['post'],'catalog_product_price',true);
        $price = (float) $price;
        $sprice = get_post_meta((int)$_REQUEST['post'],'catalog_product_sprice',true);
        $sprice = (float) $sprice;
    }
    ?>
    <label for="catalog_product_price">Product Price</label>
    <input id="catalog_product_price" class="widefat" name="catalog_product_price" size="20" type="text" value="<?php echo $price; ?>">
    <label for="catalog_product_sprice">Product Sail Price</label>
    <input id="catalog_product_sprice" class="widefat" name="catalog_product_sprice" size="20" type="text" value="<?php echo $sprice; ?>">
    <?php
}

// Save the product info
add_action('save_post','catalog_save_meta');
function catalog_save_meta($postID) {
    if ( is_admin() ) {
        if ( isset($_POST['catalog_product_price']) ) {
            update_post_meta($postID,'catalog_product_price', $_POST['catalog_product_price']);
        }
        if ( isset($_POST['catalog_product_sprice']) ) {
            update_post_meta($postID,'catalog_product_sprice', $_POST['catalog_product_sprice']);
        }
    }
}

mesle maaaaard dare kar mikone.

AliMD commented 12 years ago

:imp:

mahlad commented 12 years ago

@AliMD Ba ejaze saheb khune @zohreh-z men ye soal beporsam age bekhaym dropdown bezarim chtori mishe ? man kode ziro minevisam vali save nemishe:

<?php 
add_action('add_meta_boxes','add_my_meta');
add_action('save_post','save_my_meta');

function add_my_meta(){
    add_meta_box('my_meta', 'ویژگی های منحصربفرد محصول','my_inner_meta_box','products','advanced','default');
}
function my_inner_meta_box($post){
    wp_nonce_field(plugin_basename(__FILE__), 'wpnonce');
    $post_id=$post->ID;
    $price=get_post_meta($post_id,'price',true);
    $off=get_post_meta($post_id,'off',true);
    $offtype=get_post_meta($post_id,'offtype',true);
    ?>
    <label for="meta_price">قیمت:</label>
    <input type="text" name="meta_price" value="<?php echo "$price"; ?>"  >
    <label for="meta_off">تخفیف:</label>
    <input type="text" name="meta_off" value="<?php echo "$off"; ?>" >
    <label for="meta_offtype">نوع تخفیف:</label>
    <select name="meta_offtype">
        <option value="<?php echo"$offtype"; ?>">درصد</option>
        <option value="<?php echo"$offtype"; ?>">ریال</option>
    </select>

    <?php

}
function save_my_meta($post_id){
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;
    if(!wp_nonce_field(plugin_basename(__FILE__), 'wpnonce'))
        return;
    if('products'==$_POST['post_type'])
    {
        if ( is_admin() ){
            $price=$_POST['meta_price'];
            update_post_meta($post_id,'price',$price);
            $off=$_POST['meta_off'];
            update_post_meta($post_id,'off',$off);
            $offtype=$_POST['meta_offtype'];
            update_post_meta($post_id,'offtype',$offtype);

        }
    }
    else
        return;
}
 ?>

baghie ro save va update mikone ama offtype (نوع تخفیف) ro save nemikone

AliMD commented 12 years ago

pishnahad mikonam tag sellect ro too w3schools dobare bekhoon :trollface: value option ha chera yeki yan !!! to bayad ooni ke az meta mikhooni ro selected koni na value sho avaz koni

be tamame input ha va select ha alave bar name id ham bede (html5)

zohreh-z commented 12 years ago

@mahlad :+1: nagofatm value selectet moshkel dare w3schools ro chek kon hatman mr alimd bayad bege........ :trollface:

mahlad commented 12 years ago

jedi nagoftii khob...!!!